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 / October 2005

Tip: Looking for answers? Try searching our database.

Find and Replace

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Casey Mac - 05 Oct 2005 05:28 GMT
Greetings.

I have some code that is going in a contiunious loop.  

I want to find some text
Replace it with a hyperlink
Then change the display name of the link

But what happens is that if the display name is the same as the link i want
to find it keeps going around in circles.

If there is something that can be done to it i would really appreciated it .

Thanks

Sub FindAndReplaceWithHypertext()
    Dim rngReplace As Word.Range
    Dim rngFound As Word.Range
    Dim szFindTerm As String
    Dim szReplaceHL As String
    Dim szTexttoDP As String
   
   szFindTerm = InputBox("Enter the word you wish to replace with a
hyperlink:")
   szReplaceHL = InputBox("Enter Adobe File Name:")
   szTexttoDP = InputBox("Enter HyperLink Display Name")

    Set rngReplace = ActiveDocument.Content
    With rngReplace.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = szFindTerm
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = False

        ' Find all occurrences in the document
        Do While .Execute

            ' Create and use a totally independent range object
            Set rngFound = rngReplace.Duplicate

            ' Replace sought text with hyperlink

           
            rngFound.Text = vbNullString
            ActiveDocument.Hyperlinks.Add rngFound, szReplaceHL, _
              TextToDisplay:=szTexttoDP

            ' Resume the search after the text that we just found
            rngReplace.Collapse wdCollapseEnd
        Loop
    End With
End Sub
Helmut Weber - 05 Oct 2005 12:29 GMT
Hi Casey,

I can't remember that I ever needed a duplicate of a range.
Seems to me, it overcomplicates things.

Can't write an essay on how to handle ranges,
right here and now.

But have a look at the line I marked.

I changed variables name to what I am used to.

Sub FindAndReplaceWithHypertext()
Dim rRpl As Range     ' range replace
Dim rFnd As Range     ' range found
Dim sFnd As String    ' string to be found
Dim sHpl As String    ' hyperlink
Dim sDsp As String    ' hyperlink display
   
sFnd = "August"
sHpl = "c:\test\test.doc"
sDsp = "August"

Set rRpl = ActiveDocument.Range
ResetSearch
With rRpl.Find
  .Text = sFnd
  .Wrap = wdFindStop
   Do While .Execute
      Set rFnd = rRpl.Duplicate
      rFnd.Text = vbNullString
      ActiveDocument.Hyperlinks.Add _
      rFnd, sHpl, TextToDisplay:=sDsp
      rRpl.Select ' <<<<<<<<<<<<<<<<<<<<<<<
      rRpl.Collapse wdCollapseEnd
    Loop
End With
ResetSearch
End Sub

And this is the way I would do it:

Sub FindAndReplaceWithHypertextImproved()
Dim rDoc As Range     ' range replace
Dim sFnd As String    ' string to be found
Dim sHpl As String    ' hyperlink
Dim sDsp As String    ' hyperlink display
   
sFnd = "August"
sHpl = "c:\test\test.doc"
sDsp = "August"

Set rDoc = ActiveDocument.Range
ResetSearch
With rDoc.Find
  .Text = sFnd
  .Wrap = wdFindStop
   Do While .Execute
      ActiveDocument.Hyperlinks.Add _
      rDoc, sHpl, TextToDisplay:=sDsp
      rDoc.End = rDoc.End + Len(sDsp) ' !!!
      rDoc.Collapse wdCollapseEnd     ' !!!
      ' be the master of the range
    Loop
End With
End Sub
' ---
Public Sub ResetSearch()
With Selection.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = ""
  .Replacement.Text = ""
  .Forward = True
  .Wrap = wdFindContinue
  .Format = False
  .MatchCase = False
  .MatchWholeWord = False
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = False
  .Execute
End With
End Sub

HTH

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

Casey Mac - 05 Oct 2005 12:52 GMT
Thankyou Helmut!

That is perfect!  Im just learning VBA and after looking at your code i can
see that my "hack" job was a total mess.

Thanks again

Cheers

PS i love the web!

> Hi Casey,
>
[quoted text clipped - 82 lines]
>
> HTH
 
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



©2008 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.