简体   繁体   中英

Word Interop acting differently on Save/SaveAs2 on different machines

I have the following piece of code to transform a docx file to a pdf

MSWord.Application app = new MSWord.Application();
MSWord.Document doc = app.Documents.Open("C:/temp/mydoc.docx");
doc.Activate();
doc.Save("c:/temp/mydoc.pdf", WdSaveFormat.wdFormatPDF); //also tried with doc.SaveAs2(...)
((MSWord._Document)doc).Close(WdSaveOptions.wdDoNotSaveChanges);

This works perfectly on my pc (Win7 64b with Office 2013) but when I run it on the clients machine (Win8 64b with Office 2013), it ALWAYS displays a filedialog when executing the doc.Save() or doc.SaveAs2() calls. The filedialog also defaults to the Docx option instead of the PDF option causing the user to have to manually select it.

When clicking OK in the filedialog, the PDF is created correctly but then, still in the doc.Save() or doc.SaveAs2() method, an error occurs with message "Command failed". If I wrap my code in try-catch and execute the doc.Close() method in the finally block I can bypass the error but that's not a real solution (and the filedialog is still there).

Does anybody have an idea what might cause the filedialog to popup? Is it Win8 behaviour?

You need to use the ExportAsFixedFormat method of the Document class instead. It saves a document as PDF or XPS format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM