Fromlostdays1
New Member
- Joined
- Jul 18, 2022
- Messages
- 14
- Office Version
- 365
- Platform
- Windows
As stated, I'm trying to have Excel scan a specific cell for text, and use that text to replace every instance of another text in Word.
I have Excel creating a new Word document based on an existing word template I made per command button and deleting some bookmarks. I can't, however, figure out how to get the find and replace code to work.
For instance, I have a cell in Excel, lets just say A1, wherein I want to be able to fill in a Company name. Lets say, Company, Inc.
On my command button press, I'd like VBA to read what's in A1 (in this example Company, Inc.) and replace every instance of the text "[Company]" that I have in my word template.
This is the code I have so far. It doesn't error out, nothing to Debug, it just doesn't actually do the find and replace in the Word document. Also please note that the Replacement.Text in the below code "This Works!" was just me testing. Again, what I'm really working toward is having the replacement text be read from a cell in Excel.
Again, I can run this and no errors, the Word template opens, but nothing else happens after that. Any help would be appreciated.
Thanks!
Thanks!
I have Excel creating a new Word document based on an existing word template I made per command button and deleting some bookmarks. I can't, however, figure out how to get the find and replace code to work.
For instance, I have a cell in Excel, lets just say A1, wherein I want to be able to fill in a Company name. Lets say, Company, Inc.
On my command button press, I'd like VBA to read what's in A1 (in this example Company, Inc.) and replace every instance of the text "[Company]" that I have in my word template.
This is the code I have so far. It doesn't error out, nothing to Debug, it just doesn't actually do the find and replace in the Word document. Also please note that the Replacement.Text in the below code "This Works!" was just me testing. Again, what I'm really working toward is having the replacement text be read from a cell in Excel.
VBA Code:
Private Sub CommandButton1_Click()
Dim wdapp As Object
Dim wddoc As Object
Dim Path As String
Set wdapp = CreateObject("Word.application")
wdapp.Visible = True
Path = ThisWorkbook.Path & "\leguinst.docx"
Set wddoc = wdapp.Documents.Open(Path)
With wddoc.Content.Find
.Text = "[Company]"
.Replacement.Text = "This Works!"
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
End Sub
Again, I can run this and no errors, the Word template opens, but nothing else happens after that. Any help would be appreciated.
Thanks!
Thanks!