问题描述
限时送ChatGPT账号..我有一些代码要打开,附加一些文本,删除所述文本,然后使用 vbscript 保存并关闭 rtf 文档文件.代码如下:
Set objWord = CreateObject("Word.Application")objWord.Visible = TrueSet objDoc = objWord.Documents.Open("C:\box\test.rtf")设置 objSelection = objWord.SelectionobjSelection.Font.Size = "10"objSelection.InsertAfter Text:="你好"对象选择.删除objDoc.SaveAs ("C:\box\test.rtf")objWord.退出
但是,我需要使用文件列表来执行此操作,所以我认为我必须使用一个 FileSysObject 像:
Set objFSO = CreateObject("Scripting.FileSystemObject")如果 objFSO.FolderExists(strDirectory) 那么设置 objFolder = objFSO.GetFolder(strDirectory)' 检查 strDirectory 文件夹是否存在如果 objFSO.FolderExists(strDirectory) 那么设置 objFolder = objFSO.GetFolder(strDirectory)如果 objFSO.FileExists(strDirectory & strFile) 那么设置 objFolder = objFSO.GetFolder(strDirectory)对于 objFolder.Files 中的每个 objFile设置 objTextFile = objFSO.OpenTextFile _(strDirectory & strFile, ForAppending, True)' 每次运行此 VBScript 时写入 strTextobjTextFile.WriteLine (strText)'objTextFile.WriteLine (strBlankText)objTextFile.Close下一个
等等...请记住,我遗漏了一些初始化变量代码.
有没有办法将这两种方法结合起来解决问题,这样我就可以对文件进行操作并仍然遍历所有文件,还是我做错了?
对此有什么想法吗?
解决方案是的,您可以合并这两种方法 - 只需将 Word 代码移动到循环中,确保在迭代结束时关闭活动文档.我添加了一个 if 来检查当前文件的文件扩展名,以确保 Word 打开正确的文件类型.还有一点改变输出文件的名称,这样你就不会覆盖
Set objWord = CreateObject("Word.Application")objWord.Visible = True对于 objFolder.Files 中的每个 objFile如果 objFSO.GetExtensionName(objFile.path) = "rtf" 然后设置 objDoc = objWord.Documents.Open(objFile.path)设置 objSelection = objWord.SelectionobjSelection.Font.Size = "10"objSelection.InsertAfter Text:="你好"对象选择.删除objDoc.SaveAs objFSO.BuildPath("C:\box", objFSO.GetBaseName(objFile.path) & "-test.rtf")objDoc.Close万一下一个objWord.退出
您还需要包含变量初始化代码,但这应该是一个起点.
文件系统对象参考>
I have some code to open, append some text, delete said text, then save and close an rtf document file with vbscript. Here is the code:
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Open("C:\box\test.rtf")
Set objSelection = objWord.Selection
objSelection.Font.Size = "10"
objSelection.InsertAfter Text:="Hello"
objSelection.Delete
objDoc.SaveAs ("C:\box\test.rtf")
objWord.Quit
However, I need to do this with a list of files, so I thought I had to use a FileSysObject like:
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
' Check that the strDirectory folder exists
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
For Each objFile In objFolder.Files
Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForAppending, True)
' Writes strText every time you run this VBScript
objTextFile.WriteLine (strText)
'objTextFile.WriteLine (strBlankText)
objTextFile.Close
Next
etc... Keep in mind I'm leaving out some of the inititalization variables code.
Is there a way to combine these two approaches to the problem so I can operate on the file and still loop through all the files or am I doing this wrong?
Any ideas on this?
解决方案yes, you can merge the 2 approaches - simply move the Word code into the loop, making sure to close the active document at the end of the iteration. I have added an if to check the file extension of the current file to make sure Word opens the correct file types. Also a bit to change the name of the output file so you don't overwrite
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
For Each objFile In objFolder.Files
if objFSO.GetExtensionName(objFile.path) = "rtf" then
Set objDoc = objWord.Documents.Open(objFile.path)
Set objSelection = objWord.Selection
objSelection.Font.Size = "10"
objSelection.InsertAfter Text:="Hello"
objSelection.Delete
objDoc.SaveAs objFSO.BuildPath("C:\box", objFSO.GetBaseName(objFile.path) & "-test.rtf")
objDoc.Close
end if
Next
objWord.Quit
You'll also need to include your variable initialisation code, but this should be a starting point.
FileSystemObject reference
这篇关于如何使用 vbscript 打开、追加、删除追加和关闭 word rtf 文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论