PDA

View Full Version : Requirements Linking Macro



joe
04-20-2006, 12:34 PM
Hey Everyone,

I wrote a macro today in MS Word that will find references in your document to requirements and insert bookmarks and internal hyperlinks to those requirements automatically.

To use this macro, just paste the code below into a blank macro in Word.

You need to write all requirements in your doc with the following identifier syntax:

(* is a wildcard)

FR_******* (I usually use FR_001_001 for example)

and all references need to be in this syntax:

See fr_******* (for example, See fr_001_001)

Note that the capitalization is important here as is the number of characters in your requirement identifier.

The macro will create hyperlinks for you so you can jump around to requirements with a single click.

Have fun!

joe


Sub linkRequirements()
'
' Testing Macro
' Macro recorded 4/19/2006 by Joe Shideler
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "FR_*"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.Font.Underline = wdUnderlineNone
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False

Do While .Execute(FindText:="FR_*") = True

Selection.MoveRight Unit:=wdCharacter, Count:=7, Extend:=wdExtend

reqReference = Selection.Text

For Each aVar In ActiveDocument.Bookmarks
If aVar.Name = "reqReference" Then num = aVar.Index
Next aVar
If num = 0 Then
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:=reqReference
End With
End If

Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdMove

Loop

End With

For Each aVar2 In ActiveDocument.Bookmarks

reqReference2 = "See " + aVar2.Name
reqLink = aVar2.Name

Selection.Find.ClearFormatting
With Selection.Find
.Text = reqReference2
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

Do While .Execute(FindText:=reqReference2) = True
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
SubAddress:=reqLink, ScreenTip:="", TextToDisplay:=reqReference2
Loop

End With

Next aVar2

End Sub