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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing a word in word using a function

Status
Not open for further replies.

Chodie

Technical User
Jan 28, 2013
3
0
0
US
I've got a pretty simple function that I'm having trouble executing. I want to take a Business Name and replace default variables found in a MS Word template. Eg: Replace "Default Business Name" with "Callahan Auto Parts".

When I try it outside of a function using the following code, it works without any trouble:
With objWord.Selection.Find
.ClearFormatting
.Text = "Default Business Name"
.Replacement.ClearFormatting
.Replacement.Text = "Callahan Auto Parts"
.Forward = True
.Wrap = wdFindContinue
.Execute ,,,,,,,,,,wdReplaceAll
End With


When I try putting it into a function (so I can do it with many more words), it does not replace the word - only highlights the first instance of the word.



Here's the script:
dim BusinessName
Dim objWord

BusinessName = Inputbox("What is the business' name?")
openword
replaceword "DafaultBusinessName", BusinessName

function replaceword (original, replacement)
With objWord.Selection.Find
.ClearFormatting
.Text = original
.Replacement.ClearFormatting
.Replacement.Text = replacement
.Forward = True
.Wrap = wdFindContinue
.Execute ,,,,,,,,,,wdReplaceAll
End With
end function

function openword
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Open("C:...\Script2013.doc")
end function





It's really odd because it works totally fine if I'm not using the fuction. Any help getting the function to work would be greately appreciated!
 
replaceword "Dafault[highlight] [/highlight]Business[highlight] [/highlight]Name", BusinessName

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks for the correction PHV! Unfortunately, it still won't replace the default entry in word.
 
I'd add this line near the top of your script:
Const wdFindContinue = 1, wdReplaceAll = 2

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
That fixed it!! Thank you so much!!!!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top