Hallo water,
da $sExtension auf ".dotm" gesetzt ist. Das allein ist noch kein gültiger Filter.
Tausend Dank für den bahnbrechenden Hinweis. Ich hatte mal wieder Tomaten auf den Augen und klar den Stern im Filter vor der Dateiendung vergessen. Als Stuttgarter ist das unverzeihlich
.
Anbei das angepasste Script mit der rekursiven Dateiverarbeitung:
#include <Array.au3>
#include <File.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <Word.au3>
#include <WordConstants.au3>
#include <Word.au3>
#include <MsgBoxConstants.au3>
; ********** Änderungen nur NACH dieser Zeile **********
; Table with search and replace strings to process
Global $aSearchReplace[][] = [ _
["Tester","Master"], _
["True","False"], _
["Amtsleiter","Amtsleitung"] _
]
; For code with the following strings no replacement will be done at all
Global $aIgnore[] = [ _
'ActiveDocument.Bookmarks("Amtsleiter")"', _
'.UpdateFieldsAtPrint = True' _
]
; ********** Änderungen nur VOR dieser Zeile **********
Global $sExtension = ".dotm"
Global $sTemplate = _FileListToArrayRec(@ScriptDir, "*" & $sExtension, $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_SORT, $FLTAR_FULLPATH) ; Zu verarbeitende Word Vorlagedateien
Global $iTotalProjectCount = 0, $iTotalComponentCount = 0, $iTotalReplacementCount = 0
_ArrayDisplay($sTemplate, "");Ansicht der zu verarbeitenden Dateien anzeigen lassen (optional)
; Start the MS Word application
Global $oWord = _Word_Create()
If @error Then Exit MsgBox($MB_ICONERROR, "Error", "_Word_Create: @error=" & @error & ", @extended=" & @extended)
; Process the Template
For $i = 1 to $sTemplate[0] ; rekursives Ermitteln der zu verarbeitenden Dateien
_VBAModify($sTemplate[$i])
Next
Exit
Func _VBAModify($sTemplate)
; Open the Document
Local $oDoc = _Word_DocOpen($oWord, $sTemplate)
If @error Then Exit MsgBox($MB_ICONERROR, "Error", "_Word_DocOpen: @error=" & @error & ", @extended=" & @extended)
; Collect statistics and output them to the Console
ConsoleWrite("PROCESSING: " & $sTemplate & $sExtension & @CRLF)
ConsoleWrite(" STATISTICS" & @CRLF & " ==========" & @CRLF)
For $oVBProject In $oWord.VBE.VBProjects
ConsoleWrite(" VBProject: " & $oVBProject.Name & ", Typ: " & $oVBProject.Type)
Switch $oVBProject.Type
Case 100
ConsoleWrite(" (vbext_pt_HostProject - Host project)" & @CRLF)
Case 101
ConsoleWrite(" (vbext_pt_StandAlone - Standalone Project)" & @CRLF)
Case Else
ConsoleWrite(" Unknown Project Type!" & @CRLF)
EndSwitch
For $oVBComponent In $oVBProject.VBComponents
ConsoleWrite(" VBComponent: " & $oVBComponent.Name & ", Typ: " & $oVBComponent.Type)
Switch $oVBComponent.Type
Case 1
ConsoleWrite(" (vbext_ct_StdModule - Standard module)" & @CRLF)
Case 2
ConsoleWrite(" (vbext_ct_ClassModule - Class module)" & @CRLF)
Case 3
ConsoleWrite(" (vbext_ct_MSForm - Microsoft Form)" & @CRLF)
Case 11
ConsoleWrite(" (vbext_ct_ActiveXDesigner - ActiveX Designer)" & @CRLF)
Case 100
ConsoleWrite(" (vbext_ct_Document - Document Module)" & @CRLF)
Case Else
ConsoleWrite(" Unknown Component Type!" & @CRLF)
EndSwitch
Next
Next
; Process all VBProjects and VBComponents
ConsoleWrite(" SEARCH & REPLACE" & @CRLF & " ================" & @CRLF)
For $oVBProject In $oWord.VBE.VBProjects
ConsoleWrite(" VBProject: " & $oVBProject.Name & @CRLF)
$iTotalProjectCount += 1
For $oVBComponent In $oVBProject.VBComponents
ConsoleWrite(" VBComponent: " & $oVBComponent.Name & @CRLF)
$iTotalComponentCount += 1
Switch $oVBComponent.Type
; Only Standard Modules and Microsoft Forms will be processed
Case 1, 3
$oCodeModule = $oVBComponent.CodeModule
If @error Then Exit MsgBox($MB_ICONERROR, "Error", "CodeModule: @error=" & @error & ", @extended=" & @extended)
With $oCodeModule
$iReplaceCount = 0
For $i = 1 To .CountOfLines
$bIgnore = False
For $j = 0 To UBound($aIgnore, 1) - 1
If StringInStr(.Lines($i, 1), $aIgnore[$j]) Then $bIgnore = True
Next
If $bIgnore = False Then
For $j = 0 To UBound($aSearchReplace, 1) - 1
If StringInStr(.Lines($i, 1), $aSearchReplace[$j][0]) Then
.ReplaceLine($i, StringReplace(.Lines($i, 1), $aSearchReplace[$j][0], $aSearchReplace[$j][1]))
$iReplaceCount = $iReplaceCount + 1
EndIf
Next
EndIf
Next
ConsoleWrite(" " & $iReplaceCount & " replacements" & @CRLF)
$iTotalReplacementCount = $iTotalReplacementCount + $iReplaceCount
EndWith
Case Else
ConsoleWrite(" Not processed!" & @CRLF)
EndSwitch
Next
Next
; Send total Statistics for this document to the Console
ConsoleWrite(" SEARCH & REPLACE - TOTAL STATISTICS" & @CRLF & " ===================================" & @CRLF)
ConsoleWrite(" VBProjects processed : " & $iTotalProjectCount & @CRLF)
ConsoleWrite(" VBComponents processed: " & $iTotalComponentCount & @CRLF)
ConsoleWrite(" Replacements done : " & $iTotalReplacementCount & @CRLF)
; Save as new Document
_Word_DocSaveAs($oDoc, $sTemplate & "-NEW" & $sExtension, $WdFormatXMLTemplateMacroEnabled)
If @error Then Exit MsgBox($MB_ICONERROR, "Error", "_Word_DocSaveAs: @error=" & @error & ", @extended=" & Hex(@extended))
; Close Document
_Word_DocClose($oDoc)
EndFunc ;==>_VBAModify
Alles anzeigen
Die rekursive Abarbeitung der Aufgaben funktioniert nun sehr gut.
Hier die Konsolenausgabe bei drei Word-Dokumenten im rekursiven Durchlauf: Es sind inhaltlich drei Dokumente mit demselben Inhalt nur mit verscheidenen Dateinamen. Die Verarbeitung verlief fehlerfrei.
PROCESSING: F:\Test\Word\I-BRIEF.dotm
STATISTICS
==========
VBProject: TemplateProject, Typ: 100 (vbext_pt_HostProject - Host project)
VBComponent: ThisDocument, Typ: 100 (vbext_ct_Document - Document Module)
VBComponent: AutoNew, Typ: 1 (vbext_ct_StdModule - Standard module)
VBComponent: Dialog1, Typ: 3 (vbext_ct_MSForm - Microsoft Form)
VBComponent: Dialog2, Typ: 3 (vbext_ct_MSForm - Microsoft Form)
VBComponent: Drucken, Typ: 1 (vbext_ct_StdModule - Standard module)
VBComponent: Dialog3, Typ: 3 (vbext_ct_MSForm - Microsoft Form)
VBComponent: NewMacros, Typ: 1 (vbext_ct_StdModule - Standard module)
VBProject: Normal, Typ: 100 (vbext_pt_HostProject - Host project)
VBComponent: ThisDocument, Typ: 100 (vbext_ct_Document - Document Module)
SEARCH & REPLACE
================
VBProject: TemplateProject
VBComponent: ThisDocument
Not processed!
VBComponent: AutoNew
8 replacements
VBComponent: Dialog1
19 replacements
VBComponent: Dialog2
1 replacements
VBComponent: Drucken
0 replacements
VBComponent: Dialog3
8 replacements
VBComponent: NewMacros
0 replacements
VBProject: Normal
VBComponent: ThisDocument
Not processed!
SEARCH & REPLACE - TOTAL STATISTICS
===================================
VBProjects processed : 2
VBComponents processed: 8
Replacements done : 36
PROCESSING: F:\Test\Word\I-BRIEF_2.dotm
STATISTICS
==========
VBProject: Normal, Typ: 100 (vbext_pt_HostProject - Host project)
VBComponent: ThisDocument, Typ: 100 (vbext_ct_Document - Document Module)
VBProject: TemplateProject, Typ: 100 (vbext_pt_HostProject - Host project)
VBComponent: ThisDocument, Typ: 100 (vbext_ct_Document - Document Module)
VBComponent: AutoNew, Typ: 1 (vbext_ct_StdModule - Standard module)
VBComponent: Dialog1, Typ: 3 (vbext_ct_MSForm - Microsoft Form)
VBComponent: Dialog2, Typ: 3 (vbext_ct_MSForm - Microsoft Form)
VBComponent: Drucken, Typ: 1 (vbext_ct_StdModule - Standard module)
VBComponent: Dialog3, Typ: 3 (vbext_ct_MSForm - Microsoft Form)
VBComponent: NewMacros, Typ: 1 (vbext_ct_StdModule - Standard module)
SEARCH & REPLACE
================
VBProject: Normal
VBComponent: ThisDocument
Not processed!
VBProject: TemplateProject
VBComponent: ThisDocument
Not processed!
VBComponent: AutoNew
8 replacements
VBComponent: Dialog1
19 replacements
VBComponent: Dialog2
1 replacements
VBComponent: Drucken
0 replacements
VBComponent: Dialog3
8 replacements
VBComponent: NewMacros
0 replacements
SEARCH & REPLACE - TOTAL STATISTICS
===================================
VBProjects processed : 4
VBComponents processed: 16
Replacements done : 72
PROCESSING: F:\Test\Word\I-BRIEF_3.dotm
STATISTICS
==========
VBProject: Normal, Typ: 100 (vbext_pt_HostProject - Host project)
VBComponent: ThisDocument, Typ: 100 (vbext_ct_Document - Document Module)
VBProject: TemplateProject, Typ: 100 (vbext_pt_HostProject - Host project)
VBComponent: ThisDocument, Typ: 100 (vbext_ct_Document - Document Module)
VBComponent: AutoNew, Typ: 1 (vbext_ct_StdModule - Standard module)
VBComponent: Dialog1, Typ: 3 (vbext_ct_MSForm - Microsoft Form)
VBComponent: Dialog2, Typ: 3 (vbext_ct_MSForm - Microsoft Form)
VBComponent: Drucken, Typ: 1 (vbext_ct_StdModule - Standard module)
VBComponent: Dialog3, Typ: 3 (vbext_ct_MSForm - Microsoft Form)
VBComponent: NewMacros, Typ: 1 (vbext_ct_StdModule - Standard module)
SEARCH & REPLACE
================
VBProject: Normal
VBComponent: ThisDocument
Not processed!
VBProject: TemplateProject
VBComponent: ThisDocument
Not processed!
VBComponent: AutoNew
8 replacements
VBComponent: Dialog1
19 replacements
VBComponent: Dialog2
1 replacements
VBComponent: Drucken
0 replacements
VBComponent: Dialog3
8 replacements
VBComponent: NewMacros
0 replacements
SEARCH & REPLACE - TOTAL STATISTICS
===================================
VBProjects processed : 6
VBComponents processed: 24
Replacements done : 108
+>22:19:40 AutoIt3.exe ended.rc:0
+>22:19:40 AutoIt3Wrapper Finished.
>Exit code: 0 Time: 39.78
Alles anzeigen
Die $aSearchReplace hat fabelhaft funktioniert.
Global $aSearchReplace[][] = [ _
["Tester","Master"], _
["True","False"], _
["Amtsleiter","Amtsleitung"] _
]
; For code with the following strings no replacement will be done at all
Global $aIgnore[] = [ _
'ActiveDocument.Bookmarks("Amtsleiter")"', _
'.UpdateFieldsAtPrint = True' _
]
Hingegen wurde bei meinem Test unter $aIgnore das Wort "Amtsleiter" mit "Amtsleitung" trotz Eintrag in $aIgnore überschrieben.
Zu Punkt 1:
Das sollte jetzt schon funktionieren, da die Components vom Type 1 und 3 bearbeitet werden (die rot markierten Zeilen).
Ich habe es mit dem Text ["Sachbearbeiter","Sachbearbeitung"] versucht und da hat es im Dialog 1 (UserForm) nicht geklappt. Das ist aber wie gesagt auch nicht so schlimm, da die Textboxen u. U. sowieso händisch wegen der Textersetzung angepasst werden müssen.