C# .NET framework using Interop.Word and getting error 'Value cannot be null. Parameter name: pUnk'

Viewed 46

I'm using Interop.Word to create a .docx file add some tables, fill it with data and then exporting it to a .pdf.

I keep getting this error:

System.ArgumentNullException: 'Value cannot be null.
Parameter name: pUnk'

All the online discussions regarding this issue are talking about TFS and VS version which I'm not really sure it is related to my problem at all.

I have several try catch in the code and I even commented the error. Yet, I can't put my hand on the reason for the exception!!

        //declare word app
        word.Application wordapp = new word.Application();
        object missing = Missing.Value;//NA Values
        word.Document myworddoc = null;

        if (File.Exists((string)filename))//check if it exists
        {
            object readOnly = false;//word settings
            object isvisible = false;
            wordapp.Visible = false;

            //word doc
            myworddoc = wordapp.Documents.Open(ref filename, ref missing, ref readOnly, ref missing, ref missing, ref missing
                , ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing
                , ref missing, ref missing);

            //open the word
            myworddoc.Activate();

            //replace key words
            importdata(wordapp,myworddoc);

            //export the file
            try
            {
                myworddoc.ExportAsFixedFormat(saveas.ToString(), word.WdExportFormat.wdExportFormatPDF);
            }
            catch (Exception)
            {
                XtraMessageBox.Show("Please close the opened File!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            // close the doc
            // closeWord();
            try
            {
                myworddoc.Close();
                wordapp.Quit();
            }
            catch (Exception)
            {
                closeWord();
            }

            XtraMessageBox.Show("Export Complete!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        else
        {
            XtraMessageBox.Show("Export Failed!", "Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

I get the exception immediately after the export is done successfully, no matter what the following line is.

1 Answers

It is not recommended and not supported to automate Office applications from a service app, here is what MS states for such scenarious:

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.

Read more about that in the Considerations for server-side Automation of Office article.

As a possible workaround, if you deal with open XML documents, you may consider using the Open XML SDK, see Welcome to the Open XML SDK 2.5 for Office for more information.

Related