0
ほとんどの.docファイルと.docxファイルには、ほとんどの場合に繰り返される単純なテキストを変更したいものがあります。このマクロを使用して複数のファイルやテキストボックス内のテキストを検索し置換するにはどうすればよいですか?
だから、私は現時点では、このマクロの実行を持っています。
Option Explicit
Public Sub BatchReplaceAll()
Dim FirstLoop As Boolean
Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim Response As Long
PathToUse = "C:\Files\"
'Error handler to handle error generated whenever
'the FindReplace dialog is closed
On Error Resume Next
'Close all open documents before beginning
Documents.Close SaveChanges:=wdPromptToSaveChanges
'Boolean expression to test whether first loop
'This is used so that the FindReplace dialog will'only be displayed for the first document
FirstLoop = True
'Set the directory and type of file to batch process
myFile = Dir$(PathToUse & "*.docx")
While myFile <> ""
'Open document
Set myDoc = Documents.Open(PathToUse & myFile)
If FirstLoop Then
'Display dialog on first loop only
Dialogs(wdDialogEditReplace).Show
FirstLoop = False
Response = MsgBox("Do you want to process " & _
"the rest of the files in this folder", vbYesNo)
If Response = vbNo Then Exit Sub
Else
'On subsequent loops (files), a ReplaceAll is
'executed with the original settings and without
'displaying the dialog box again
With Dialogs(wdDialogEditReplace)
.ReplaceAll = 1
.Execute
End With
End If
'Close the modified document after saving changes
myDoc.Close SaveChanges:=wdSaveChanges
'Next file in folder
myFile = Dir$()
Wend
End Sub
あなただけの単純なテキストを交換する場合には、作業を行います。 私はこれを使ってテキストボックス内を検索して置き換えることができますか?
ありがとうございます。
EDIT 1:これで
With Dialogs(wdDialogEditReplace)
.ReplaceAll = 1
.Execute
End With
:
With Dialogs(wdDialogEditReplace)
For Each myStoryRange In ActiveDocument.StoryRanges
With myStoryRange.Find
.Text = "ORIGINAL_TEXT"
.Replacement.Text = "MODIFIED_TEXT"
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Do While Not (myStoryRange.NextStoryRange Is Nothing)
Set myStoryRange = myStoryRange.NextStoryRange
With myStoryRange.Find
.Text = "ORIGINAL_TEXT"
.Replacement.Text = "MODIFIED_TEXT"
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Loop
Next myStoryRange
End With
まあ、私は私がこれを何か(少なくとも今のところは)働い
を変更しましたこの新しいコードの唯一の問題は、いくつかのテキストボックスをスキップし、やや遅いです。何か案は?
私はメインポストを見ています。 – Azuen
明示的に 'TextFrame'を探すために節を置くと、それらはまったくスキップされません。問題は、コードがさらに遅くなり、最終的にプロセスが遅くなることです。ループに追加するすべてのチェックは、反復の間に多くの時間を作成します。私は良いオプティマイザになることは知られていませんが、どちらかと言うと悲しいことです:( – SalvadorVayshun