Bootstrap

VBA代码-批量替换多个word文档中的内容,并详细标注

Sub 批量替换内容()

    Dim myDialog As FileDialog, oDoc As Document
	Dim oFile As Variant
     '定义文件夹选取对话框
	Set myDialog = Application.FileDialog(msoFileDialogFilePicker)


    'Selection.Find.ClearFormatting
    'Selection.Find.Replacement.ClearFormatting
	
    With myDialog
	 .Filters.Clear  
	 .Filters.Clear  '清除所有文件筛选器中的项目,确保所有的条件都显示
		
	 '增加筛选器的项目为所有Word文件,添加文件类型的项目位置,该参数从1开始
     .Filters.Add "所有Word文件", "*.doc,*.docx", 1  
     .AllowMultiSelect = True   '允许多项选择
		
     If .Show = -1 Then   '确定   显示对话框,等待用户操作,-1 为用户进行了确认操作
	   For Each oFile In .SelectedItems   '在所有选取项目中循环
        '对单个文档进行处理
	    Set oDoc = Word.Documents.Open(FileName:=oFile, Visible:=False) 
		With oDoc.content.Find
		.ClearFormatting
        .Text = "信息科"
        .Replacement.Text = "XXX部门"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
		'Find 方法的一个参数,用于执行查找并全部替换操作,也可以定义只替换第一个
        .Execute Replace:=wdReplaceAll 
	    End With
	 oDoc.Save
     oDoc.Close
	    Next
	 End If
	 MsgBox "所有文档处理完成"
    End With

;