Reputation: 23
I'm trying to input my Excel results into a pre made Word document. I want to use a macro for this. I'm fairly new to the code writing game but I understand the basics. I tried to copy from different courses but none of them works.
This simple macro should change 'x1' into 'anything', but all it does for me is open the document and select the x1 in the document.
Does anyone know where I go wrong?
Private Sub CommandButton1_Click()
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("C:\Users\mycomputer\Documents\PremadeDocument.docx")
wrdApp.Selection.Find.ClearFormatting
wrdApp.Selection.Find.Replacement.ClearFormatting
With wrdApp.Selection.Find
.Text = "x1"
.Replacement.Text = "anything"
' .Forward = True
.Wrap = wdFindContinue
' .Format = False
' .MatchCase = False
' .MatchWholeWord = False
' .MatchWildcards = False
' .MatchSoundsLike = False
' .MatchAllWordForms = False
End With
wrdApp.Selection.Find.Execute Replace:=wdReplaceAll
Set wrdDoc = Nothing
Set wrdApp = Nothing
End Sub
Upvotes: 2
Views: 2283
Reputation: 3898
Add Word Enumerated Const value for wdReplaceAll
Const wdReplaceAll = 2
After the Macro definition
Private Sub CommandButton1_Click()
Const wdReplaceAll = 2
Upvotes: 3