此项目的目标是让此脚本执行以下操作:
1-弹出一个对话框,要求处理工作目录
2-创建一个“text”子目录
3-打开一个word文件,在文件上运行一个word宏,并使用与word文档相同的原始文件名将其保存为文本文件
4-在目录的整个内容中重复步骤3
到目前为止,我已经找到并测试了第3步,保存了文件命名,我认为这将是最难找到的部分(让单词宏无形地运行)但它运行得很好。
过去几周我一直在自学python,然后撞墙试图解决宏观问题,在VBS和bam中尝试过,直接开始工作。但我不知道如何在整个目录中进行递归,或者使用变量获取原始文件名并使用相同的变量重命名。我意识到其中一些是基本问题,并为此道歉 - 请参阅我帮助我学习的文档在这里绝对没问题。我的代码粘贴在下面,以供参考,看看我在做什么。
任何帮助它处理整个目录并保持文件名相同(IE document.doc或.docx成为document.txt)都会很棒!
感谢所有指针!
-
Dim AppWord
Dim OpenDocument
Const docTXT = 2
Set AppWord = CreateObject("Word.Application")
AppWord.Visible = False
Set OpenDocument = AppWord.Documents.Open("C:\test\doc.docx")
AppWord.Run "macro1"
OpenDocument.SaveAs "C:\test\text\docnowtext", docTXT
OpenDocument.Close
Set OpenDocument = Nothing
AppWord.Quit
Set AppWord = Nothing
MsgBox "All done!"
答案 0 :(得分:0)
完成这项工作的关键是你需要:
InputBox
Sub
或Function
.docx
文件时,您都会处理它Subfolder
时,都会将其用于递归例如:
Option Explicit
Sub DoSomethingWithDOCX(docxpath)
Dim fso, file, folder, txtpath
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.GetFile(docxpath)
Set folder = file.ParentFolder
If Not fso.FolderExists(folder + "\text") Then
Call fso.CreateFolder(folder + "\text")
End If
If Right(file.Name, 5) = ".docx" Then
txtpath = folder & "\text" & "\" & Replace(file.Name, ".docx", ".txt")
ElseIf Right(file.Name, 4) = ".doc" Then
txtpath = folder & "\text" & "\" & Replace(file.Name, ".doc", ".txt")
End If
' @@TODO: Do your original code with docxpath and txtpath
End Sub
Sub SearchForDOCX(path)
Dim fso, file, folder, subfolder, txt
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(path)
For Each file in folder.Files
If right(file.Name, 5) = ".docx" Then
Call DoSomethingWithDOCX(path + "\" + file.Name)
ElseIf right(file.Name, 4) = ".doc" Then
Call DoSomethingWithDOCX(path + "\" + file.Name)
End If
Next
'For Each subfolder in folder.SubFOlders
' Call SearchForDOCX(subfolder.Path)
'Next
End Sub
Dim path
path = InputBox("Specify Folder to process")
Call SearchForDOCX(path)