0

I'm having trouble with getting text from a bookmark in a hyperlink. Can I get the contents of the bookmark range from an alternate document (since it's defined by bookmark), or do I have to jump back and forth between docs to copy the contents? I can get a whole file to insert as a sanity check. However I need to automatically get the contents of a multiple bookmarks (in the hyperlinks) from a supplemental file Insertion.docm. VB help indicates I can retrieve part of a file (bookmark or range), but I don't see how to access the bookmarked selection using a macro after I've set them up. Note that I'm trying to get text FROM the hyperlink/bookmark, and INSERT it at the location of the hyperlink. Here's my macro:

Sub MAFExpandHyperlink()
    Dim Str
    Dim Path As String
    Dim HyperRng As Range
    Dim Bookmk As Variant 'Using Range kills it
    Dim Cntr As Integer

    For Each Bookmk In ActiveDocument.Hyperlinks
       Path = "\\User\Mark\Macro\"
       Str = Path & Hyper.Address
       Cntr = Cntr + 1
       HyperRng = Bookmk.Range
       Hyperlink.Range.InsertFile Bookmk.Address, HyperRng 

    Next
    End Sub

As background: '!!!!Debug watch shows Subaddress="TextInSec1"
'Address = "InsertionMaterial.docm" ' Hyper.Range.Insertfile(Str) 'This works (I'm close), but it inserts the whole file! When I set a watch on the contents of the bookmark after it exists, I see Address = "InsertionMaterial" and Subaddress="TextInSec1", which is right.

As I run the macro in Main, I've got 2 files open, Main.docm (where I run the macro) and Insertion.docm, created several bookmarks defining selected areas Insertion.docm, and have several hyperlinks in Main.docm that point to the appropriate bookmarked regions in Insertion#Material.

In searching for an answer, I've seen many examples where folks are doing the opposite; trying to add text to a bookmark, but I'm trying to GET text FROM a bookmarked range in another file to insert in main at the hyperlink.

Please help, or maybe point me to an example which accesses a bookmark in a second Word window.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118

1 Answers1

0

If you want to access two documents programmatically, you do not want to use ActiveDocument. Declare variables for the documents:

Dim oWordDoc As Word.Document

and then open the documents you want to work on:

Set oWordDoc = Documents.Open(Filename:=strFolder & "\" & strFilename, Format:=wdOpenFormatText)

(the above is an example from some of my code, but you should be able to figure it out.)

Then get the hyperlinks as follows:

For Each Bookmk In oWordDoc.Hyperlinks

Also, get rid of that "End With" as I don't see a With statement.

  • Thanks for your help! I've gotten parts to work, although I'm still trying to use members that will work with the For Each Bookmk and insert the formatted text. – user3610011 May 14 '14 at 18:57
  • Why do you suggest not to use ActiveDocument? – user3610011 May 14 '14 at 18:57
  • A few reasons. One is that you need to activate the document before using ActiveDocument, and then, you propbably want to check whether the ActiveDocument is the one you want. Much simpler to just assign the document to a variable and access it that way. Second, if the user clicks on the inactive document, it becomes the active document, and may bring unintended results. – Michael Blaustein May 14 '14 at 19:01