简体   繁体   English

如何在VBScript中保存活动的“ Word”文档?

[英]How to save the active “Word” document in VBScript?

Here I have a small VBS script that helps me append a new line to a table in MS "Word" 2003: 在这里,我有一个小的VBS脚本,可以帮助我在MS“ Word” 2003中向表中添加新行:

Set wd = CreateObject("Word.Application")

wd.Visible = True

Set doc = wd.Documents.Open ("c:\addtotable.doc")

Set r = doc.Tables(1).Rows.Add

aa = Split("turtle,dog,rooster,maple", ",")

For i = 0 To r.Cells.Count - 1
  r.Cells(i + 1).Range.Text = aa(i)
Next

It works fine, but it doesn't save anything. 它工作正常,但不会节省任何费用。 I want it to save the performed changes. 我希望它保存执行的更改。

By the method of macro-recording in the "Word" I got this macro command that saves active "Word" document: 通过在“ Word”中进行宏记录的方法,我得到了此宏命令,用于保存活动的“ Word”文档:

ActiveDocument.Save

So, I decided to append this macro to the VBS script above: 因此,我决定将此宏附加到上面的VBS脚本中:

 Set wd = CreateObject("Word.Application")

    wd.Visible = True

    Set doc = wd.Documents.Open ("c:\addtotable.doc")

    Set r = doc.Tables(1).Rows.Add

    aa = Split("turtle,dog,rooster,maple", ",")

    For i = 0 To r.Cells.Count - 1
      r.Cells(i + 1).Range.Text = aa(i)
    Next

     ActiveDocument.Save

But it doesn't save anything. 但这并不能节省任何东西。 What am I doing wrong here? 我在这里做错了什么?

Have you already tried calling doc.Save after making those changes? 进行这些更改后,您是否已经尝试过调用doc.Save If that doesn't work: 如果那不起作用:

The issue is that ActiveDocument doesn't automatically reference what you think it does in VBScript the way it does in Word's VBA. 问题在于ActiveDocument不会像Word在VBA中那样自动引用您认为在VBScript中所做的事情。

Try setting a new variable to the active document, like so: 尝试为活动文档设置一个新变量,如下所示:

Dim activeDoc
Set activeDoc = wd.ActiveDocument
activeDoc.Save

I think you have to use ActiveDocument.SaveAs("C:\\addtotable.doc"); 我认为您必须使用ActiveDocument.SaveAs("C:\\addtotable.doc"); because I can't find any documentation for .Save . 因为我找不到.Save任何文档。 SaveAs accepts a second parameter which specifies what format to save it in. Pastebin of the parameters here . SaveAs接受第二个参数,该参数指定将其保存为哪种格式。 这里的参数的粘贴。

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

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