VBA: Can't change SubAddress property of Hyperlinks in Word Document










1














I'm trying to fix the hyperlinks in a Word document. I need to change the SubAddress property of some hyperlinks. To that end, I'm looping through them. Unfortunately, I get a very weird error saying method 'subaddress' of object 'hyperlink' failed when I try to change any SubAddress. Apparently this happens because something is broken with VBA itself.



Sub FixHyperlinks()
'
' FixHyperlinks Macro
'
'
ActiveDocument.Hyperlinks(1).SubAddress = "some new subaddress"
End Sub


I'm rocking Office 2016 Professional Plus. Can anybody tell me if this works for you?



It's easy to test. Just create a new document, type two one-word lines. Make the second line style "Heading 1". Go to first line, hit CTRK + K (to create hyperlink) point it to "a place in this document", select the heading you just created. DO NOT enter any address. Now go to Macros, paste the above and hit F5 while your caret is inside the code.



The hyperlink works fine when clicked with the mouse (first line hyperlink will take you to the 2nd line Heading).










share|improve this question




























    1














    I'm trying to fix the hyperlinks in a Word document. I need to change the SubAddress property of some hyperlinks. To that end, I'm looping through them. Unfortunately, I get a very weird error saying method 'subaddress' of object 'hyperlink' failed when I try to change any SubAddress. Apparently this happens because something is broken with VBA itself.



    Sub FixHyperlinks()
    '
    ' FixHyperlinks Macro
    '
    '
    ActiveDocument.Hyperlinks(1).SubAddress = "some new subaddress"
    End Sub


    I'm rocking Office 2016 Professional Plus. Can anybody tell me if this works for you?



    It's easy to test. Just create a new document, type two one-word lines. Make the second line style "Heading 1". Go to first line, hit CTRK + K (to create hyperlink) point it to "a place in this document", select the heading you just created. DO NOT enter any address. Now go to Macros, paste the above and hit F5 while your caret is inside the code.



    The hyperlink works fine when clicked with the mouse (first line hyperlink will take you to the 2nd line Heading).










    share|improve this question


























      1












      1








      1







      I'm trying to fix the hyperlinks in a Word document. I need to change the SubAddress property of some hyperlinks. To that end, I'm looping through them. Unfortunately, I get a very weird error saying method 'subaddress' of object 'hyperlink' failed when I try to change any SubAddress. Apparently this happens because something is broken with VBA itself.



      Sub FixHyperlinks()
      '
      ' FixHyperlinks Macro
      '
      '
      ActiveDocument.Hyperlinks(1).SubAddress = "some new subaddress"
      End Sub


      I'm rocking Office 2016 Professional Plus. Can anybody tell me if this works for you?



      It's easy to test. Just create a new document, type two one-word lines. Make the second line style "Heading 1". Go to first line, hit CTRK + K (to create hyperlink) point it to "a place in this document", select the heading you just created. DO NOT enter any address. Now go to Macros, paste the above and hit F5 while your caret is inside the code.



      The hyperlink works fine when clicked with the mouse (first line hyperlink will take you to the 2nd line Heading).










      share|improve this question















      I'm trying to fix the hyperlinks in a Word document. I need to change the SubAddress property of some hyperlinks. To that end, I'm looping through them. Unfortunately, I get a very weird error saying method 'subaddress' of object 'hyperlink' failed when I try to change any SubAddress. Apparently this happens because something is broken with VBA itself.



      Sub FixHyperlinks()
      '
      ' FixHyperlinks Macro
      '
      '
      ActiveDocument.Hyperlinks(1).SubAddress = "some new subaddress"
      End Sub


      I'm rocking Office 2016 Professional Plus. Can anybody tell me if this works for you?



      It's easy to test. Just create a new document, type two one-word lines. Make the second line style "Heading 1". Go to first line, hit CTRK + K (to create hyperlink) point it to "a place in this document", select the heading you just created. DO NOT enter any address. Now go to Macros, paste the above and hit F5 while your caret is inside the code.



      The hyperlink works fine when clicked with the mouse (first line hyperlink will take you to the 2nd line Heading).







      vba ms-word






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 at 7:40

























      asked Nov 12 at 8:58









      Axonn

      3,60252433




      3,60252433






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Although Hyperlink.SubAddress Property is supposed to be a read/write string, writing to it fails - even in Word 2010. Try something along the lines of:



          Dim Rng As Range, StrAddr As String, StrTxt As String
          With ActiveDocument
          With .Hyperlinks(1)
          Set Rng = .Range
          StrAddr = .Address
          StrTxt = .TextToDisplay
          .Delete
          End With
          .Hyperlinks.Add Anchor:=Rng, Address:=StrAddr, SubAddress:="new_sub_address"
          End With





          share|improve this answer




















          • That's indeed the choice I eventually went for. I deleted and re-created the hyperlink. Thank you! :). How did you find out that the property is read-only? Just by trying or is there some "secret" documentation for VBA or some way to inspect the capabilities of the DLLs (and other libraries) involved?
            – Axonn
            Nov 14 at 7:41











          • @Axonn I'm not saying it's read-only, as the VBA doco says otherwise; all I said is that writing to it, even in Word 2010, results in an error.
            – macropod
            Nov 14 at 9:10










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258728%2fvba-cant-change-subaddress-property-of-hyperlinks-in-word-document%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Although Hyperlink.SubAddress Property is supposed to be a read/write string, writing to it fails - even in Word 2010. Try something along the lines of:



          Dim Rng As Range, StrAddr As String, StrTxt As String
          With ActiveDocument
          With .Hyperlinks(1)
          Set Rng = .Range
          StrAddr = .Address
          StrTxt = .TextToDisplay
          .Delete
          End With
          .Hyperlinks.Add Anchor:=Rng, Address:=StrAddr, SubAddress:="new_sub_address"
          End With





          share|improve this answer




















          • That's indeed the choice I eventually went for. I deleted and re-created the hyperlink. Thank you! :). How did you find out that the property is read-only? Just by trying or is there some "secret" documentation for VBA or some way to inspect the capabilities of the DLLs (and other libraries) involved?
            – Axonn
            Nov 14 at 7:41











          • @Axonn I'm not saying it's read-only, as the VBA doco says otherwise; all I said is that writing to it, even in Word 2010, results in an error.
            – macropod
            Nov 14 at 9:10















          1














          Although Hyperlink.SubAddress Property is supposed to be a read/write string, writing to it fails - even in Word 2010. Try something along the lines of:



          Dim Rng As Range, StrAddr As String, StrTxt As String
          With ActiveDocument
          With .Hyperlinks(1)
          Set Rng = .Range
          StrAddr = .Address
          StrTxt = .TextToDisplay
          .Delete
          End With
          .Hyperlinks.Add Anchor:=Rng, Address:=StrAddr, SubAddress:="new_sub_address"
          End With





          share|improve this answer




















          • That's indeed the choice I eventually went for. I deleted and re-created the hyperlink. Thank you! :). How did you find out that the property is read-only? Just by trying or is there some "secret" documentation for VBA or some way to inspect the capabilities of the DLLs (and other libraries) involved?
            – Axonn
            Nov 14 at 7:41











          • @Axonn I'm not saying it's read-only, as the VBA doco says otherwise; all I said is that writing to it, even in Word 2010, results in an error.
            – macropod
            Nov 14 at 9:10













          1












          1








          1






          Although Hyperlink.SubAddress Property is supposed to be a read/write string, writing to it fails - even in Word 2010. Try something along the lines of:



          Dim Rng As Range, StrAddr As String, StrTxt As String
          With ActiveDocument
          With .Hyperlinks(1)
          Set Rng = .Range
          StrAddr = .Address
          StrTxt = .TextToDisplay
          .Delete
          End With
          .Hyperlinks.Add Anchor:=Rng, Address:=StrAddr, SubAddress:="new_sub_address"
          End With





          share|improve this answer












          Although Hyperlink.SubAddress Property is supposed to be a read/write string, writing to it fails - even in Word 2010. Try something along the lines of:



          Dim Rng As Range, StrAddr As String, StrTxt As String
          With ActiveDocument
          With .Hyperlinks(1)
          Set Rng = .Range
          StrAddr = .Address
          StrTxt = .TextToDisplay
          .Delete
          End With
          .Hyperlinks.Add Anchor:=Rng, Address:=StrAddr, SubAddress:="new_sub_address"
          End With






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 23:42









          macropod

          2,167228




          2,167228











          • That's indeed the choice I eventually went for. I deleted and re-created the hyperlink. Thank you! :). How did you find out that the property is read-only? Just by trying or is there some "secret" documentation for VBA or some way to inspect the capabilities of the DLLs (and other libraries) involved?
            – Axonn
            Nov 14 at 7:41











          • @Axonn I'm not saying it's read-only, as the VBA doco says otherwise; all I said is that writing to it, even in Word 2010, results in an error.
            – macropod
            Nov 14 at 9:10
















          • That's indeed the choice I eventually went for. I deleted and re-created the hyperlink. Thank you! :). How did you find out that the property is read-only? Just by trying or is there some "secret" documentation for VBA or some way to inspect the capabilities of the DLLs (and other libraries) involved?
            – Axonn
            Nov 14 at 7:41











          • @Axonn I'm not saying it's read-only, as the VBA doco says otherwise; all I said is that writing to it, even in Word 2010, results in an error.
            – macropod
            Nov 14 at 9:10















          That's indeed the choice I eventually went for. I deleted and re-created the hyperlink. Thank you! :). How did you find out that the property is read-only? Just by trying or is there some "secret" documentation for VBA or some way to inspect the capabilities of the DLLs (and other libraries) involved?
          – Axonn
          Nov 14 at 7:41





          That's indeed the choice I eventually went for. I deleted and re-created the hyperlink. Thank you! :). How did you find out that the property is read-only? Just by trying or is there some "secret" documentation for VBA or some way to inspect the capabilities of the DLLs (and other libraries) involved?
          – Axonn
          Nov 14 at 7:41













          @Axonn I'm not saying it's read-only, as the VBA doco says otherwise; all I said is that writing to it, even in Word 2010, results in an error.
          – macropod
          Nov 14 at 9:10




          @Axonn I'm not saying it's read-only, as the VBA doco says otherwise; all I said is that writing to it, even in Word 2010, results in an error.
          – macropod
          Nov 14 at 9:10

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258728%2fvba-cant-change-subaddress-property-of-hyperlinks-in-word-document%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto