none
Problem in opening a docx file using Interop 11.0 RRS feed

  • Question

  • I am trying to open a docx file using interop 11.0. For simple text it works fine. My file contains tables, images, bookmarks etc.. When I try to open it hangs out. Below is the code.

     

    Dim oWordAppl As Word.Application = Nothing

    Dim oDoc As Word.Document =

    Nothing

     

    Dim x As Object = True
     

     

    Dim y As Object = False
     

     

    Dim missing As Object = System.Reflection.Missing.Value

     

    Try

    oWordAppl =

    New Word.ApplicationClass

    oWordAppl.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone

    oWordAppl.ScreenUpdating =

    False

    oWordAppl.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable

    oWordAppl.WordBasic.DisableAutoMacros(1)

    oWordAppl.Visible =

    False

     

    Dim fileConv As Object = Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenFormatAuto

     

    Dim encode As Object = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8

    oDoc = oWordAppl.Documents.Open(Server.MapPath(

    "./TEMP_UPLOAD/"File.docx"), y, x, missing, missing, missing, missing, missing, missing, fileConv, encode, missing, x, missing, x, missing)

     

    oDoc.Close()

    oDoc =

    Nothing

    oWordAppl.Application.Quit()

    oWordAppl =

    Nothing

     

    Catch e As Exception

     

    If Not oDoc Is Nothing Then

    oDoc.Close()

     

    End If

    oDoc =

    Nothing

     

    If Not IsNothing(oWordAppl) Then

    oWordAppl.Application.Quit()

     

    End If

    oWordAppl =

    Nothing

     

    End Try

    Am I missing something in Open() parameters. This is office automation on server side. I have also installed comaptibilty pack on the server. It works fine for simple docx file having only text.

    Please Help

    Thanks in Advance

     

     


    Irfan Tareen
    Tuesday, May 31, 2011 4:45 PM

Answers

  • Ok I got it working I removed all optional parameters when I open. Finally I am relieved. Thanks for the support Cindy. I am all set.

    Thanks

     


    Irfan Tareen
    Friday, June 3, 2011 3:29 PM

All replies

  • Hi Irfan

    Hook up a display to the server and make the Word window visible. Chances are, Word is displaying a message and waiting for user input of some kind.

    Or the document may be damaged.


    Cindy Meister, VSTO/Word MVP
    Tuesday, May 31, 2011 6:09 PM
    Moderator
  • I am getting an error now when I try to open it

    COMException was caught  --  "There was an error opening the file"

    File is not corrupt and also tried one of the solution for this error i.e. registering the Windows\System32\ole32.dll.

    I also have Compatibilty Pack installed. With high priority update.

    No luck yet.

    Thanks

     

     


    Irfan Tareen
    Thursday, June 2, 2011 3:02 PM
  • Hi Ifran

    How can you be sure the file is not corrupt?

    Have you tried opening it with the Word.Application visible so that you can see whether Word has something specific to complain about?


    Cindy Meister, VSTO/Word MVP
    Thursday, June 2, 2011 3:08 PM
    Moderator
  • Hi Cindy

    I am getting this document from user who is having 2007 installed. I tried with different files and same result.

    It works when I rename the doc file to docx and upload it on my local machine. It will open. But when I get get a file from other machine where 2007 is installed I get the error.

    So what might be the difference between these two files.

     


    Irfan Tareen
    Thursday, June 2, 2011 4:31 PM
  • Hi Irfan

    Let me make sure I'm understanding you correctly:

    The file originated in Word 2007, but it has the file ending *.doc? But opens correctly when renamed to *.docx?

    Starting with Word 2007, Word differentiates how it tries to open a Word document depending on the file extension. Those file extensions mean the files have totally different file formats (old Word 97-2003 binary vs. new Open XML). If a file has the wrong extension, Word doesn't try with the other file converter to see whether that will work. It simply won't open the file correctly.

    Given this information and looking back to the code in your original post, I'd also remove the following from the Document.Open method and pass "missing" instead of this object: Dim encode As Object = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8

     


    Cindy Meister, VSTO/Word MVP
    Thursday, June 2, 2011 5:18 PM
    Moderator
  • Since my local machine has only 2003 installed what I did is I renamed the file to docx and did an upload. Once uploaded the Interop tries to open this file and it works fine no issues at all.  But when I get a docx file from other machine (generated from 2007 installed on it) and try to upload and open it using interop I get the error.

     I did removed encoding.

    Also set the Confirm Conversion dialog box to false, NoEncoding dialog box to true, openonrepair to true. All of these have been tested but I am getting the same error.

    Even if I try with all missing parameters I get the same error.

    Also registered mime type for docx on IIS 5.1 server.

    All permissions to the folder and files are taken care ("ASPNET" and "NETWORK SERVICE" full control).

    Registered the ole32.dll (Which might have been the cause of this error) No luck here.

    My machine now has office 2003 with SP1 , Compatiblity Pack with All latest updates and SP's, High Priroirty Updates for Compatability Pack, OS is WINDOWS XP  

    Also checked for WebClient service which runs under windows services for create accces and modify internet files. It has a status of Started and Automatic (normal functioning).


    Irfan Tareen

    Thursday, June 2, 2011 5:34 PM
  • Ok I got it partially working with this links answer

    http://forums.asp.net/t/1655704.aspx/1?Word+Automation+and+IIS+issue

    But now I am loosing Images from the file when I save it as doc. Any idea what type of encoding I need to use while opening docx and saving doc file?

    I am loosing Header and Footer along with an image.

    Thanks


    Irfan Tareen
    Friday, June 3, 2011 1:20 PM
  • Ok I got it working I removed all optional parameters when I open. Finally I am relieved. Thanks for the support Cindy. I am all set.

    Thanks

     


    Irfan Tareen
    Friday, June 3, 2011 3:29 PM
  • Hi Irfan

    Glad you tracked it all down :-) Since I couldn't sit there and look over your shoulder, best I could do was try to point you in the right direction and hope you'd be able to do the real detective work, which you did!


    Cindy Meister, VSTO/Word MVP
    Friday, June 3, 2011 3:40 PM
    Moderator