简体   繁体   English

打开Word文档,并使用vbscript将其保存到另一个文件夹

[英]Open a word document and save it to another folder with vbscript

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. 我的工作代码可以使用vbscript以编程方式打开Word文档,进行更改,然后保存该相同文档并关闭该相同文档。

Is it possible to do the same thing, but save the document to another folder and close out both instances of word? 是否可以做同样的事情,但是将文档保存到另一个文件夹并关闭两个word实例? I slightly modified my original code, but it only closed out the second instance of the word document that was saved. 我稍稍修改了原始代码,但只关闭了保存的word文档的第二个实例。

Apparently, word thinks if I'm saving to another folder it needs 2 instances of the document. 显然,Word认为如果我要保存到另一个文件夹,它需要2个文档实例。 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. 最后我打开了两个相同的word文档,当我关闭保存的一个word文档时,另一个保持打开状态。

Is there a simple way to do this? 有没有简单的方法可以做到这一点?

Thanks, 谢谢,

James 詹姆士

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 . 最好将Word文档保存在原始文件夹中,然后编写其他代码以将其移动到另一个位置,也许使用FileSystemObject.MoveFile Don't rely on Word's "Save-as" functionality to do this - as you have observed you get a nasty side-effect. 不要依赖Word的“另存为”功能来执行此操作-正如您所看到的那样,您会收到讨厌的副作用。

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

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