none
Word 2010 VSTO issue RRS feed

  • Question

  • Hi,

     

    I have made a custom ribbon for word 2010 using VSTO and I'm having trouble with the code of one of my buttons.

    I'm trying to insert a predefined control (e.g. Link label) when I click my custom button.

    I am able to get the control to insert at the range of the document and storing its X and Y coordinates inside database with the help of below code snippets.

     

       Public Sub InsertControlAtSelection(ByRef c As Control, ByVal name As String)

            Dim horizontal As Object = False

            Dim vertical As Object = True

            c.Name = name

     

       Dim selRange As Word.Range = VstoDocument.Application.Selection.Range   

            'Floating

           ‘ VstoDocument.Controls.AddControl(c, Fix(selRange.Start), Fix(selRange.End), VstoDocument.Application.PixelsToPoints(c.Width, horizontal), VstoDocument.Application.PixelsToPoints(c.Height, vertical), name)

            'Inline

           VstoDocument.Controls.AddControl(c, selRange, Fix(c.Width), Fix(c.Height), name)

       End Sub

     

    We tried following two different options

    Inline

    Floating.

     

    The above functions works fine when we are adding controls initially. They are adding control at proper position and giving proper range start and end values which we are storing inside database.

    When we are repopulating the controls again into the word document by using stored X and Y co-ordinates from database and passing it to below function then sometimes we are getting an exception.

     

    Public Sub InsertControl(ByRef c As Control, ByVal name As String, ByVal xPos As Integer, ByVal yPos As Integer, ByVal Width As Single, ByVal height As Single)

            Dim horizontal As Object = False

            Dim vertical As Object = True

           c.Name = name

     

            Try

                Dim selRange As Word.Range

                selRange = VstoDocument.Range(0, 0)

                selRange.Start = xPos

                selRange.End = yPos

                selRange.Select()

     

                'Inline

                VstoDocument.Controls.AddControl(c, selRange, Fix(Width), Fix(height), name)

     

                'Floating

                'VstoDocument.Controls.AddControl(c, VstoDocument.Application.PixelsToPoints(xPos, horizontal), VstoDocument.Application.PixelsToPoints(yPos, vertical), Width, height, name)

     

           Catch ex As Exception

           End Try   

        End Sub

     

    Exception:-

    {System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component." & vbCrLf & "   at Microsoft.Office.Interop.Word.InlineShapes.AddOLEControl(Object& ClassType, Object& Range)" & vbCrLf & "   at Microsoft.Office.Tools.Word.ControlCollectionImpl.CreateInlineWrapperAndGetCookie(String name, Range range, InlineShape& outInlineShape)" & vbCrLf & "   at Microsoft.Office.Tools.Word.ControlCollectionImpl.InternalAddInlineOtherControl(Control control, Range range, Single width, Single height, String name)" & vbCrLf & "   at Microsoft.Office.Tools.Word.ControlCollectionImpl.AddControl(Control control, Range range, Single width, Single height, String name)" & vbCrLf & "   at Microsoft.Office.Tools.Word.ControlExtensions.AddLinkLabel(ControlCollection controls, Range range, Single width, Single height, String name)" & vbCrLf & "   at Sureprep.WordlAddIns.CrossRefFunctionality.Add_CrossRefernce(CrossReference CR_Info, String strToolTip)

     

    Have you discover such issues?  What is the solution ?

    Please let me know if you need any additional information.

     

    Regards,

    Manoj


    ITTEAM

    Wednesday, April 24, 2013 6:44 AM

Answers

  • Hi Manoj

    Difficult to say, since it could be anything. The first thing I'd look into, however, would be the Range object you're passing. Identifying a Range using the Start and End points is a tricky business and usually doesn't yield the results you expect. There's a lot of "hidden" stuff in a Word document that will change those values (including adding/removing controls, such as your code does).

    Better would be for you to "mark" these Ranges with, for example, a bookmark. Then you can use that as the target. Test adding a bookmark in a document, then try positioning a control there using something like this to get the Range object:

      Dim rngTarget as Word.Range = theDocument.Bookmarks("theName").Range


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, April 24, 2013 10:57 AM
    Moderator
  • make sure to use selRange.Select() very carefully, because selection change event gets fire when ever select operation is performed. and also be sure to release the selection and range objects as well. 
    Marshal.ReleaseComObject()
    

    Faisal Ahmed Farooqui

    Wednesday, April 24, 2013 11:18 AM

All replies

  • Hi Manoj

    Difficult to say, since it could be anything. The first thing I'd look into, however, would be the Range object you're passing. Identifying a Range using the Start and End points is a tricky business and usually doesn't yield the results you expect. There's a lot of "hidden" stuff in a Word document that will change those values (including adding/removing controls, such as your code does).

    Better would be for you to "mark" these Ranges with, for example, a bookmark. Then you can use that as the target. Test adding a bookmark in a document, then try positioning a control there using something like this to get the Range object:

      Dim rngTarget as Word.Range = theDocument.Bookmarks("theName").Range


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, April 24, 2013 10:57 AM
    Moderator
  • make sure to use selRange.Select() very carefully, because selection change event gets fire when ever select operation is performed. and also be sure to release the selection and range objects as well. 
    Marshal.ReleaseComObject()
    

    Faisal Ahmed Farooqui

    Wednesday, April 24, 2013 11:18 AM