none
The remote procedure call failed. (Exception from HRESULT: 0x800706BE) while trying to use WORD 2010 in VB.NET. RRS feed

  • Question

  • Hi,

    I am getting the The remote procedure call failed. (Exception from HRESULT: 0x800706BE) while trying to use WORD 2010 in VB.NET. The functionality we are trying to achieve is creating a single word file from 9 different files (1.docx; 2.doc etc). This is occuring only on Windows 7 machine. Below is the code snippet i am using. I tried to debug the issue but the error is coming up from docO.Close(False) at different instances like sometimes in loop 5, sometimes in loop 8 etc. The version of interop.word used is 14.0.0.0

    Dim oApp As Word.Application
            Dim docI As Word.Document
            Dim docO As Word.Document
            Dim arrayCount As Integer
            Dim txtFilename As String
            txtFilename = Mid(FileOut, 9)

    oApp = CreateObject("Word.Application")

            docI = oApp.Documents.Add()
            docI.Select()

            Dim dRan As Word.Range = docI.Range(Start:=0, End:=0)

            For Count = 1 To I - 1
                    docO = oApp.Documents.Open(i & ".docx")
                    docO.Content.Copy()
                    docO = oApp.ActiveDocument
                    dRan = docI.Content 'reference to all text in doc
                    dRan.Collapse(Word.WdCollapseDirection.wdCollapseEnd) 'move to a point after all the text
                    dRan.Paste() 'paste at the end of the doc
                    My.Computer.Clipboard.Clear() 'save memory, speed things up
                    If arrayCount <> I - 1 Then
                        dRan = docI.Content
                        dRan.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
                        docI.Sections.Add()
                    End If
                    dRan = Nothing
                    docO.Close(False)
                    docO = Nothing
                Next
    Wednesday, April 23, 2014 9:12 PM

Answers

  • Hi Avik

    Headers and footers are specific to SECTIONS within a document. Sections are created by inserting a SECTION BREAK. In the UI you find the command in Page Layout/Page Setup/Breaks. In the object model: Range.InsertBreak([WdBreakType])

    There are various kinds of breaks. For your requirement best is probably the Next Page type of section break.

    Once the section break has been inserted, you need to UNLINK it from the preceding (and possibly following) sections so that the headers/footers remain unique to that section.

    In the UI you'll find the command in the context-sensitive Header & Footer Tools Design tab, in the Navigation tab. In the object model: HeaderFooterObject.LinkToPrevious = False

    I mention the UI commands because it's always a good idea to first understand how Word works before trying to use the object model...


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Avik Kesh Monday, April 28, 2014 5:54 PM
    Monday, April 28, 2014 3:47 PM
    Moderator
  • Record performing the action in a macro in order to get the basic syntax.

    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Avik Kesh Saturday, April 26, 2014 4:14 PM
    Thursday, April 24, 2014 6:45 PM
    Moderator

All replies

  • Hi Avek

    Why would you open each document then copy/paste? Why not use Insert/File?

    You might try making oApp.Visible = True so that you can see whether Word is displaying a dialog box and waiting for user input. 


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, April 24, 2014 4:38 PM
    Moderator
  • Hi Cindy

    I am also working with Avik on this. Actually, I am not supposed to use user input as my client wants this functionality only. Do you have any work around maintaining the same above mentioned functionality? Can you provide the work around code replicating the same functionality above but not raising the same error?

    Thanks

    Shaleen

    Thursday, April 24, 2014 5:32 PM
  • Hi Shaleen

    I wasn't suggesting user input. Making the app window visible in order to see whether Word is displaying a message, and what the message is, is a trouble-shooting step to find out what your code needs to be aware of.

    You also need to realize that the Office applications were never designed to be run without user interaction. There's no way to suppress all messages, you can only try to code in a way that will avoid them. I did make one suggestion: use InsertFile instead of copy/paste (and opening/closing the files you want to insert).


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, April 24, 2014 6:06 PM
    Moderator
  • Thanks Cindy. Will try the InsertFile Option and let u know.
    Thursday, April 24, 2014 6:20 PM
  • Could you help us out with a sample code using InsertFile with respect to our requirement? Please help.
    Thursday, April 24, 2014 6:35 PM
  • Record performing the action in a macro in order to get the basic syntax.

    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Avik Kesh Saturday, April 26, 2014 4:14 PM
    Thursday, April 24, 2014 6:45 PM
    Moderator
  • Thanks Cindy, The insert solved by basic problem.

    Now I have a small thing that I am finding difficult to fix. Any suggestions. The doc is now getting created using the insert instead of the copy/paste.

    My 3rd word document has a header/footer. When i try to create the single document from the 3 docs, the header/footer of the 3rd doc gets added to the pages of the 1st 2 docs which didnot have a header/footer. Any suggestion on how to fix this?

    Monday, April 28, 2014 3:24 PM
  • Hi Avik

    Headers and footers are specific to SECTIONS within a document. Sections are created by inserting a SECTION BREAK. In the UI you find the command in Page Layout/Page Setup/Breaks. In the object model: Range.InsertBreak([WdBreakType])

    There are various kinds of breaks. For your requirement best is probably the Next Page type of section break.

    Once the section break has been inserted, you need to UNLINK it from the preceding (and possibly following) sections so that the headers/footers remain unique to that section.

    In the UI you'll find the command in the context-sensitive Header & Footer Tools Design tab, in the Navigation tab. In the object model: HeaderFooterObject.LinkToPrevious = False

    I mention the UI commands because it's always a good idea to first understand how Word works before trying to use the object model...


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Avik Kesh Monday, April 28, 2014 5:54 PM
    Monday, April 28, 2014 3:47 PM
    Moderator
  • Thanks Cindy for the response. It resolved most of my problems.

    Now the only thing left for me is to have the formatting of the docs 1, 2, & 3 that i am copying over to the new doc created. It is taking up the margins and other formatting from the default word formats set up. I am trying to get it take up from the formatting set up in the original word document.

    Monday, April 28, 2014 5:57 PM
  • Hi Avik

    It depends very much on WHICH formatting you have in mind. Some of the formatting is controlled via the Section Breaks, some by styles. And then there's formatting the user applies directly to text (from buttons rather than using a style). The approaches required depend very much on what you have in mind.

    Margins "belong" to sections. But when insert a document the last section is "snipped off" before insertion, thus losing any section-relevant information. The only way to retain it is to insert an additional section break, at the very end of the document you're inserting.

    The Master/Subdocument feature can do this for you. But it's not something you want to use if the resulting document should be edited (instability)...


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, April 29, 2014 5:29 PM
    Moderator