Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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!

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.