Insert Hyperlink via VBA

Posted by Martin on Super User See other posts from Super User or by Martin
Published on 2013-06-23T20:33:32Z Indexed on 2013/06/24 22:24 UTC
Read the original article Hit count: 265

Filed under:
|
|

I have a Word VBA macro that loops through a directory and writes down the file path of files selected for some criteria into a new Word document. Works well as plain text (as part of a loop):

wdDocResults.Content.InsertAfter objFile.Path & Chr(13)

However, I'd like them to be hyperlinks. The following works as single macro, but when called from within another script, it does nothing at all (no matter if path is provided as variable or string, or as H:... or \\MyServernameAsNetDrive...):

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

If try to select the current line in order to make sure something is selected at the right place --> error: out of memory":

wrdDocResults.Content.InsertAfter objFil.Path
Selection.Expand wdLine
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= objFile.Path, _
    SubAddress:="", ScreenTip:="", TextToDisplay:=objFile.Path

I also tried inserting a string resembling the Hyperlink field code ({ Hyperlink "..." }, which is of course not recognized...

Any help is appreciated... Thanks in advance!

© Super User or respective owner

Related posts about microsoft-word

Related posts about vba