none
AccessViolationException while accessing the WordOpenXML property RRS feed

  • Question

  • Hello,

    I have created an application which generates the Reports in Word formats.

    I have office 2007 and VS2010 installed on my development machine.

    I have created a word template to generate the reports. Everything is working on my development machine. But it throws an error on server with word 2003 installed.

    Below is the code:

    Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
    
    Microsoft.Office.Interop.Word.Document doc = null;
    
    wordApp.Visible = false;
    
                    //open the word document
                    doc = 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 isVisible, ref missing, ref missing, ref missing, ref missing);
    
                    doc.Activate();
    
    //Above line of code get executed successfully on server with word 2003 installed - It throws an error while accessing the doc.WordOpenXML property
    
    Match match = Regex.Match(doc.WordOpenXML, @"%~([A-Za-z_\[\]’.%\-?&:;=#'\s\w/]+)~%", RegexOptions.IgnorePatternWhitespace);

    Application throws AccessViolationException while trying to access doc.WordOpenXML property on the server.

    I think this is because of the older version of word(2003) installed on server.

    I can not install word 2007 on server.

    Is there any way to make it work on all the machine. What are the dependency needs to be included in application to make it work on all the machine.


    Monday, September 24, 2012 4:04 AM

Answers

  • Hi KP SE

    Word 2003 is, indeed, the issue.

    That version has no WordOpenXML property because Word Open XML did not even exist back then.

    Word 2003 did have a slightly different XML vocabulary that can be accessed using the Range.XML property. This is the original WordProcessingML that was completely "flat file". Word 2007 and 2010 still support it, but you have to be aware that WordProcessingML can only work with things that were part of Word 2003. So if your reports use, for example, content controls or the new Charts you cannot use WordProcessingML.

    Why would you automate (use the "interop") on the server, anyway? Why not work completely with Word Open XML and the OPen XML SDK, which was designed expressly for generating Word documents server-side?


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, September 24, 2012 9:32 AM
    Moderator
  • Word 2003 cannot save in PDF format. That capability was introduced in Word 2007, and then in the original release it was only possible using an Add-in.

    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Kalpen Patel Tuesday, September 25, 2012 6:13 AM
    Tuesday, September 25, 2012 4:36 AM
    Moderator

All replies

  • Hi KP SE

    Word 2003 is, indeed, the issue.

    That version has no WordOpenXML property because Word Open XML did not even exist back then.

    Word 2003 did have a slightly different XML vocabulary that can be accessed using the Range.XML property. This is the original WordProcessingML that was completely "flat file". Word 2007 and 2010 still support it, but you have to be aware that WordProcessingML can only work with things that were part of Word 2003. So if your reports use, for example, content controls or the new Charts you cannot use WordProcessingML.

    Why would you automate (use the "interop") on the server, anyway? Why not work completely with Word Open XML and the OPen XML SDK, which was designed expressly for generating Word documents server-side?


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, September 24, 2012 9:32 AM
    Moderator
  • Hi Cindy,

    Thank you for your reply.

    I have wrriten foreach on Storyboard and it accomplish my requirement. It is working for Word 2003/07/10.

    I can use Open XML SDK but not for now.

    The only issue I am facing now is to Save document in PDF format.

    I have Windows Server 2008 R2 OS on the server. and Word 2003 installed in it.

    I am trying to save document in the PDF format. 

    WdSaveFormat.wdFormatPDF

    But it throws "value out of range" exception. shown in below image.

    


    I have passed below arguments in SaveAs method.

    object fileFormat = WdSaveFormat.wdFormatPDF;doc.SaveAs(ref SaveAsPDF,                        ref fileFormat, 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, ref missing);

    Monday, September 24, 2012 10:37 AM
  • Word 2003 cannot save in PDF format. That capability was introduced in Word 2007, and then in the original release it was only possible using an Add-in.

    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Kalpen Patel Tuesday, September 25, 2012 6:13 AM
    Tuesday, September 25, 2012 4:36 AM
    Moderator