Reputation: 1
Trying to create a simple replace function in a word document and create a new file with the saved information to keep the template
keep getting the error of "object doesn't support this property or method" when running the saveas function
Public Sub WordFindAndReplace()
Dim ws As Worksheet, msWord As Object
Set ws = ActiveSheet
Set msWord = CreateObject("Word.Application")
With msWord
.Visible = True
.Documents.Open "C:\test\PSNS_Letter.docx"
.Activate
With .ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "MM1 Billy Budd"
.Replacement.Text = ws.Range("C1").Value2
.Forward = True
.Wrap = 1 'wdFindContinue (WdFindWrap Enumeration)
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=2 'wdReplaceAll (WdReplace Enumeration)
End With
msWord.SaveAs ("c:\test\test.docx")
'.Quit SaveChanges:=True
End With
End Sub
Upvotes: 0
Views: 155
Reputation: 42236
Try, please
msWord.ActiveDocument.SaveAs ("c:\test\test.docx")
or, even better:
msWord.Documents("PSNS_Letter.docx").SaveAs ("c:\test\test.docx")
Upvotes: 1