简体   繁体   English

VbScript 保存当前打开的Word文档

[英]VbScript to save a current open Word document

hi I want vbscript to save a current open Word document, I am using the code:嗨,我想要 vbscript 保存当前打开的 Word 文档,我正在使用代码:

 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.但它正在打开一个新的word文档,然后要求我保存。

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. objWord.Documents.Add 表示创建一个新文档。

Try: objDoc = objWord.ActiveDocument试试: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.不要使用 VBScript,而是使用 VBA 宏,它将在进程中启动。 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.使用“ Application ” object 将授予您访问当前 Word 实例的权限,而ActiveDocumentApplication.ActiveDocument的缩写)将允许您访问当前文档。

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.您必须填写 rest 并初始化变量,但这是工作代码。

HTH,高温下,

James詹姆士

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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