Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 替换所有子文件夹中word文件中的文本_Vba_Replace_Ms Word - Fatal编程技术网

Vba 替换所有子文件夹中word文件中的文本

Vba 替换所有子文件夹中word文件中的文本,vba,replace,ms-word,Vba,Replace,Ms Word,我正在组装一个VBA宏,它: 1.读取文件夹 2.创建其所有子文件夹的集合 3.在所有子文件夹中循环并找到任何以.doc结尾的word文档 4.在每个.doc文件中:替换一点文本并保存,然后关闭文档 此宏无法正常工作:它不会替换子文件夹中任何word文档中的文本。 它实际上没有打开任何word文档,我不确定它是应该一个接一个地打开每个word文档,还是在后台运行 Sub DoLangesNow() Dim file Dim path As String Dim strFolder As Stri

我正在组装一个VBA宏,它:

1.读取文件夹

2.创建其所有子文件夹的集合

3.在所有子文件夹中循环并找到任何以.doc结尾的word文档

4.在每个.doc文件中:替换一点文本并保存,然后关闭文档

此宏无法正常工作:它不会替换子文件夹中任何word文档中的文本。 它实际上没有打开任何word文档,我不确定它是应该一个接一个地打开每个word文档,还是在后台运行

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 = "G:\2009\09771\Design\ESD\Commercial Tower KSD1\Green Star As Built\Round 1 Submission - Draft\02. Indoor Environment Quality"

     ' 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)

' Start of macro 1replace text GS-XXXAB  with GS-1624AB
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Application.WindowState = wdWindowStateNormal
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "GS-XXXAB "
        .Replacement.Text = "GS-1624AB "
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
' End of macro 1
' Saves the file
ActiveDocument.Save
ActiveDocument.Close
' set file to next in Dir
strFile = Dir
         Loop
     Next varItem
 End Sub
Sub-DoLangesNow()
暗文件
将路径设置为字符串
作为字符串的Dim strFolder
将strSubFolder设置为字符串
作为字符串的Dim strFile
将子文件夹设置为新集合
作为变体的Dim varItem
'包含尾部反斜杠的父文件夹
'您必须编辑此。
strFolder=“G:\2009\09771\Design\ESD\Commercial Tower KSD1\Green Star竣工\Round 1提交-草稿\02.室内环境质量”
'循环遍历子文件夹并填充集合对象
strSubFolder=Dir(strFolder&“*”,vbDirectory)
不使用strSubFolder=“”时执行
选择案例strSubFolder
案例“……”
'当前文件夹或父文件夹-忽略
其他情况
'添加到集合中
colSubFolders.Add项:=strSubFolder,键:=strSubFolder
结束选择
“继续下一个
strSubFolder=Dir
环
'循环浏览集合
对于子文件夹中的每个变量
'循环浏览子文件夹中的word文档
'如果要更改文件扩展名,必须编辑此文件
strFile=Dir(strFolder&varItem&“*.doc”)
当strFile“”时执行
Set file=Documents.Open(文件名:=strFolder&_
变量(&“\”&strFile)
'宏1的开始用GS-1624AB替换文本GS-XXXAB
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
选择。查找
.Text=“”
.Replacement.Text=“”
.Forward=True
.Wrap=wdFindContinue
.Format=False
.MatchCase=False
.MatchWholeWord=False
.MatchWildcards=False
.MatchSoundsLike=False
.MatchAllWordForms=False
以
Application.WindowState=wdwindowstateformal
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
选择。查找
.Text=“GS-XXXAB”
.Replacement.Text=“GS-1624AB”
.Forward=True
.Wrap=wdFindContinue
.Format=False
.MatchCase=False
.MatchWholeWord=False
.MatchWildcards=False
.MatchSoundsLike=False
.MatchAllWordForms=False
以
Selection.Find.Execute Replace:=wdReplaceAll
'宏1的结尾
'保存文件
ActiveDocument.Save
ActiveDocument。关闭
'将文件设置为目录中的下一个
strFile=Dir
环
下一个变量
端接头

您的代码对我有效,但有以下更改

strFile=Dir(strFolder&varItem&“*.doc”) 到 strFile=Dir(strFolder&varItem&“\”和“*.doc”)

  • 确保
    strFolder
    变量中文件夹的路径正确。例如(
    strFolder=“C:\Users\Santosh\Desktop\tes\
    ”)
strFile = Dir(strFolder & varItem & "*.doc") strFile = Dir(strFolder & varItem & "\" & "*.doc")