Opening file in Excel

Lemniscate d_blade8 at hotmail.com
Wed Aug 21 15:53:44 EDT 2002


"dsavitsk" <dsavitsk at e-coli.net> wrote in message news:<buT79.3072$yt3.1265418 at newssrv26.news.prodigy.com>...
> have you tried using the whole file path? i.e. -> 'C:\\my\\path\\mytest.xls'
> also, instead of throwing the file name at XL, see if the file exists first.
> >>> import os.path
> >>> if os.path.exists(filename):
> ...    lf.xlBook = self.xlApp.Workbooks.Open(filename)
> 
> -d
> 

Sorry for the delayed response, an unexpected twist at work has kept
me away from the computer.

Yes, my original message included the line "Using the full path of the
file results in the
exact same thing."  Here is a complete interactive session to show
what I mean...

PythonWin 2.2.1 (#34, Apr  9 2002, 19:34:33) [MSC 32 bit (Intel)] on
win32.
Portions Copyright 1994-2001 Mark Hammond (mhammond at skippinet.com.au)
- see 'Help/About PythonWin' for further copyright information.
>>> import win32com.client
>>> import tkFileDialog
>>> myFile = tkFileDialog.askopenfilename()
>>> myFile # full path is being used
'C:/Documents and Settings/dblade/Desktop/Current TB.xls'
>>> xlApp = win32com.client.Dispatch("Excel.Application")
>>> xlApp.Visible = 1 # so I can see it
>>> xlApp.Workbooks.Open(myFile)
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "win32com\gen_py\00020813-0000-0000-C000-000000000046x0x1x3.py",
line 14716, in Open
    ret = self._oleobj_.InvokeTypes(0x2aa, LCID, 1, (9, 0), ((8, 1),
(12, 17), (12, 17), (12, 17), (12, 17), (12, 17), (12, 17), (12, 17),
(12, 17), (12, 17), (12, 17), (12, 17), (12, 17)),Filename,
UpdateLinks, ReadOnly, Format, Password, WriteResPassword,
IgnoreReadOnlyRecommended, Origin, Delimiter, Editable, Notify,
Converter, AddToMru)
com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel',
"'Current TB.xls' could not be found. Check the spelling of the file
name, and verify that the file location is correct.\n\nIf you are
trying to open the file from your list of most recently used files on
the File menu, make sure that the file has not been renamed, moved, or
deleted.", 'C:\\Program Files\\Microsoft
Office\\Office\\1033\\xlmain9.chm', 0, -2146827284), None)
>>> xlApp.Workbooks.Open(Filename=myFile) # try some keyword arguments
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "win32com\gen_py\00020813-0000-0000-C000-000000000046x0x1x3.py",
line 14716, in Open
    ret = self._oleobj_.InvokeTypes(0x2aa, LCID, 1, (9, 0), ((8, 1),
(12, 17), (12, 17), (12, 17), (12, 17), (12, 17), (12, 17), (12, 17),
(12, 17), (12, 17), (12, 17), (12, 17), (12, 17)),Filename,
UpdateLinks, ReadOnly, Format, Password, WriteResPassword,
IgnoreReadOnlyRecommended, Origin, Delimiter, Editable, Notify,
Converter, AddToMru)
com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel',
"'Current TB.xls' could not be found. Check the spelling of the file
name, and verify that the file location is correct.\n\nIf you are
trying to open the file from your list of most recently used files on
the File menu, make sure that the file has not been renamed, moved, or
deleted.", 'C:\\Program Files\\Microsoft
Office\\Office\\1033\\xlmain9.chm', 0, -2146827284), None)
>>> xlApp.Workbooks.Open(FileName=myFile) # and again
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
TypeError: Open() got an unexpected keyword argument 'FileName'
>>> # Open the file through excel -> File -> Open
>>> # Now close the file in excel
>>> xlApp.Workbooks.Open(myFile)
<win32com.gen_py.Microsoft Excel 9.0 Object Library.Workbook>
>>> # now it works, why???

Thanks



More information about the Python-list mailing list