Results 1 to 4 of 4

Thread: Odd behavior in word macro

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    2

    Odd behavior in word macro

    So what I'm trying to do, is programatically create hyperlinks in a word document and point them to text written as a heading in another part of the word document. Easy enough to do by right clicking and selecting the correct option. So I figured I'd start by recording a macro doing exactly that. This is the result:


    ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
    SubAddress:="_Yet_more_text.", ScreenTip:="", TextToDisplay:="Link "


    Now the wierd part. Creating the hyperlink through the menu works fine (ie. I click it and it goes where i expect it to), but if I run the above macro, the link that is created sends me to the top of the document instead of to the specified heading! Almost like it doesn't recognize the address.

    Has anyone seen this before and can point out what I'm doing wrong? I'm using Word 2000 SP3, On XP SP2 (I've tried the same thing on several different machines with different OS's and different Word versions with the same result).

    Thanks

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Odd behavior in word macro

    Welcome to the Forums.

    So does it at least go to the correct page? Or just take you to the top of the current page?

    The anchor is tied to selected text when you recorded the macro. So if you dont have anything selected it wont have an anchor to go to.

    VB Code:
    1. 'This part of your code.
    2. Anchor:=Selection.Range 'If empty then it goes nowhere.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    2

    Re: Odd behavior in word macro

    The text is selected at the time the link is created, so the hyperlink appears to be anchored to something.

    No, it does not go to the correct page. If I create the link via the recorded macro, then click on the link, it takes me to the top of the document. So for example if its a 20 page document, I'm on page 6 when I click the link, it takes me to the top of page 1.

    Of course if I select the text, rick click and select Hyperlink, then it works just fine.

  4. #4
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: Odd behavior in word macro

    It seems that if you change the name of the text that you want to Link by putting different text in the TextToDisplay part of the code then it will not work.

    So if you have text called Link1 then in your code the TextToDisplay MUST be the Same - i.e. - 'Link1'

    If you change this to something else then it doesn't work !

    Examples -

    Text in Document = Link1 - Selected text for Hyperlink

    Then this WILL work !!

    VB Code:
    1. ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
    2. SubAddress:="_Header1", ScreenTip:="", TextToDisplay:="Link1"

    This WONT work !!

    VB Code:
    1. ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
    2. SubAddress:="_Header1", ScreenTip:="", TextToDisplay:="Link1Changed"

    Oh and DONT have spaces in your selection ie TextToDisplay:="Link "
    Last edited by NeedSomeAnswers; Oct 21st, 2005 at 06:48 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width