Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving word document using code

Status
Not open for further replies.

WMXTeam

Programmer
Oct 5, 2005
41
US
I have a Windows Form Application that creates a Word document and then saves it to a PDF. The application works fine on three workstations. On two workstations, we get the an error at the point the document is saved.

Microsoft.Office.Interop.Word.DocumentClass.SaveAs(Object FileName, Object FileFormat, Object LockComments, Object Password, Object AddToRecentFiles, Object WritePassword, Object ReadOnlyRecommended, Object EmbedTrueTypeFonts, Object SaveNativePictureFormat, Object SaveFormsData, Object SaveAsAOCELetter, Object Encoding, Object InsertLineBreaks, Object AllowSubstitutions, Object LineEnding, Object AddBiDiMarks)

We have confirmed that all the workstations have Microsoft Office 2007 Primary Interop Assemblies and Visual Studio 2010 Tools for Office Runtime installed.

I have tried to compare all the workstations to figure out if there is a missing installation or setting, but have had no success.

Any and all help is greatly appreciated.
 

What is the error? Can you show your code at the point the error occurs?

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Unfortunately, the error routine in the app isn't providing a lot of details. All I get is this:

Err#: 1 TimeStamp: 1/18/2011 2:13:19 PM


etype=U

errSubName=0

curPrgVer=|.0

enumber=0

edesc1= at Microsoft.Office.Interop.Word.DocumentClass.SaveAs(Object FileName, Object FileFormat, Object LockComments, Object Password, Object AddToRecentFiles, Object WritePassword, Object ReadOnlyRecommended, Object EmbedTrueTypeFonts, Object SaveNativePictureFormat, Object SaveFormsData, Object SaveAsAOCELetter, Object Encoding, Object InsertLineBreaks, Object AllowSubstitutions, Object LineEnding, Object AddBiDiMarks)
at kevconApp.frmProjects.docxSearchReplace(String docxName, String newFileNamePdf, String strType)
at kevconApp.frmProjects.btnSubContractOpenPDFs_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message m)
at System.Windows.Forms.ButtonBase.WndProc(Message m)
at System.Windows.Forms.Button.WndProc(Message m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

The code is as follows. The red text is where the error occurs.

Code:
Dim intPages

        Dim docxFileName As String = hPath + "\documents\templates\" + docxName + ".docx"
        newFileNamePdf = hPath + "\documents\pdfs\" + docxName + "_P" + currProjectId.ToString + "_S" + currSubId.ToString + "_ts" + Format(Now(), "yyyyMMddhhmmss") + ".pdf"

        Dim oWord As Word.Application
        oWord = New Word.Application
        Dim oWordDoc As Word.Document
        oWordDoc = oWord.Documents.Open(FileName:=docxFileName, ReadOnly:=True)

        ' 
        ' the word template is updated here
        ' 
        ' save the updated word template as PDF
        [COLOR=red]oWordDoc.SaveAs(newFileNamePdf, Word.WdSaveFormat.wdFormatPDF)[/color]
        oWordDoc.Close(False)
        oWord.Quit(Word.WdSaveOptions.wdDoNotSaveChanges)

        oWord = Nothing

Thanks for taking a look at this. I am stumped as to why it works on some workstations and not others. I have been trying to troubleshoot this in my sleep!
 
Does the user have permission to save the documewnt in whatever folder Word is trying to save the doc?
 
Yes. I double checked and the user has permission to save to that folder. I even did a test by manually opening a word document and saving it to the folder.

Any other ideas?
 
Thank you PRPhx for you help. Your last question put me on a path where I found the answer.

The two workstations I was having an issue with did not have .PDF as a "Save As Type" in Word. I found "2007 Microsoft Office Add-in: Microsoft Save as PDF or XPS" on Micosoft's site, Installed it on both machines and now they work perfectly.

I hope this helps someone else.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top