jplay

Save word doc as PDF

Discussion created by jplay on Feb 1, 2012
Latest reply on Apr 9, 2014 by hillaryhall
I have a problem I am hoping someone can help with. I've written a script that opens a word doc, populates several bookmarks, then saves to PDF and closes. It runs beautifully on my machine, however, I have been unable to get it to work anywhere else. The problem appears to be with the line of code that saves my document to pdf having stripped out this section to test by itself. I have tried both win32client and comtypes. Both give me a similar error, but for the life of me I have no idea what the error means.

the win32client code:

import arcpy, os, string, win32com.client

wdFormatPDF = 17

# Local variables:
OUTPATH = str(arcpy.GetParameterAsText(0))

# Create table of Contents
out_TOC = OUTPATH + r"\Table of Contents.pdf"

#Start Word Application
app = win32com.client.Dispatch("Word.Application")
app.visible = 0
TABLEOFCONTENTS = "C:\\Files\\Table of Contents.dotx"
doc = app.Documents.Open(TABLEOFCONTENTS)

doc.SaveAs(out_TOC, FileFormat=wdFormatPDF)
doc.Close()
app.Quit()

Error:
Traceback (most recent call last):
  File "C:\Program Files\Hurricane Tools\Scripts\CreateAffectedAreaMaps.py", line 17, in <module>
    doc.SaveAs(out_TOC, FileFormat=wdFormatPDF)
  File "<COMObject Open>", line 8, in SaveAs
com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Word', u'Command failed', u'C:\\Program Files\\Microsoft Office\\Office12\\1033\\WDMAIN11.CHM', 36966, ), None)

comtypes code:

import arcpy, os, string, comtypes.client

wdFormatPDF = 17

# Local variables:
OUTPATH = str(arcpy.GetParameterAsText(0))

# Create table of Contents
out_TOC = OUTPATH + r"\Table of Contents.pdf"

#Start Word Application
app = comtypes.client.CreateObject('Word.Application')
app.visible = 0
TABLEOFCONTENTS = "C:\\Files\\Table of Contents.dotx"
doc = app.Documents.Open(TABLEOFCONTENTS)

doc.SaveAs(out_TOC, FileFormat=wdFormatPDF)
doc.Close()
app.Quit()

comtypes error:

Traceback (most recent call last):
  File "C:\Program Files\Hurricane Tools\Scripts\comtypesdoctopdf.py", line 17, in <module>
    doc.SaveAs(out_TOC, FileFormat=wdFormatPDF)
COMError: (-2146824090, None, (u'Command failed', u'Microsoft Word', u'C:\\Program Files\\Microsoft Office\\Office12\\1033\\WDMAIN11.CHM', 36966, None))

Like I said, it runs perfectly on my system, but on an identical system (identical in that we are running the same versions of Arc, python, and word) it does not. Any clue what those errors are?

Outcomes