【发布时间】:2015-09-10 17:04:28
【问题描述】:
我发现这个线程与我的问题相同,但我已将代码复制到我的项目中,但它似乎不起作用。
VBA macro: replace text in word file in all sub folders
我正在单步执行代码,它到达第 32 行(在 colSubFolders 中的 For Each varItem 下),但随后它直接跳过查找/替换部分到代码末尾。是我的文件格式有问题吗?
编辑:此外,当我到达 ln 31 中的 varitem 时,“varitem”的值是文件夹的名称,而不是文件夹中单词文档的名称:我认为这就是问题所在。
Sub DoLangesNow()
Dim file
Dim path As String
Dim strFolder As String
Dim strSubFolder As String
Dim strFile As String
Dim colSubFolders As New Collection
Dim varItem As Variant
' Parent folder including trailing backslash
'YOU MUST EDIT THIS.
strFolder = "L:\Admin\Corporate Books\2015\2014 Consents macro\company Annual Consents"
' Loop through the subfolders and fill Collection object
strSubFolder = Dir(strFolder & "*", vbDirectory)
Do While Not strSubFolder = ""
Select Case strSubFolder
Case ".", ".."
' Current folder or parent folder - ignore
Case Else
' Add to collection
colSubFolders.Add Item:=strSubFolder, Key:=strSubFolder
End Select
' On to the next one
strSubFolder = Dir
Loop
' Loop through the collection
For Each varItem In colSubFolders
' Loop through word docs in subfolder
'YOU MUST EDIT THIS if you want to change the files extension
strFile = Dir(strFolder & varItem & "\" & "*.doc")
Do While strFile <> ""
Set file = Documents.Open(FileName:=strFolder & _
varItem & "\" & strFile)
【问题讨论】: