<html><head><meta http-equiv="Content-Type" content="text/html; charset=us-ascii"><meta name="Generator" content="Microsoft Word 12 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style></head><body lang="EN-US" link="blue" vlink="purple"><div class="WordSection1"><p class="MsoNormal">Greetings,</p><p class="MsoNormal"> </p><p class="MsoNormal">I am having a problem with using win32com to open an instance of Excel when there is already one open. I would appreciate any help.</p>
<p class="MsoNormal"> </p><p class="MsoNormal">My example goes as follows:</p><p class="MsoNormal">The user has an Excel file (and therefore an EXCEL.exe process) open already.</p><p class="MsoNormal"> </p><p class="MsoNormal">
I do:</p><p class="MsoNormal">xl=win32com.client.DispatchEx("Excel.Application") #create a new instance of EXCEL.exe that will not interfere with the already open process. It remains hidden.</p><p class="MsoNormal">
book=xl.Workbooks.Open(filename) #open the excel file</p><p class="MsoNormal"> </p><p class="MsoNormal">There are now 2 EXCEL.exe’s in the Task Manager.</p><p class="MsoNormal"> </p><p class="MsoNormal">
After this, but before I am done with the Excel file I opened with DispatchEx, the user then closes their open Excel file. There is now only one EXCEL.exe in the task manager, supposedly the one that I opened with DispatchEx.</p>
<p class="MsoNormal"> </p><p class="MsoNormal">I then call:</p><p class="MsoNormal">try:</p><p class="MsoNormal"> book.Close() </p><p class="MsoNormal"> </p><p class="MsoNormal">and receive the error: </p><p class="MsoNormal">
Exception pywintypes.com_error: com_error(-2147418111, 'Call was rejected by callee.', None, None)</p><p class="MsoNormal"> </p><p class="MsoNormal">I then go into an except statement and do:</p><p class="MsoNormal">
nbooks=xl.Workbooks.Count <--FAILS HERE</p><p class="MsoNormal">if not xl.Visible and nbooks==0:</p><p class="MsoNormal"> # if excel is not visible and there are no books open, close excel.</p><p class="MsoNormal">
# Do this because another program may have excel open in the background.</p><p class="MsoNormal"> print "Asking Excel to Quit"</p><p class="MsoNormal"> xl.Quit()</p>
<p class="MsoNormal"> xl=None</p><p class="MsoNormal"> </p><p class="MsoNormal">At “FAILS HERE” I get the same error again: “Exception pywintypes.com_error: com_error(-2147418111, 'Call was rejected by callee.', None, None)” and the EXCEL.exe is left hanging in the process manager, which I have to now manually kill.</p>
<p class="MsoNormal"> </p><p class="MsoNormal">Can anyone help me with this error and how to keep connected to the Excel instance I opened with DispatchEx no matter what the user does with their own open Excel which includes the user closing it?</p>
<p class="MsoNormal"> </p><p class="MsoNormal">Thank you for your assistance.</p><p class="MsoNormal">Mark</p></div></body></html>