Note that there are some explanatory texts on larger screens.

plurals
  1. POWord to PDF conversion fails on machines other than my development PC
    primarykey
    data
    text
    <p>I am testing converting Word documents to PDFs programmatically from Winforms apps. I wrote the following code which works fine on my machine. The app consists of a form with 2 buttons and a text box. Clicking the first button opens a dialog box to allow the user to navigate to a folder. The folder address is stored in the text box. Then the second button takes each Word document from the specified folder and creates a PDF with the same name for each.</p> <pre><code>namespace PDFTesting { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnFind_Click(object sender, EventArgs e) { try { FolderBrowserDialog folder = new FolderBrowserDialog(); DialogResult result = folder.ShowDialog(); if (result == DialogResult.OK) { string[] files = Directory.GetFiles(folder.SelectedPath); txtLocation.Text = folder.SelectedPath.ToString(); } else { txtLocation.Text = null; } } catch (Exception eX) { throw new Exception("cDocument: Error atempting to GetPath()" + Environment.NewLine + eX.Message); } } private void btnConvert_Click(object sender, EventArgs e) { // Create a new Microsoft Word application object Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); // C# doesn't have optional arguments so we'll need a dummy value object oMissing = System.Reflection.Missing.Value; // Get list of Word files in specified directory string docPath = txtLocation.Text; DirectoryInfo dirInfo = new DirectoryInfo(docPath); FileInfo[] wordFiles = dirInfo.GetFiles("*.doc"); word.Visible = false; word.ScreenUpdating = false; foreach (FileInfo wordFile in wordFiles) { // Cast as Object for word Open method Object filename = (Object)wordFile.FullName; // Use the dummy value as a placeholder for optional arguments Document doc = word.Documents.Open(ref filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); doc.Activate(); object outputFileName = wordFile.FullName.Replace(".docx", ".pdf"); object fileFormat = WdSaveFormat.wdFormatPDF; // Save document into PDF Format doc.SaveAs(ref outputFileName, ref fileFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); // Close the Word document, but leave the Word application open. // doc has to be cast to type _Document so that it will find the // correct Close method. object saveChanges = WdSaveOptions.wdDoNotSaveChanges; ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing); doc = null; } // word has to be cast to type _Application so that it will find // the correct Quit method. ((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing); word = null; } } } </code></pre> <p>This works exactly as intended on my development PC, but when I build the solution and distribute to another machine (running the same version of Windows 7, 64 bit) and with Word installed, I get the following error -</p> <p><img src="https://i.stack.imgur.com/2ZqMx.png" alt="Error"></p> <p>Does anyone have any ideas why my mini PDF app does not work on the alternative machine?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload