简体   繁体   中英

Open with Excel VBA a Word document, add some data and just save it (the Word document)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM