I'm opening a preexisting word document with Excel VBA and add some data to it and then I just want to save this file. But when I run the Excel VBA macro the word "save as" window appears.
And that's not my aim.
The word document should just be saved... (like when you add some data to the word document by hand and then press CTRL S)
If more details are needed let me know... And many thanks for your help in advance.
Now my code:
Dim appWord As Word.Application
Dim document As Word.Document
Set appWord = CreateObject("Word.Application")
' the excel file and the word document are in the same folder
Set document = appWord.Documents.Add( _
ThisWorkbook.Path & "\Testfile.docx")
' adding the needed data to the word file
...
' in this following line of code I tried to do the correct saving... but it opens the "save as" window - I just want to save it automatically
document.Close wdSaveChanges = -1
'Close our instance of Microsoft Word
appWord.Quit
'Release the external variables from the memory
Set document = Nothing
Set appWord = Nothing
CodePudding user response:
Set document = appWord.Documents.Add( _
ThisWorkbook.Path & "\Testfile.docx")
You’re not opening a document with this code, you’re creating a new, unsaved, document from an existing one. Change it to:
Set document = appWord.Documents.Open( _
ThisWorkbook.Path & "\Testfile.docx")
You’re also missing a colon.
document.Close wdSaveChanges:=-1