Question : From my VB6 app, how do I replace specific text in a Word 2003 document with a hyperlink?

Hi Experts

I'm "driving" Word 2003 from my VB6 app, using OLE Automation. I'm successfully replacing specific text items in Word docs with other bits of text (values I pull from a database). However, how do I repalce a specific bit of text with a hyperlink? I know how to set the properties of a hyperlink, it's creating it in the first place I have a problem with. How do I get a suitable object to use for the Anchor parameter of the Document.Hyperlinks.Add method?

I'm trying this, where mobjDoc is a Word document object and "%ApplicantOnlineTimesheetsLink%" is the piece of text I want to find and replace:

    With mobjDoc
      .Range.Find.ClearFormatting 'to ensure we're not specifying formatting in our search criteria
      If .Range.Find.Execute(Findtext:="%ApplicantOnlineTimesheetsLink%", _
                            MatchCase:=False, _
                            MatchWholeWord:=False, _
                            MatchWildcards:=False, _
                            MatchSoundsLike:=False, _
                            MatchAllWordForms:=False, _
                            Forward:=True, _
                            Wrap:=wdFindContinue, _
                            Format:=False) Then
        .Hyperlinks.Add Address:="http://www.myurl.com/", Anchor:=mobjDoc.Range
      End If
    End With

This replaces everything in the document with hyperlinks, not just the piece of text I want to replace.



Answer : From my VB6 app, how do I replace specific text in a Word 2003 document with a hyperlink?

Oops I didn't specify what type of hyperlink in the Dim statement
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
Sub ReplaceWithHyperlink(mobjDoc As Document)
Dim rng As Word.Range
Dim hyp As Word.Hyperlink

Set rng = mobjDoc.Range
  Do While rng.Find.Execute("%ApplicantOnlineTimesheetsLink%")
       Set hyp = mobjDoc.Hyperlinks.Add(rng, "http://www.myurl.com/")
        Set rng = hyp.Range
        rng.Collapse wdCollapseEnd 'make sure that the new hyperlink text is not included in the search
        rng.End = mobjDoc.Range.End 'extend the Find range to the end of the document
   Loop
End Sub
Random Solutions  
 
programming4us programming4us