views:

546

answers:

2

I have an HTML page with links that when clicked open to a specific bookmark in a word document. I am working with an existing word 2003 document with no preexisting bookmarks. I want to add bookmarks to all section number header locations using a macro or a VBA script . Example

3.1.4.2 HERE
STUFF
3.1.4.2.1 Here again
MORE STUFF
3.1.4.2.1.1 Here again again
EVEN MORE STUFF
3.1.4.2.2 Here again again again
LOTS MORE STUFF

I want to bookmark all the lines that start with X.X.X... with a standard format for the name.

Example (using above as reference )

3.1.4.2 HERE line would have a book mark named M_3_1_4_2
3.1.4.2.1 Here again would have a book mark named M_3_1_4_2_1

ect.

My question is what approach with a VBA script or a macro would I need to take that would make this happen.

+2  A: 

Adding a bookmark is easy enough if you have the range object already.

ActiveDocument.Bookmarks.Add Name:=rngBookmark.Text, Range:=rngBookmark

Getting the range is often the difficult task. Now you said these were section headers. Are they actual word section headers? Are they delimited with a certain style? Are they in the body of the document or in page headers?

You can cycle through the sections of a document like this and set a range to the start of the section.

Dim sectCurrent As Word.Section
Dim rngCurrent As Word.Range
For Each sectCurrent In ActiveDocument.Content.Sections

   ' get range that refers to the whole section
   Set rngCurrent = sectCurrent.Range.Duplicate

   ' collapse the range to the start of the section
   rngCurrent.Collapse wdCollapseStart

   ' expand the range to hold the first "word"
   ' you can also use other units here like wdLine
   rngCurrent.MoveEnd Unit:=wdWord, Count:=1

   ' now that you have the range you can add the bookmark
   ' you can process the range and create your own name with a custom function GenerateBookmarkName.  To get the string, just use rngCurrent.Text.
   ActiveDocument.Bookmarks.Add Name:=GenerateBookmarkName(rngCurrent), Range:=rngCurrent

Next sectCurrent

Now if they aren't actual sections, you'll often want to use the Find object to find something in the document and loop through all such items. The trick here is to know what to search for. An example loop is below.

   ' setup range object for search results
   Set rngFind = ActiveDocument.Content

   ' cycle through search results looking for whatever
   With rngFind.Find

      ' search text
      .Text = "FINDME"
      .Format = False
      .Wrap = wdFindStop

      ' loop while find is successfull
      Do While .Execute

         ' get range you can modify based on found item
         ' each time you call .Execute rngFind is changed to the found text
         Set rngModifyMe = rngFind.Duplicate    


      Loop

   End With

For more word vba help, you can vist the word MVPs site here: http://word.mvps.org

Will Rickards
A: 

Hi I tried to create a bookmark using hese code, i tried using wdLine instead wdWord since i want to select a particular range of lines for a bookmark. Can you just help me wat parameters to give for these