在这里,我有一个小的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
它工作正常,但它不保存任何东西 . 我希望它保存执行的更改 .
通过“Word”中的宏录制方法,我得到了这个保存活动“Word”文档的宏命令:
ActiveDocument.Save
所以,我决定将这个宏附加到上面的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
但它没有保存任何东西 . 我在这做错了什么?
2 回答
在进行这些更改后,您是否已尝试调用
doc.Save
?如果这不起作用:问题是
ActiveDocument
没有't automatically reference what you think it does in VBScript the way it does in Word'的VBA .尝试将新变量设置为活动文档,如下所示:
我认为你必须使用
ActiveDocument.SaveAs("C:\addtotable.doc");
因为我找不到.Save
的任何文档 .SaveAs
接受第二个参数,该参数指定要将其保存的格式 . 参数 here 的Pastebin .