none
Exceptions when converting docx to pdf programmatically RRS feed

  • Question

  • Ok, so first of all, this works fine on my development machine.  It is having an "Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
     at ConvertDocXToPdf.Program.Main(String[] args)" problem when run during a build.  Does anyone see anything wrong with the code?  Or is there an alternate way I can convert from docx to pdf (needs to work w/ command line since is being used in WorkFlow InvokeProcess).  The docx gets downloaded from a Sharepoint site before this, then the exe below should just save it as pdf.  Here is the code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using Word = Microsoft.Office.Interop.Word;
    using System.Windows.Forms;
     
    namespace ConvertDocXToPdf
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Create a new Microsoft Word application object
                Word._Application wordApp = new Word.Application();
                wordApp.Visible = false;
                wordApp.ScreenUpdating = false;
     
                // Get list of Word files in specified directory from args
                DirectoryInfo dirInfo = new DirectoryInfo(args[0]);
     
                FileInfo[] wordFiles = dirInfo.GetFiles("*.docx");
     
                // C# doesn't have optional arguments so we'll need a dummy value
                object oMissing = System.Reflection.Missing.Value;
                object isVisible = false;
                object readOnly = false;
     
                //Do not save changes (for close and quit)
                object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
     
                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
                    Word._Document oDoc = wordApp.Documents.Open(ref filename, ref oMissing, ref readOnly, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref isVisible,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing);
     
                    // Word.Document oDoc = wordApp.Documents.Open(ref filename, false, false, false, null, null, null, null, null, null, null, false, false, null, true, null);
     
                    // oDoc.Activate();
     
                    //Change from docx to pdf
                    object outputFileName = wordFile.FullName.Replace(".docx"".pdf");
                    object fileFormat = Word.WdSaveFormat.wdFormatPDF;
     
                    // Save document into PDF Format
                    oDoc.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);
     
                    // oDoc.ExportAsFixedFormat(@"c:/stuff/testpdf.pdf", Word.WdExportFormat.wdExportFormatPDF);
     
                    // Close the Word document, but leave the Word application open.
                    oDoc.Close(ref doNotSaveChanges);
                    oDoc = null;
                }
     
                //Close Word app
                ((Word._Application)(wordApp)).Quit(doNotSaveChanges);
                wordApp = null;
            }
        }
    }

    If i comment out the Close() and SaveAs() method, i don't get the exception, so I'm assuming its a problem with Close and Save methods.  Not sure though.  Even on build machine, if I run manually from command line it works fine.


    Frederick Von Stubenstein
    Friday, January 20, 2012 4:39 PM

All replies

  • Hi Frederick,

    I tested your code on my side, but it works well and I do not get the exception.

    What is the version of the word application?

    Have you ever tried to debug and see on which line the error happens? I guess there are some corrupted word files in your folder. Please check them.

    Hope this can give you some hint and just feel free to follow up.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Monday, January 23, 2012 5:21 AM
  • Hi Frederick,

    I tested your code on my side, but it works well and I do not get the exception.

    What is the version of the word application?

    Have you ever tried to debug and see on which line the error happens? I guess there are some corrupted word files in your folder. Please check them.

    Hope this can give you some hint and just feel free to follow up.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us

    Thanks for testing it Bruce.  Like I said, it always runs fine for me UNLESS the program is started via the InvokeProcess activity during a build, then the exception shows up for some reason.  I've tried changing the SaveAs to SavesAs2, ExportAsFixedFormat etc but no matter what, if the program has any of the three of these methods or even the Close() method, the exception occurs during build!?  There is only one Word file in the folder, and it is not corrupt, because after the build i can run the program manually and it creates the PDF just fine.  I don't know how I can Debug since the program is first copied to the build machine and run on it afterwards during build-and as i said it never has the exception when i run it on its own anyway.  BTW I am using Office 2010 Professional.

    Frederick Von Stubenstein

    Monday, January 23, 2012 2:40 PM
  • Frederick, I think it should be the problem that the program is started via InvokeProcess activity during a build.

    >> the exception occurs during build

    Could you give more details about your scenario? What is your build process? Did you pass the correct parameter to the word program? Did you do the server-side automation of Office:http://support.microsoft.com/kb/257757? As far as  I know, Office is not supported to be automated in any server or service applications.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, January 24, 2012 7:09 AM
  • Frederick, I think it should be the problem that the program is started via InvokeProcess activity during a build.

    >> the exception occurs during build

    Could you give more details about your scenario? What is your build process? Did you pass the correct parameter to the word program? Did you do the server-side automation of Office:http://support.microsoft.com/kb/257757? As far as  I know, Office is not supported to be automated in any server or service applications.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Not sure what details you'd need, other than that, yes, i am supplying the correct argument to the program in the InvokeProcess.  I know this because the build log shows it, and I have literally copied and pasted the call and args and run that in command line, which worked fine.  I looked at the link you provided, but I have not seen anywhere where it actually tells you how to convert to PDF using System.IO.Package.IO.  Is there an example anywhere of someone actually accomplishing this?

    Frederick Von Stubenstein
    Tuesday, January 24, 2012 1:12 PM