Group: microsoft.public.word.vba.general
From: "macropod"
Date: Sunday, March 30, 2008 1:01 AM
Subject: Re: HyperLink editing in Word VBA - Hyperlinks.Item(Index) value not r

Hi Rob,

If you bookmark the hyperlink field, you can then use a PAGREF field to cross-reference it's page number.

If you give all such bookmarks a unique set of meaningful sequential names (eg PageMrk1, PageMrk2, etc) then any vba code you might
want to use to process these can find them easily enough. If you likewise bookmark the first of any line# cross-references in the
text (eg LineMrk1, LineMrk2, etc), then your vba code can use the PageMrk# bookmarks to update the LineMrk# bookmarks.

For example the following sub will update any LineMrk# bookmark for which a corresponding PageMrk# exists:

Private Sub GetLineRef(BmkNm As String)
Dim BmkRng As Range
Dim LineRef As String
Dim LineMrk As String
With ActiveDocument
If .Bookmarks.Exists(BmkNm) = False Then Exit Sub
LineRef = .Bookmarks(BmkNm).Range.Information(wdFirstCharacterLineNumber)
LineMrk = "Line" & Mid(BmkNm, 5, (Len(BmkNm) - 4))
If .Bookmarks.Exists(LineMrk) Then
Set BmkRng = .Bookmarks(LineMrk).Range
BmkRng.Text = LineRef
.Bookmarks.Add LineMrk, BmkRng
.Bookmarks.Item(LineMrk).Range.Fields.Update
Set BmkRng = Nothing
End If
End With
End Sub

and the following sub can be used to process them all:

Sub UpdateAllLineRefs()
Dim i As Integer
Dim BkStr As String
With ActiveDocument
For i = 1 To .Bookmarks.Count
BkStr = "PageMrk"
GetLineRef BkStr & i
Next
End With
End Sub

Now, since you can only have a single instance of any given bookmark, if you need to refer to the same line number two or more
times, simply cross-reference the relevant LineMrk# bookmark via Insert|Cross-reference. You can then use add a simple
'.fields.update' statement to the above sub, just before the 'End With' line to update those also.

Cheers
--
macropod
[MVP - Microsoft Word]
-------------------------

"Rob" wrote in message news:B68B7014-D832-43A9-9C6B-F6584C94FE60@microsoft.com...
> Hello;
>
> First my purpose.....to help a friend with an office automation problem
> (see other posts I have made) with updating the text that is displayed in
> hyperlinks. It may be that using some other field or tag may work
> better..opinions and solutions please...
> NOTE: Some of the below code is from other posts/contributors in this forum
> (Thank You macropod!).
>
> The problem is in legal pleading type pages, there needs to be a table with
> a column of crossreferences that list the page and linenumber (that may not
> be possible to do automatically I am finding) or section/heading/numbering.
>
> Some type of fields will automatically update on F9 like page number using
> the PAGE or PAGEREF field code. Same thing with almost everything except line
> number. In order to exploit if a line number solution is possible I am
> enumerating the various properties of the fields. At the moment (having never
> used the office automation but having C#/Ansi C/ and some VB5-6 experience) I
> am struggling to get the included below code to do the following (it seems
> pertinent to do, but if I am in error please advise me so):
>
> Cycle through all the fields in the ActiveDocument
>
> If the field is a hyperlink, display the Text to display,
> target, address, subaddress, index number.
> Then using the index number, access the hyperlink object
> from the hyperlinks collection using the
> index number to get/display the properties.
> If the field is a bookmark,
> display the Title of the bookmark,
> display the page it is located on,
> most importantly (I think) is get the
> character range (ie start and end)
> so that I might be able to get the
> linenumber from one of the properties of the range object
> after setting the range object to the
> bookmarks start and end position.
>
> For each hyperlink, change the Text to display to the
> following format:Page/LineNumber or Page,LineNumber
> Count total of HyperLinks in document
>
> Please advise where I have improperly coded or assumed the schema. I am
> working in Office 2002 SP3.
> References are:
> Visual Basic for Applications
> Microsoft Word 10.0 Object Library
> OLE Automation
> Normal
> Microsoft Office 10.0 Object Library
>
> Do I have to Dimension a hyperlink object and then if the field.type is
> hyperlink, then do hyperlink=field in order to get the Address? Or is the way
> I am trying below the better approach?
>
>
> Sub HLinkTest()
> Dim oFld As Field
> Dim oCount As Integer
> Dim oRange As Word.Range
> With ActiveDocument
> .Range.AutoFormat
> For Each oRange In .StoryRanges
> Do
> For Each oFld In oRange.Fields
> If oFld.Type = wdFieldHyperlink Then
> If Left(oFld.Result, 7) = "mailto:" Then MsgBox
> "oFld.Result= " & oFld.Result, vbOKOnly, "oFld.Result"
> If Left(oFld.Result, 7) = "mailto:" Then MsgBox
> "oFld.Kind= " & oFld.Kind, vbOKOnly, "oFld.Kind"
> If Left(oFld.Result, 7) = "mailto:" Then MsgBox
> "oFld.Code= " & oFld.Code, vbOKOnly, "oFld.Code"
> If Left(oFld.Result, 7) = "mailto:" Then MsgBox
> "oFld.ShowCodes= " & oFld.ShowCodes, vbOKOnly, "oFld.ShowCodes"
> If Left(oFld.Result, 7) = "mailto:" Then MsgBox
> "oFld.Index= " & oFld.Index, vbOKOnly, "oFld.Index"
> If Left(oFld.Result, 7) = "mailto:" Then MsgBox
> ActiveDocument.Hyperlinks.Item(oFld.Index).Target, vbOKOnly,
> "ActiveDocument.Hyperlinks.Item(oFld.Index).Target"
> If Left(oFld.Result, 7) = "mailto:" Then MsgBox
> ActiveDocument.Hyperlinks.Item(oFld.Index).Address, vbOKOnly,
> "ActiveDocument.Hyperlinks.Item(oFld.Index).Address"
> If Left(oFld.Result, 7) = "mailto:" Then MsgBox
> ActiveDocument.Hyperlinks.Item(oFld.Index).SubAddress, vbOKOnly,
> "ActiveDocument.Hyperlinks.Item(oFld.Index).SubAddress"
> If Left(oFld.Result, 7) = "mailto:" Then MsgBox
> ActiveDocument.Hyperlinks.Item(oFld.Index).TextToDisplay, vbOKOnly,
> "ActiveDocument.Hyperlinks.Item(oFld.Index).TextToDisplay"
> oCount = oCount + 1
> End If
> If oFld.Type = wdBookmark Then
> MsgBox "BM oFld.Result= " & oFld.Result, vbOKOnly,
> "oFld.Result"
> End If
> Next oFld
> Set oRange = oRange.NextStoryRange
> Loop Until oRange Is Nothing
> Next oRange
> MsgBox "Total Detected HyperLinks=" & oCount
> End With
> End Sub
>
> Thanks...I feel the programming pain, where is the gain?
> Rob

Safety Articles | Usenet Groups | Usenet News | Bluegrass