Help with Prompts = Code attached Simple if statement?

C

Casey Mac

hello,

What im wanting to try is for this code to cycle through the document and
once you have imputted your criteria:

sFnd = Enter the word you wish to replace with a hyperlink
sHpl = Enter File Name
sDsp = Enter the HyperLink display name

it will prompt you once it finds the word you wish to replace. At that
point you can choose either yes (to replace it) or no to ignore it.

Im thinking this must be a simple if statement but for the life of me i cant
find where it should go. I have a partial piece "MsgBox "Replace selection",
vbYesNo, "Testing" but cant figure out the rest.

Please help. i would like to try and sleep tonight.

Thanks

cm


Sub FindAndReplaceWithHypertext()
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 = InputBox("Enter the word you wish to replace with a hyperlink:")
sHpl = InputBox("Enter File Name:")
sDsp = InputBox("Enter the HyperLink display name")


Set rDoc = ActiveDocument.Range
ResetSearch
With rDoc.Find
.Text = sFnd
.Wrap = wdFindStop

MsgBox "Replace selection", vbYesNo, "Testing"
Do While .Execute
ActiveDocument.Hyperlinks.Add _
rDoc, sHpl, TextToDisplay:=sDsp
rDoc.End = rDoc.End + Len(sDsp)
rDoc.Collapse wdCollapseEnd
Loop
End With

End Sub
 
D

David Kavanagh

I think the following is probably what you need:

Sub FindAndReplaceWithHypertext()
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 = InputBox("Enter the word you wish to replace with a hyperlink:")
sHpl = InputBox("Enter File Name:")
sDsp = InputBox("Enter the HyperLink display name")


Set rDoc = ActiveDocument.Range
ResetSearch
With rDoc.Find
.Text = sFnd
.Wrap = wdFindStop

Do While .Execute
rDoc.Select
If MsgBox("Replace selection?", vbYesNo, "Testing") = vbYes Then
ActiveDocument.Hyperlinks.Add _
rDoc, sHpl, TextToDisplay:=sDsp
rDoc.End = rDoc.End + Len(sDsp) ' I'm not convinced you need
this line
rDoc.Collapse wdCollapseEnd
End If
Loop
End With

Regards,
David.
 

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