2

I have a variable (strLastname) that I use to send a string to a bookmark. That works well. I also wish to use that variable to replace temporary text "Name>" in a long document.

This is what I have now.

  Sub cmdOK_Click()
    Dim strLastname As String   ' from dialogue box "BoxLastname" field
    strLastname = BoxLastname.Value
....
  End sub

The macro that does not work:

Sub ClientName()

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Text = "Name>"
    .Replacement.Text = strLastname??????

             'Selection.TypeText (strLastname) ????
              'How to use the variable from the Dialogue Box - strLastname????

     End With
  Selection.Find.Execute Replace:=wdReplaceAll
End Sub

I tried .Replacement.Text = strLastname and .Replacement.Text = BoxLastname.Value but no one work.

1 Answer 1

3

A quick search from google finds this link http://msdn.microsoft.com/en-us/library/office/aa211953(v=office.11).aspx

I tried this on a simple document to find and replace text

With ActiveDocument.Content.Find
    .Forward = True
    .Wrap = wdFindStop
    .Execute FindText:="Text", ReplaceWith:="Tax", Replace:=wdReplaceAll

That replaces all occurence of Text with Tax .... is this what you're after ??

Works with variables too

OldWord = "VAT"
NewWord = "Text"

With ActiveDocument.Content.Find
    .Forward = True
    .Wrap = wdFindStop
    .Execute FindText:=OldWord, ReplaceWith:=NewWord, Replace:=wdReplaceAll, Matchcase:=True
End With

Add , Matchcase:=True at end to fix case problem (modified now above)

3rd Modification results in this

Dim strLastname As String   ' from dialogue box "BoxLastname" field
strLastname = BoxLastname.Value

OldWord = "Name"
NewWord = strLastName

With ActiveDocument.Content.Find
    .Forward = True
    .Wrap = wdFindStop
    .Execute FindText:=OldWord, ReplaceWith:=NewWord, Replace:=wdReplaceAll, Matchcase:=True
End With
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks. My goal is to replace several occurence of a temporary word by a variable taken fron a dialogue box where the Clien's name typed in. This avriable is use to send the string to a bookmark ans thats works well. But how to use this variable (strLastname) to replace the temporary text "Name>" in my very long document?
modified answer for variables ... they went to uppercase for me ... checking that now
I think thats the answer but if you've more clarifications needed just ask, if not and we're sorted please tick the answer
OldWord = strLastname NewWord = "Name" in sample code above
Thanks guys, you are very nice. This is the code I have now and still does not work.Sub ClientName() OldWord = "Name>" NewWord = strLastname With ActiveDocument.Content.Find .Forward = True .Wrap = wdFindStop .Execute FindText:=OldWord, ReplaceWith:=NewWord, Replace:=wdReplaceAll End With End Sub >>>>I still cannot understand why the variable StrLastname can not be "re-use" to do a Find/Replace. The variable is properley declared: Sub cmdOK_Click() Dim strLastname As String ' from dialogue box "BoxLastname" field strLastname = BoxLastname.Value .... End sub
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.