Beiträge von water
-
-
-
Zu Punkt 2:
In Array $aIgnore die zu ignorierenden Strings erfassen. ACHTUNG: Wenn einer der Strings gefunden wird, dann wird keine Ersetzung in dieser Zeile durchgeführt!
C
Alles anzeigen#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"], _ ["Maier","Müller"] _ ] ; For code with the following strings no replacement will be done at all Global $aIgnore[] = [ _ 'ActiveDocument.Bookmarks("Amtsleiter")"', _ '.UpdateFieldsAtPrint = True' _ ] Global $sTemplate = @ScriptDir & "\I-Brief" ; Zu verarbeitende Word Vorlagedatei ; ********** Änderungen nur VOR dieser Zeile ********** Global $sExtension = ".dotm" Global $iTotalProjectCount = 0, $iTotalComponentCount = 0, $iTotalReplacementCount = 0 ; 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 _VBAModify($sTemplate) Exit Func _VBAModify($sTemplate) ; Open the Document Global $oDoc = _Word_DocOpen($oWord, $sTemplate & $sExtension) 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 -
-
-
Kleine Anpassung.
Nun kannst Du in einem Durchlauf mehrere Strings ersetzen lassen.C
Alles anzeigen#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"], _ ["Maier","Müller"] _ ] ; Global $sSearch = "Tester" ; Wonach soll gesucht werden? ; Global $sReplace = "Master" ; Wodurch soll es ersetzt werden? Global $sTemplate = @ScriptDir & "\I-Brief" ; Zu verarbeitende Word Vorlagedatei ; ********** Änderungen nur VOR dieser Zeile ********** Global $sExtension = ".dotm" Global $iTotalProjectCount = 0, $iTotalComponentCount = 0, $iTotalReplacementCount = 0 ; 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 _VBAModify($sTemplate) Exit Func _VBAModify($sTemplate) ; Open the Document Global $oDoc = _Word_DocOpen($oWord, $sTemplate & $sExtension) 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 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 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 -
-
-
Habe das Skript aktualisiert. Nun werden alle Projects und Components eines Dokumentes verarbeitet und entsprechende Statistiken ausgegeben:
AutoIt
Alles anzeigen#include <Word.au3> #include <MsgBoxConstants.au3> ; ********** Änderungen nur NACH dieser Zeile ********** Global $sSearch = "Tester" ; Wonach soll gesucht werden? Global $sReplace = "Master" ; Wodurch soll es ersetzt werden? Global $sTemplate = @ScriptDir & "\I-Brief" ; Zu verarbeitende Word Vorlagedatei ; ********** Änderungen nur VOR dieser Zeile ********** Global $sExtension = ".dotm" Global $iTotalProjectCount = 0, $iTotalComponentCount = 0, $iTotalReplacementCount = 0 ; 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 _VBAModify($sTemplate) Exit Func _VBAModify($sTemplate) ; Open the Document Global $oDoc = _Word_DocOpen($oWord, $sTemplate & $sExtension) 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 If StringInStr(.Lines($i, 1), $sSearch) Then .ReplaceLine($i, StringReplace(.Lines($i, 1), $sSearch, $sReplace)) $iReplaceCount = $iReplaceCount + 1 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 -
Ich habe mir das nochmals angeschaut.
Wenn "I-Brief.dotm" ident mit dem Dokument ist, das Du mir per PM gesendet hast, dann kann ich das Ergebnis nachvollziehen.Ich habe in meinem Beispielskript das Project "TemplateProject" und darin die Componente "AutoNew" verarbeitet. Es gibt dort kein "Tester" das ersetzt werden könnte.
Aber in einem der Dialgn Forms habe ich "Tester" gefunden.
Soll die Ersetzung daher in allen Projects und Components (Macro, GUI ...) des Dokumentes durchgeführt werden?
Code
Alles anzeigenPROCESSING: ...\I-Brief.dotm STATISTICS ========== VBProject: Normal, Typ: 100 (vbext_pt_HostProject - Host project) VBComponent: ThisDocument, Typ: 100 (vbext_ct_Document - Document Module) VBComponent: NewMacros, Typ: 1 (vbext_ct_StdModule - Standard 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) -
-
-
-
Beispielscript für das konkrete Problem.
- Es müssen nur mehr die markierten Variablen eingesetzt werden
- Die Originaldateien werden nicht überschrieben
- Im Ursprungsverzeichnis wird die geänderte Datei als Dateiname-NEW.dotm gespeichert
- Für die Stapelverarbeitung muss nun nur noch mit _FileListToArray ein Array der zu verarbeitenden Dateien erstellt und für jede Datei dann die Funktion mit dem Dateinamen als Parameter aufgerufen werden
AutoIt
Alles anzeigen#include <Word.au3> #include <MsgBoxConstants.au3> ; ********** Änderungen nur NACH dieser Zeile ********** Global $sSearch = "True" ; Wonach soll gesucht werden? Global $sReplace = "FALSE" ; Wodurch soll es ersetzt werden? Global $sTemplate = @ScriptDir & "\I-Brief" ; Zu verarbeitende Word Vorlagedatei ; ********** Änderungen nur VOR dieser Zeile ********** Global $sExtension = ".dotm" ; 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 _VBAModify($sTemplate) Exit Func _VBAModify($sTemplate) ; Open the Document Global $oDoc = _Word_DocOpen($oWord, $sTemplate & $sExtension) If @error Then Exit MsgBox($MB_ICONERROR, "Error", "_Word_DocOpen: @error=" & @error & ", @extended=" & @extended) ; Open TemplateProject $oVBProject = $oWord.VBE.VBProjects("TemplateProject") If @error Then Exit MsgBox($MB_ICONERROR, "Error", "VBProjects: @error=" & @error & ", @extended=" & @extended) ; Open AutoNew VBComponent $oVBComponent = $oVBProject.VBComponents("AutoNew") If @error Then Exit MsgBox($MB_ICONERROR, "Error", "VBComponents: @error=" & @error & ", @extended=" & @extended) ; Modify line by line $oCodeModule = $oVBComponent.CodeModule If @error Then Exit MsgBox($MB_ICONERROR, "Error", "CodeModule: @error=" & @error & ", @extended=" & @extended) With $oCodeModule For $i = 1 To .CountOfLines If StringInStr(.Lines($i, 1), $sSearch) Then .ReplaceLine($i, StringReplace(.Lines($i, 1), $sSearch, $sReplace)) Next EndWith ; 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 -
Vielleicht hilft die PrintOut Methode des Worksheet / Range Objektes: https://docs.microsoft.com/en-us/office/v….range.printout
Ob sich mit der Druckausgabe in eine Datei (z.B. Output.PDF) eine PDF-Datei erstellen lässt oder ob ein (virtueller) PDF-Drucker eingerichtet werden muss, kann ich leider nicht sagen.
-
Beispielskript. Jetzt musst Du nur noch die - aus Deiner Sicht - notwendigen Änderungen einfügen.
- Listet die Projekte mit Name und Typ
- Listet die Komponenten von Projekt "TemplateProject" mit Name und Typ
- Öffnet das "AutoNew" Macro
- Schreibt den Inhalt des "AutoNew" Macros Zeile für Zeile auf die Konsole
- Ändert das "AutoNew" Macro (ersetzen, löschen, einfügen von Zeilen)
- Schreibt den Inhalt des "AutoNew" Macros inkl. aller Änderungen als einen einzigen String auf die Konsole
C
Alles anzeigen#include <Word.au3> #include <Array.au3> #include <MsgBoxConstants.au3> Global $oWord = _Word_Create() Global $oDoc = _Word_DocAdd($oWord, Default, @ScriptDir & "\I-Brief-Sec.dotm") ; List Projects For $oVBProject In $oWord.VBE.VBProjects ConsoleWrite("VBProject: " & $oVBProject.Name & " - " & $oVBProject.Type & @CRLF) Next ; Open TemplateProject $oVBProject = $oWord.VBE.VBProjects("TemplateProject") ; List components in a Project For $oVBComponent In $oVBProject.VBComponents ConsoleWrite("VBComponent: " & $oVBComponent.Name & " - " & $oVBComponent.Type & @CRLF) Next ; Open AutoNew VBComponent $oVBComponent = $oVBProject.VBComponents("AutoNew") ; Write all Codelines line by line $oCodeModule = $oVBComponent.CodeModule ConsoleWrite(@CRLF & @CRLF) For $i = 1 To $oCodeModule.CountOfLines ConsoleWrite("Code line by line: " & $oCodeModule.Lines($i, 1) & @CRLF) Next ; Modify the CodeModule With $oCodeModule .ReplaceLine (1 , "Sub Test()") ; Change the name of the function .DeleteLines (6, 2) ; Delete lines 6 and 7 ("ShowFieldCodes = True" and ".ShowBookmarks = False") .InsertLines (2 , "' TEST") ; Insert text at line 2 EndWith ; Write all Codelines as a single string $sResult = $oCodeModule.Lines(1, $oCodeModule.CountOfLines) ConsoleWrite(@CRLF & "Code as a single string: " & @CRLF & $sResult & @CRLF) -
Meinem Verständnis nach befinden sich die zu ändernden Informationen nicht im Inhalt des Dokumentes das ich im Objekt Modell als Range ansprechen kann, sondern im VBProject Objekt.
Daher ist es für die "normale" Word find & replace nicht erreichbar.
VBProject ist ein die Office Produkte überspannendes Objekt, daher wird in der Word Objekt Referenz auch nicht darauf eingegangen. Es wird in einer eigenen Objekt Referenz abgehandelt (siehe oben).
Mit einem Beispieldokument können wir dann meine Theorie überprüfen
-
Um sicherzugehen, dass wir alle dasselbe Verständnis des Problems/der Anforderung haben (siehe Post #1), fasse ich meine Sicht kurz zusammen.
- Wenn Vorlagen aufgerufen werden, dann wird auch VBA Code aus dieser Vorlage ausgeführt.
- DIe Aufgabe besteht nun darin, Teile dieses VBA Codes zu ersetzen (Name von Vorgesetzten, Orgeinheiten etc.)
- Es geht also nicht darum, Text im Word Dokument zu ersetzen und auch nicht um die Anpassung von Dokument-Eigenschaften
- Ist meine Sichtweise korrekt?
- Ein Beispieldokument als Spielwiese wäre natürlich hilfreich
-
-
In der Excel UDF sind einige - aber eben nicht alle - Excel Konstanten definiert.
Wenn das Skript Fehler auswirft, dass eine/beide Konstanten "undefined" ist/sind, dann findest Du die Werte hier:xlSourceRange bzw. xlHtmlStatic.