none
Word 2010 Addin fails with RPC_E_DISCONNECTED RRS feed

  • Question

  • Hi

    I've created a word 2010 VBA addin creating a document with 8 linked Excel tables. From the word addin I run Excel, load a workbook containing 8 worksheets, copy the used range and paste special into the word document. 4 times it works perfectly and during or after the 5th paste special I get an RPC_E_DISCONNECTED exception.

    The Addin once worked with Office 2007.

    Any ideas what is wrong ?

    thanks for your help

    Patrik

     

    Friday, April 8, 2011 7:42 AM

Answers

  • And my last question?

    Probably, Office 2010 is handling resources differently when working with OLE objects. If you do the copy/paste stuff manually, do you eventually run into the same issues?

    If you press Alt+F9 to reveal field codes do you see LINK fields in your document in place of the workbook objects? An alternate approach to try would be to generate these fields directly.

    Another thing to try would be to record Insert Object, From File, with a link then adjust that code. Avoiding the Clipboard could possibly reduce the "footprint".


    Cindy Meister, VSTO/Word MVP
    Friday, April 8, 2011 9:57 AM
    Moderator
  • Hi Cindy

    following code runs without any problem. Thanks for your help.

    Regards

    Patrik

        Dim xlAppl As Excel.Application = New Excel.Application
    
        xlAppl.Visible = True
    
        Dim xlWb As Excel.Workbook = xlAppl.Workbooks.Open(Filename:="C:\Users\Patrik.Cueni\Documents\Visual Studio 2010\Projects\TestLinkedOLE\TestLinkedOLE\Test.xlsx")
        Dim xlWs As Excel.Worksheet
        Dim xlWsRange As Excel.Range
        Dim doc As Word.Document = Application.Documents.Add
        Dim inlineShape As Word.InlineShape
        Dim filename As String
    
        For Each xlWs In xlWb.Worksheets
    
          xlWs.Activate()
          xlWsRange = xlWs.UsedRange
          filename = xlWb.Path & "\" & xlWb.Name & "!" & xlWs.Name & "!" & xlWsRange.AddressLocal(ReferenceStyle:=Excel.XlReferenceStyle.xlR1C1)
    
          inlineShape = doc.ActiveWindow.Selection.InlineShapes.AddOLEObject(ClassType:="Excel.Sheet.12", _
                                      FileName:=filename, _
                                      LinkToFile:=True, _
                                      DisplayAsIcon:=False)
    
    
        Next
    
        doc.SaveAs2(FileName:="Test.doc")
        doc.Close()
    
    
    Monday, April 11, 2011 11:29 AM

All replies

  • Hi Patrik

    As this is VBA I'm moving your question to the Word Developer forum. VSTO is a special .NET technology, so people think along different lines here than with VBA :-)

    It would help if you could show us the parts of your code that:

    - initiate the Excel application
    - finish the work with the Excel application (quit, clean-up, etc.)

    Please check in the Windows task manager after running the macro a couple of times whether (and how many) instances of Excel.exe are running.

    Also, please describe how/if behavior is different if you 1. close then re-open the Word document and 2. Quit, restart Word. Does either (or both) of those actions change anything?


    Cindy Meister, VSTO/Word MVP
    Friday, April 8, 2011 8:36 AM
    Moderator
  • Hi Cindy

    here are the code parts:

    1.initiate Excel

    Dim ExcelApplication As Excel.Application = Nothing
    
    If ExcelApplication Is Nothing Then ExcelApplication = New Excel.Application
    
    ExcelApplication.Visible = debug
    
    ExcelApplication.UserControl = False
    
    

    2.finish Excel

     

    ExcelApplication.ActiveWorkbook.Close(false)
    
    ExcelApplication.Quit
    
    ExcelApplication = nothing
    

     

    3. following is the procedure to copy paste special the excel ranges into the word document.

    rng equals to worksheet.used_range

    bookmark is the name of an existing bookmark in the word template

    
      Private Sub InsertTable(ByVal rng As Excel.Range, ByRef doc As Word.Document, ByVal bookmark As String)
    
        Try
          rng.Application.CutCopyMode = False
          rng.Select()
          rng.Copy()
    
          doc.Activate()
          Dim bm As Word.Range
          Try
            bm = doc.GoTo(What:=Word.WdGoToItem.wdGoToBookmark, Name:=bookmark)
            doc.Bookmarks.DefaultSorting = Word.WdBookmarkSortBy.wdSortByLocation
            doc.Bookmarks.ShowHidden = False
            bm.Select()
          Catch ex As Exception
            log.SaveExceptionInfo(ex, True)
            Return
          End Try
          doc.ActiveWindow.Selection.PasteSpecial(Link:=True, _
                              DataType:=Word.WdPasteDataType.wdPasteOLEObject, _
                              Placement:=Word.WdOLEPlacement.wdInLine, _
                              DisplayAsIcon:=False)
    
        Catch ex As Exception
          log.SaveExceptionInfo(ex, True)
        End Try
      End Sub
    
     

    In Task manager there is only one Excel.exe visible.

    Regards

    Patrik

    Friday, April 8, 2011 9:28 AM
  • And my last question?

    Probably, Office 2010 is handling resources differently when working with OLE objects. If you do the copy/paste stuff manually, do you eventually run into the same issues?

    If you press Alt+F9 to reveal field codes do you see LINK fields in your document in place of the workbook objects? An alternate approach to try would be to generate these fields directly.

    Another thing to try would be to record Insert Object, From File, with a link then adjust that code. Avoiding the Clipboard could possibly reduce the "footprint".


    Cindy Meister, VSTO/Word MVP
    Friday, April 8, 2011 9:57 AM
    Moderator
  • Hello Cindy

    I can create all of the 8 OLE objects manually without problems. I already checked the LINK fields and they are also ok. I also copy pasted a LINK field and modified the source path, etc. This worked also. Do you know how I can create these LINK fields ?

    Patrik

    Friday, April 8, 2011 12:23 PM
  • Hi Patrik

    I don't have a lot of time at the moment, but perhaps a description will be all you need...

    Document.Fields.Add is the method you need. For the first parameter, the Range where the field should be added. For Type, pass Word.WdFieldType.wdFieldEmpty. For Text, pass the complete contents that should appear between the field code brackets (for example "LINK Excel.12.Worksheet 'C:\\Test\\MyWorkbook.xlsx' \a \f 0 \p" ) as a string value (note: this might take some trail-and-error, what with quotes and all). PreserveFormatting, set that to false.


    Cindy Meister, VSTO/Word MVP
    Friday, April 8, 2011 3:07 PM
    Moderator
  • Hi Cindy

    following code runs without any problem. Thanks for your help.

    Regards

    Patrik

        Dim xlAppl As Excel.Application = New Excel.Application
    
        xlAppl.Visible = True
    
        Dim xlWb As Excel.Workbook = xlAppl.Workbooks.Open(Filename:="C:\Users\Patrik.Cueni\Documents\Visual Studio 2010\Projects\TestLinkedOLE\TestLinkedOLE\Test.xlsx")
        Dim xlWs As Excel.Worksheet
        Dim xlWsRange As Excel.Range
        Dim doc As Word.Document = Application.Documents.Add
        Dim inlineShape As Word.InlineShape
        Dim filename As String
    
        For Each xlWs In xlWb.Worksheets
    
          xlWs.Activate()
          xlWsRange = xlWs.UsedRange
          filename = xlWb.Path & "\" & xlWb.Name & "!" & xlWs.Name & "!" & xlWsRange.AddressLocal(ReferenceStyle:=Excel.XlReferenceStyle.xlR1C1)
    
          inlineShape = doc.ActiveWindow.Selection.InlineShapes.AddOLEObject(ClassType:="Excel.Sheet.12", _
                                      FileName:=filename, _
                                      LinkToFile:=True, _
                                      DisplayAsIcon:=False)
    
    
        Next
    
        doc.SaveAs2(FileName:="Test.doc")
        doc.Close()
    
    
    Monday, April 11, 2011 11:29 AM