用vbscript打开word文档并保存到另一个文件夹
我有工作代码,可以使用 vbscript 以编程方式打开 Word 文档,进行更改,然后保存同一文档并关闭同一文档。
是否可以做同样的事情,但将文档保存到另一个文件夹并关闭两个单词实例?我稍微修改了原来的代码,但它只关闭了保存的word文档的第二个实例。
显然,Word 认为如果我保存到另一个文件夹,它需要该文档的 2 个实例。这是我的一些原始代码:
Set objDoc = objWord.Documents.Open(objFile.Path)
'modify the document.
etc...
objDoc.SaveAs objFSO.BuildPath(strDirectory, objFSO.GetFileName(objFile.Path))
objDoc.Close
这对于一个文档来说效果很好。然后我所做的一切更改就是这样,添加另一个要写入的目录而不是原始目录:
objDoc.SaveAs objFSO.BuildPath(strDirectory & saveDir, objFSO.GetFileName(objFile.Path))
最后我打开了两个相同的Word文档,当我关闭正在保存的一个时,另一个保持打开状态。
有没有一种简单的方法可以做到这一点?
谢谢,
詹姆斯
I have working code that opens up a word document programmatically using vbscript, makes a change, then saves that same document and closes that same document.
Is it possible to do the same thing, but save the document to another folder and close out both instances of word? I slightly modified my original code, but it only closed out the second instance of the word document that was saved.
Apparently, word thinks if I'm saving to another folder it needs 2 instances of the document. This is some of my original code:
Set objDoc = objWord.Documents.Open(objFile.Path)
'modify the document.
etc...
objDoc.SaveAs objFSO.BuildPath(strDirectory, objFSO.GetFileName(objFile.Path))
objDoc.Close
this works fine for one document. Then all I changed was this, adding another directory to write to instead of the original:
objDoc.SaveAs objFSO.BuildPath(strDirectory & saveDir, objFSO.GetFileName(objFile.Path))
and I ended up with two identical word documents opened and when I closed the one I was saving, the other stayed open.
Is there a simple way to do this?
Thanks,
James
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最好将 Word 文档保存在原始文件夹中,然后编写进一步的代码将其移动到另一个位置,可能使用 FileSystemObject.MoveFile。不要依赖 Word 的“另存为”功能来执行此操作 - 正如您所观察到的,您会产生令人讨厌的副作用。
It may be better to save the word document in the original folder and then write further code to move it into another location, perhaps using
FileSystemObject.MoveFile
. Don't rely on Word's "Save-as" functionality to do this - as you have observed you get a nasty side-effect.