简体   繁体   中英

VbScript to save a current open Word document

hi I want vbscript to save a current open Word document, I am using the code:

 Dim objWord As Object
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add()
    objDoc.Save

but it is opening a new word document and then asking me to save.

My requirement is i have created a menu button on click of it if the doc is already saved at a location it should save the changes made or if it is not it should ask me the path and save it there

Thanks Creator

objWord.Documents.Add means creating a new document.

Try: objDoc = objWord.ActiveDocument

Try adding the following code:

Dim activeDoc
Set activeDoc = objWord.ActiveDocument
activeDoc.Save

Instead of using VBScript, use a VBA macro, which will be started in-process. Using the " Application " object will grant you access to the current Word instance, and ActiveDocument (which is short for Application.ActiveDocument ) will give you access to the current document.

I had to do the same thing and this is what I used:

' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Check for directory folder.
If objFSO.FolderExists(strDirectory) Then
        Set objFolder = objFSO.GetFolder(strDirectory)
        Set objWord = CreateObject("Word.Application")
        objWord.Visible = True

        Set objDoc = objWord.Documents.Open(objFile.Path)
        objDoc.SaveAs objFSO.BuildPath(strDirectory, objFSO.GetFileName(objFile.Path))
        objDoc.Close
etc...

You will have to fill in the rest and init the variables, but this is working code.

HTH,

James

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