Insert Hyperlink via VBA

1

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!

Martin

Posted 2013-06-23T20:33:32.030

Reputation: 237

Answers

0

Finally I found the problem: after using

wdDocResults.Content.InsertAfter "some text"

-> even though "some text" is nicely written into the right document, this document seems not to be activated. Only after one does

wrdDocResults.Activate
Selection.EndKey Unit:=wdStory

one can do one of these commands

ActiveDocument.Hyperlinks.Add Anchor:= ...
Selection.TypeText "some more text"

Otherwise both do not work.

Martin

Posted 2013-06-23T20:33:32.030

Reputation: 237