none
Word Add-In use of HomeKey RRS feed

  • Question

  • I am trying to port VBA code to an Add-In for MS Word.  I am getting befuddled by the complexity that Add-Ins bring to simple VBA code.  I cannot get this simple VBA to work in VSTO:

        'Move to the beginning of the document

        Selection.HomeKey Unit:=wdStory

    I have been unable to crack how to move to the beginning of the document!  Really???  The code I have tried (in a variety of permutations) is

    Global Variable

        Public obj As New Microsoft.Office.Interop.Word.Document

    Set OBJ

            obj = Globals.ThisAddIn.Application.ActiveDocument

    Local variable

            Dim selxn As Microsoft.Office.Interop.Word.Selection

    Code

            selxn.HomeKey(Unit:=Word.WdUnits.wdStory)

    The error I get is selxn isn't set to anything.  What do I set it to?  in VBA, SELECTION just IS - I don't need to initialize it.

    If I put Selection.HomeKey... (which every website seems to use as an example of how to use it), I get "Selection" is not declared - it thinks its a variable!

    Thursday, September 28, 2017 6:03 PM

Answers

  • Hello,

    Please use:

            Dim selxn As Microsoft.Office.Interop.Word.Selection
            selxn = Globals.ThisAddIn.Application.Selection
            selxn.HomeKey(Unit:=Word.WdUnits.wdStory)

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by FlyinBlind Friday, September 29, 2017 7:00 PM
    Friday, September 29, 2017 2:21 AM
    Moderator

All replies

  • Hello,

    Please use:

            Dim selxn As Microsoft.Office.Interop.Word.Selection
            selxn = Globals.ThisAddIn.Application.Selection
            selxn.HomeKey(Unit:=Word.WdUnits.wdStory)

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by FlyinBlind Friday, September 29, 2017 7:00 PM
    Friday, September 29, 2017 2:21 AM
    Moderator
  • Thank you Celeste.  The Object Model presented in VSTO isn't as intuitive as the VBA version, it makes it harder to figure these things out on my own.
    Friday, September 29, 2017 7:01 PM