0
votes

I'm trying to create a script in VBA for a Word document that bookmarks and hyperlinks matching strings with numbering to a location later in the document. Right now I have two sets of matching for matching text that looks like this (sometimes with hundreds of more entires):

Contents

'There is a bookmark here named "InpCon"

  1. 4326: Info 1
  2. 32534: Info 2
  3. 7980: Info 3

Body

'There is a bookmark here named "InpBod"

  1. 4326: Info 1
  2. 32534: Info 2
  3. 7980: Info 3

This is what I'm trying to do:

Table of Contents

'There is a bookmark here named "InpCon"

  1. 4326: Info 1 'Hyperlink to Bookmark Name: "Info1"
  2. 32534: Info 2 'Hyperlink to Bookmark Name: "Info2"
  3. 7980: Info 3 'Hyperlink to Bookmark Name: "Info3"

Body

'There is a bookmark here named "InpBod"

  1. 4326: Info 1 'Bookmark Name: "Info1"
  2. 32534: Info 2 'Bookmark Name: "Info2"
  3. 7980: Info 3 'Bookmark Name: "Info3"

I'm trying to implement this at the end of a larger script I have. The larger script copies everything under the "InpBod" bookmark to the "InpCon" bookmark at one point. I don't know if inputting a process there to do this would be more efficient.

Basically, my VBA knowledge is pretty limited. I've tried looking for something that accomplishes this task, but haven't found anything. I'd paste my code, but it's pretty large and on a stand alone system. This would shave hours off my week if I can get it done. Can someone help me out or point me in the right direction? Thanks you in advance.

1

1 Answers

0
votes

Don't "manually" generate "InpCon". Use Word to generate the TOC. When generating "InpBod", everytime you come across something you want to link to, mark it and then actomatically generate the TOC. Two possible ways to do so:

  1. Use heading styles and then generate a TOC. Everytime you stumble upon an entry to be marked, mark the paragraph as a heading Selection.Range.Paragraphs.Style = ActiveDocument.Styles(wdStyleHeading1). You may want to change the default heading styles. Then insert a TOC at the beginning.
  2. Use bookmarks if the document uses headers for something else. Everytime you stumble upon an entry to be marked, create a bookmark like this: ActiveDocument.Bookmarks.Add Range:=Selection.Range.Collapse wdCollapseEnd, Name:=**THEBOOKMARKNAME**. Notice that you'll have to remove spaces and that kind of stuff for the bookmark name. Then iterate thorugh all the activedocument.Bookmarks inserting the links using Selection.InsertCrossReference ReferenceType:="Bookmark", ReferenceKind:= _ wdContentText, ReferenceItem:=ITEM, InsertAsHyperlink:=True, _ IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "

The macro recorder will expose every object you need to do this.


Using newer info from the comments: The Word's list object model is pretty well illustrated here: Looping a Word macro over many list paragraphs causes memory issue and here http://oreilly.com/catalog/writewordmacro/chapter/ch17.html with more detail. Here's the proper documentation: http://msdn.microsoft.com/en-us/library/aa223019(v=office.11).aspx

You should loop through the lists collection members (for what I gather you should only have one list, the InpBod list). Then loop through the paragraphs in that list selecting each like this:

Dim para as Range
For Each para In ActiveDocument.Lists(1).ListParagraphs
   para.Range.Select
Next para