Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems using ActiveDocument.Content.Find

Status
Not open for further replies.

monak

MIS
Jul 2, 2002
22
US
I am doing a string search within a open Word Document. I know string exist in the document but my comparison is telling me that it can not find it. Could someone shed some light as to what I could be doing wrong?


Private Sub InsertSymbol(RedStrikeText)

With ActiveDocument.Content.Find
.ClearFormatting
.Text = RedStrikeText
.Execute
If .Found = True Then
With .Replacement
.ClearFormatting
Dim NewRedStrikeText As String
NewRedStrikeText = "[&" & RedStrikeText & "&]"
.Text = NewRedStrikeText
End With
.Execute Replace:=wdReplaceAll
End If
End With

End Sub
 
svfb,

How did you test it? Because I have this code embedded into a macro.


Mona
 
1.) I copied your post into Word.
2.) Since your sub InsertSymbol is expecting a variable to be passed to it. I created a subroutine that called it:
Private Sub ChangeRedStrikeText()
Dim ChangeMe As String

ChangeMe = "comparison"
Call InsertSymbol(ChangeMe)
End Sub

3.) Then I ran the macro ChangeRedStrikeText

After I ran it I had:
...I know string exist in the document but my [&comparison&] is telling me that ...

If that wasn't the output you expected, then could you post what you did expect?
 
sfvb,

This is exactly what I had expected. I must be running the script wrong. I am new to VBA. Could you tell me how are you running the script? Currently I am running it in debug mode with a breakpt and pressing the button that looks like a play button on your VCR.

Mona
 
sfvb,


I just tested it the way you did and still output hasn't come out this way. This time I just pressed F5, no breakpt and nothing. What am I doing wrong?

Mona
 
I'm not sure. But just to verify that we're both doing the same thing, could you try this?

Copy the following into the Word document.
This is a comparison.

Copy this Subroutine. (It's the same as before but without the "Private" keyword.
Sub ChangeRedStrikeText()
Dim ChangeMe As String

ChangeMe = "comparison"
Call InsertSymbol(ChangeMe)
End Sub


Then from Word, press Alt+F8 (this is the shortcut to bring up the Macros pop-up window), and run ChangeRedStrikeText.

When I run it, I get:
This is a [&comparison&].

Does this work on your system?
 
sfvb ,


I have tried it exactly like you told me and I still don't get it.


Mona
 
Thanks sfvb for all your help. I think it is a network problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top