0

Background

I have a SharePoint 2013 (SP2013) environment (env) where the Word Automation Services (WAS) has stopped working, so my application has be failing to convert XML documents to PDF.

Previous Status

I use OpenXML SDK to convert the XML InfoPath document to Word document (works as expected). Then convert the Word document to PDF using WAS on SP.

Current Status

The WAS stopped working. My application converts the XML to Word but never converts to PDF. As a stop gap I am using a C# code snippet (shown below) to try converting to PDF but I keep getting the error "Object reference not set to an instance of an object."

...
using Word = Microsoft.Office.Interop.Word;
...

string fileName = generatedDoc; //generatedDoc is Word doc converted from XML
string pdfFileName = fileName.Replace("docx", "pdf");
string sourceUrl = siteUrl + "/DocLibMemo/" + fileName;
string destUrl = siteUrl + "/ApprovedMemoPDF/" + pdfFileName;

Convert(sourceUrl, destUrl, Word.WdSaveFormat.wdFormatPDF);

public static void Convert(string input, string output, Word.WdSaveFormat format)
{
    // Create an instance of Word.exe
    Word._Application oWord = new Word.Application();
            
    // Make this instance of word invisible (Can still see it in the taskmgr).
    oWord.Visible = false;
    oWord.ScreenUpdating = false;
    
    // Interop requires objects.
    object oMissing = System.Reflection.Missing.Value;
    object isVisible = false;
    object readOnly = false;
    object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
    object oInput = input;
    object oOutput = output;
    object oFormat = format;
    
    // Load a document into our instance of word.exe
    Word._Document oDoc = oWord.Documents.Open(
        ref oInput, 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);

    // Make this document the active document.
    oDoc.Activate(); // The execption is hit here

    // Save this document using Word
    oDoc.SaveAs(ref oOutput, ref oFormat, 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);

    // Always close Word.exe.
    oWord.Quit(ref oMissing, ref oMissing, ref doNotSaveChanges);
}

The above snippet worked when I tested it using a console application and the Word files was on my C drive. However now that the Word files are on a SP library It doesn't convert to PDF.

Community
  • 1
  • 1
ArthurEzenwanne
  • 176
  • 2
  • 10
  • Are you certain that the location and filename of the Word document are correct? – Andy G Mar 12 '19 at 10:30
  • Also, temporarily make the application and document visible so that you can see any warnings, and confirm that it opens the document. – Andy G Mar 12 '19 at 10:33
  • 1
    Your code isn't using the OpenXML SDK or the WAS services at all. It's starting a new Word instance, something that should *never* be done on a server. First, because it will leave a lot of open Word instances behind if the Word instance isn't closed properly. In this case, there's no error handling so any errors will leave Word running. Second, because you'd require a Word license for *every* end user that called that code. – Panagiotis Kanavos Mar 12 '19 at 11:03
  • 1
    In fact, you code doesn't show anything related to the question. No attempt to use OpenXML or WAS. The NRE could be thrown simply because there's no Word installed on the server – Panagiotis Kanavos Mar 12 '19 at 11:07
  • @AndyG, yes the location and filename of the Word doc is correct. I have also made the Word application visible. However, still getting same error. – ArthurEzenwanne Mar 12 '19 at 11:25
  • @PanagiotisKanavos, I represented the code part with OpenXML and other try catch blocks with ```...```. Didn't want to drop blocks and blocks of code here. WAS was ***used***. It is no longer working. Concerning licences, no problem our entrprise have those for all users. Word is also installed on the server. Also this is just a stop gap until we get WAS back up. – ArthurEzenwanne Mar 12 '19 at 11:30
  • In that case you should fix whatever broke WAS, not try something that's guaranteed to break down. If you want to use this code, *debug* it to find which object is null. Add proper exception handling so you can get the error line and full call stack. Right now this could easily be closed as a duplicate of [What is a NullReferenceException and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it). *Check* the returned objects before using them – Panagiotis Kanavos Mar 12 '19 at 11:37

1 Answers1

0

I have encountered with the same problem. SharePoint library is a network drive which doesn't have a physical location. The files of SharePoint are indeed saved in database, so that is the reason why we cannot write and save files in SharePoint library. MS Office has a different approach of saving files to SP Library, it actually upload files instead of saving them directly.

Solution for the problem is to take a local copy of the Word file and make changes to the local copy and upload(i.e Copy) it to the same location in the SharePoint library.

Hope this helps.

Thanks.

  • Did you start Word on the server? Did you get a NullReferenceException? And no, there's no need to copy locally. Word *can* save to any network drive anyway, but knows about SharePoint and can take advantage of other APIs. There's no need to use *Word* to convert documents though, especially not on a server – Panagiotis Kanavos Mar 12 '19 at 11:06