|
- Private Sub CommandButton1_Click()
- Application.ScreenUpdating = False
- Dim myFile$, myPath$, i%, myDoc As Object, myAPP As Object, txt$, Re_txt$, Sp
- Set myAPP = CreateObject("word.application")
- With Application.FileDialog(msoFileDialogFolderPicker)
- .Title = "选择目标文件夹"
- If .Show = -1 Then
- myPath = .SelectedItems(1)
- Else
- Exit Sub
- End If
- End With
- myPath = myPath & "": myFile = Dir(myPath & "*.doc*")
- txt = InputBox("需要替换的文字:")
- Re_txt = InputBox("替换成:")
- 'myAPP.Visible = True '是否显示打开文档
- Do While myFile <> ""
- Set myDoc = myAPP.Documents.Open(myPath & myFile)
- If myDoc.ProtectionType = wdNoProtection Then '是否受保护
- With myDoc.Content.Find
- .Text = txt
- .Replacement.Text = Re_txt
- .Forward = True
- .Wrap = 2
- .Format = False: .MatchCase = False
- .MatchWholeWord = False
- .MatchByte = True
- .MatchWildcards = False
- .MatchSoundsLike = False
- .MatchAllWordForms = False
- .Execute Replace:=2
- End With
- Rem ------------------------------------------------------------------------------------------------------
- '替换文本框中的文字 |
- For Each Sp In myDoc.Shapes ' |
- If Sp.TextFrame.TextRange.Text Like "*" & txt & "*" Then ' |
- Sp.TextFrame.TextRange.Text = Replace(Sp.TextFrame.TextRange.Text, txt, Re_txt) ' |
- End If ' |
- Next ' |
- '替换结束' |
- Rem ------------------------------------------------------------------------------------------------------
- End If
- myDoc.Save: myDoc.Close
- myFile = Dir
- Loop
- myAPP.Quit '关掉临时进程
- Application.ScreenUpdating = True
- MsgBox ("全部替换完毕!")
- End Sub
复制代码
|
|