Share via

Help with Insert Multiple Hyperlinks Macro for Word

Anonymous
2010-12-23T00:11:37+00:00

So Word's built-in "Insert Hyperlink" feature in Word 2002 is awful, and it does not appear to have changed in Word 2007 or 2010 either.  In short, it forces you to insert each hyperlink individually, and each time you select the option from the menu, it gives you a file dialog that has no memory of the last place you navigated to.  This makes it an ABSOLUTE PAIN to insert, say, hyperlinks to 5 documents located in a directory structure which is 5 levels deep--at least 25 clicks are involved.  To me, it is incomprehensible that multi-pick functionality and previous working directory memory functionality was not included from the get-go.  It is criminal that this is still the case in 2010. 

Given that Microsoft appears to think this isn't worth addressing, I figured I'd just write my own code, which is included below.

It works phenomenally well, until it inexplicably causes an "Out of Memory" error and dies.  I want to know what I'm doing wrong or why it would do this.  Some additional background facts:

a) If you insert a hyperlink using VBA while in the middle of a pre-existing hyperlink, you'll crash.  Thus, I have code to prevent such occurences.

b) If you have Word as your email editor, then the macro works there as well.  But sometimes it won't paste it into the current email document, but into whatever document is currently active in Word (which doesn't seem like the "current" document if you're currently editing an email).

c) The "Out of memory" error appears to be entirely random.  Some days, I can use it all day without issue.  Others, it crashes within minutes.

Cheers,

Christian

Sub InsertMultiHL_PathLbl()

Dim fd As FileDialogSet fd = Application.FileDialog(msoFileDialogFilePicker)

Dim SelectedFile As VariantWith fd    If .Show = -1 Then        Application.ActiveWindow.Selection.Collapse        If Application.ActiveWindow.Selection.Hyperlinks.Count >= 1 Then            MsgBox "Insertion Point Cannot Be Within Preexisting Hyperlink!"        Else            For Each SelectedFile In .SelectedItems                Application.ActiveWindow.Document.Hyperlinks.Add Anchor:=Selection.Range, _                    Address:=SelectedFile, _                    SubAddress:="", _                    ScreenTip:="", _                    TextToDisplay:=SelectedFile                Application.ActiveWindow.Selection.TypeParagraph            Next SelectedFile        End If    Else    End IfEnd WithSet fd = Nothing

End Sub

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

6 answers

Sort by: Most helpful
  1. Anonymous
    2010-12-28T21:30:50+00:00

    Thanks, Andreas--I'll try your modification out and see if that changes anything.

    Christian

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2010-12-28T21:30:15+00:00

    Hi Gerry,

    I'm afraid I don't understand the relevance of the 3 "buts" above to the issue I am having. 

    1. I typically include hyperlinks to documents which are logically named; I include 2-4 such document links in an email for the reader to open and peruse.  I also have a code version which truncates out the pathname so that it is only the filename provided to the reader.  And if you wanted to, you could, as you indicate, put a dialog box in which shows up in each iteration and lets the user alter the filename.  I didn't feel it necessary in this case.
    2. Granted, all the files must be in the same folder.  In my case, I typically have all of the files in the same folder, so it isn't a big deal that it can't navigate to multiple folders.  I can't think of many (or any) file pickers which allow for picks from multiple folders.  Also, folder memory helps tremendously when I DO have files in several folders which are in close proximity to each other--it might require two separate Insert Hyperlink actions, but at least the second one will require less navigating than the first.

    3)  I'm not sure why this is a big disadvantage.  Do you mean "folder"?  If so, I agree--inserting a folder as a hyperlink would be nice.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2010-12-26T11:41:11+00:00

    a) If you insert a hyperlink using VBA while in the middle of a pre-existing hyperlink, you'll crash.  Thus, I have code to prevent such occurences.

    Whilst it is unlikely to be what you want, and having code to suppress it is probably wise, this is possible and should not crash Word. That it does, and the fact that you have other errors suggests either (a) a misbehaving AddIn or (b) a faulty installation of Word.

    To see if it is the former, try running your code with Word in Safe Mode (press and hold Ctrl while starting Word). Otherwise, try repairing your installation of Word (Office button > Word Options > Resources tab > Diagnose)


    Enjoy,

    Tony

    www.WordArticles.com

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2010-12-23T20:07:00+00:00

    I am not sure either why you would get an Out of Memory error.  Something else must be going on.

    True, your procedure allows you to insert multiple hyperlinks.  But...

    1.  you can not change the display text (unless you add another dialog, an InputBox say?)

    2.  you can not use multiple locations (all the files must be from the same folder)

    3.  you can only use files, you can not use bookmarks as a Target


    Gerry Word MVP

    Was this answer helpful?

    0 comments No comments
  5. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2010-12-23T06:52:31+00:00

    Am 23.12.2010 01:11, schrieb Christianovitch:

    It works phenomenally well, until it inexplicably causes an "Out of Memory" error and dies.  I want to know what I'm doing wrong or why it would do this.

    I have not realy an idea whats wrong, but I think there is no need to use ActiveWindow.

    Andreas.

    Sub InsertMultiHL_PathLbl()

      Dim SelectedFile As Variant

      If Selection.Hyperlinks.Count >= 1 Then

        MsgBox "Insertion Point Cannot Be Within Preexisting Hyperlink!"

        Exit Sub

      End If

      With Application.FileDialog(msoFileDialogFilePicker)

        If .Show = -1 Then

          For Each SelectedFile In .SelectedItems

            ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _

              Address:=SelectedFile, _

              SubAddress:="", _

              ScreenTip:="", _

              TextToDisplay:=SelectedFile

            Selection.TypeParagraph

          Next

        End If

      End With

    End Sub

    Was this answer helpful?

    0 comments No comments