Controlling SaveAs Word 9.0 COM

Mark Sloboda denchan at earthlink.net
Fri Apr 27 11:32:05 EDT 2001


Hello,
I have to open a Word file which is text, save it as DOS-encoded text
under a different name, and close it.
A similar VB code example is:
myDocname = ActiveDocument.Name
pos = InStr(myDocname, ".")
If pos > 0 Then
myDocname = Left(myDocname, pos -1)
myDocname = myDocname & ".txt"
ActiveDocument.SaveAs FileName:=myDocname, FileFormat:=wdFormatText
End If

In my Python code I have tried several approaches:
from win32com.client import Dispatch

Word = Dispatch("Word.Application")
Word.Visible = 1

WordDoc = Word.Documents.Open("C:\\test.txt")
WordDoc.SaveAs(FileName='C:\\TextTest2.txt',FileFormat='wdFormatDOSText')
WordDoc.Close()
Word.Quit()

It works fine if I leave the FileFormat argument out. However, when I
add the FileFormat argument as shown above, I get

Traceback (most recent call last):
File "d:\python20\pythonwin\pywin\framework\scriptutils.py", line 301,
in RunScript
exec codeObject in __main__.__dict__
File "C:\...\OpenApp.py", line 7, in ?
WordDoc.SaveAs(FileName='C:\\TextTest2.txt',FileFormat='wdFormatDOSText')
File
"d:\python20\win32com\gen_py\00020905-0000-0000-C000-000000000046x0x8x1.py",
line 13959, in SaveAs
return self._oleobj_.InvokeTypes(0x66, LCID, 1, (24, 0), ((16396, 17),
(16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396, 17),
(16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396,
17)),FileName, FileFormat, LockComments, Password, AddToRecentFiles,
WritePassword, ReadOnlyRecommended, EmbedTrueTypeFonts,
SaveNativePictureFormat, SaveFormsData, SaveAsAOCELetter)
com_error: (-2147352571, 'Type mismatch.', None, 2)

I have tried several variants (including specifying a value for all
possible args and at best I get no error, but no DOS encoding either.
Any suggestions would be greatly appreciated.

Mark


 





More information about the Python-list mailing list