close but need some help with form

C

Casey Mac

Greetings.

I would like for a user to input a word into a form and then a second form
appears that asks what to link to. I have (i think) the code so that if a
word in this case "ability" is found it will replace all occurances of
"ablity" with a hyperlink (c:\test.txt). What i need is to populate those
variables with what a user inputs.

Please help

Thanks in advance.


Sub FindAndReplaceWithHypertext()
Dim rngReplace As Word.Range
Dim rngFound As Word.Range
Dim szFindTerm As String

szFindTerm = InputBox("Enter the word you wish to replace with a
hyperlink:")

Set rngReplace = ActiveDocument.Content
With rngReplace.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "ability"
.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, "c:\test.txt", _
TextToDisplay:="test"

' Resume the search after the text that we just found
rngReplace.Collapse wdCollapseEnd
Loop
End With
End Sub
 
C

Casey Mac

okay i found part of my answer but now im really stumped. My code works
only if my text that im trying to find and my display text is different.
Otherwise it goes in an infinate loop.

Thoughts?



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
 

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