Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2008

Tip: Looking for answers? Try searching our database.

HyperLink editing in Word VBA - Hyperlinks.Item(Index) value not r

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rob - 30 Mar 2008 02:03 GMT
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

<seems to work>  If the field is a hyperlink, display the Text to display,
target, address, subaddress, index number.
<does not work>   Then using the index number, access the hyperlink object
from the hyperlinks collection using the
                            index number to get/display the properties.
<note coded yet>   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.

<note coded yet>   For each hyperlink, change the Text to display to the
following format:Page/LineNumber or Page,LineNumber
<seems to work>   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
macropod - 30 Mar 2008 07:01 GMT
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
Signature

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

> Hello;
>
[quoted text clipped - 99 lines]
> Thanks...I feel the programming pain, where is the gain?
> Rob
macropod - 30 Mar 2008 07:08 GMT
Even better would be to use:
LineMrk = Replace(BmkNm, "Page", "Line")
instead of
LineMrk = "Line" & Mid(BmkNm, 5, (Len(BmkNm) - 4))

Cheers
Signature

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

> Hi Rob,
>
[quoted text clipped - 145 lines]
>> Thanks...I feel the programming pain, where is the gain?
>> Rob
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.