Bookmark and Hyperlink creation automation script needed

G

Gregg Parrish

I am authoring test documents that have between 45 and 700+ steps documented
in a Word table. The document requires that each step have a link navigate to
a screenshot at the bottom of the document and a link to navigate back to the
step.

illustration:

Step 1 | Test the Widget |(3 more content columns)| {PASS/FAIL}
'
' Rest of document
' End of Table

{Screenshot 1} Test the Widget


Where the Pass/Fail link navigates to the Screenshot 1 bookmark and the
Screenshot 1 link navigates to the Pass/Fail bookmark

We are currently writing these links and bookmarks manually. I can't figure
out how to automate this process. It seems simple, but the scripting is
beyond my skillset.
 
T

Tony Jollans

Assuming you already have a document and are just missing the links, it
would be possible to automate the whole process if ....

... the text "Test the Widget" is exactly the same in the table and with
the image
... and there is some way to link the image and its associated text

Alternatively, if you are writing the document it should be possible to
write a custom routine that you could run with the cursor in the table cell,
to ask for and insert the image, with caption and hyperlinks - or perhaps to
run against a completed table and insert all the images at one time

How are your pictures arranged - or how do want them arranged?

As a very rough and ready example, this code will look through a document
(containing no hyperlinks) containing a single table and pick up the text in
each cell in column 2 in turn, search for that text after the table, look
back from the found text (if any) for an image and then set up hyperlinks
back and fore between the table and the image.

For Each c In ActiveDocument.Tables(1).Columns(2).Cells
With ActiveDocument.Range(ActiveDocument.Tables(1).Range.End,
ActiveDocument.Content.End)
.Find.Execute (StrReverse(Mid(StrReverse(c.Range.Text), 2)))
If .Find.Found Then
.Collapse wdCollapseStart
.MoveUntil Chr(1), wdBackward
ActiveDocument.Bookmarks.Add "BM" & c.RowIndex & "From", c.Range
ActiveDocument.Bookmarks.Add "BM" & c.RowIndex & "To",
ActiveDocument.Range(.Start - 1, .End)
.Hyperlinks.Add ActiveDocument.Range(.Start - 1, .End), , "BM" &
c.RowIndex & "From"
.Hyperlinks.Add ActiveDocument.Range(c.Range.Start,
c.Range.End - 1), , "BM" & c.RowIndex & "To"
End If
End With
Next

(beware of line breaks in the code)

Clearly you will need something a bit more sophisticated but is the sort of
thing you want?
 
G

Gregg Parrish

Tony,

First, thank you very much for your kind response. I apologize for
miscommunicating.

The "Test the widget" text is identical in both places and there are no
images in the document (Those are added during testing)

To clarify, I am scripting the creation of the text of the document, no
images involved yet. I can pseudocode the process as:

For each row of the table 2 except the first two rows (header rows)
Type the words "PASS / FAIL" in Column 8 (Actual Test Result)
Make the text a hyperlink to the bookmark named "Screenshot<Step Number>"
Bookmark the hyperlink with the name "Step<Step Number>"
Copy the text the cell in column 2 (Scenario Name)
Move to the end of the document
Type "Step <Step Number> - " Paste the text from column 2
Make the "Screenshot <Step Number>" text a hyperlink to the bookmark
"Step<Step Number>"
Bookmark the hyperlink with the name "Screenshot<Step Number>"


Once again, thank you for your time. I hope that you or another power user
can help me.

Regards,
Gregg
 
T

Tony Jollans

Hi Gregg,

Give this a try - see if it does what you want.

Sub Widget()

Dim Ro As Row
Dim No As Long
Dim Name As String
Dim EndPos As Long

For Each Ro In ActiveDocument.Tables(2).Rows
If Not Ro.HeadingFormat Then

No = CLng(Left(Ro.Cells(1).Range.Text,
Len(Ro.Cells(1).Range.Text) - 1))
Name = Left(Ro.Cells(2).Range.Text,
Len(Ro.Cells(2).Range.Text) - 1)

With ActiveDocument
Ro.Cells(8).Range.InsertAfter "PASS/FAIL"
.Bookmarks.Add "Step" & No, .Range(Ro.Cells(8).Range.Start,
Ro.Cells(8).Range.End - 1)

EndPos = .Range.End - 1
.Content.InsertAfter "Step" & No & " - " & Name
.Bookmarks.Add "Screenshot" & No, .Range(EndPos,
..Range.End - 1)

.Hyperlinks.Add .Bookmarks("Step" & No).Range, ,
"Screenshot" & No
.Hyperlinks.Add .Bookmarks("Screenshot" & No).Range, ,
"Step" & No
End With

End If
Next

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top