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!
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!