Seite 1 von 1
Suchen und Ersetzen in Word - hat das jemand schon gemacht ?
Verfasst: Mi, 22. Nov 2017 8:44
von brandelh
Hi,
ich will suchen und ersetzen in Word per ActiveX steuern, das ist das Macro, aber wie übersetzt man das ?
Code: Alles auswählen
Sub Makro3()
'
' Makro3 Makro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "{Inhalt1}"
.Replacement.Text = "Test"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
hat das schon jemand gemacht und kann mir Beispiel code geben ?
Das Alaska Beispiel selbst ersetzt Bookmarks, die habe ich aber nicht.
Re: Suchen und Ersetzen in Word - hat das jemand schon gemacht ?
Verfasst: Mi, 22. Nov 2017 8:57
von brandelh
Ich habe das mal so übersetzt, aber spätestens bei Execute weiß ich nicht wie wir das übergeben
Code: Alles auswählen
function WordReplace(oWord,cVon,cNach)
local oSel
oSel := oWord:selection
oSel:Find:ClearFormatting
oSel:Find:Replacement:ClearFormatting
oSel:Find:Text := cVon
oSel:Find:Replacement:Text := cNach
oSel:Find:Forward = True
oSel:Find:Wrap = wdFindContinue
oSel:Find:Format = False
oSel:Find:MatchCase = False
oSel:Find:MatchWholeWord = False
oSel:Find:MatchWildcards = False
oSel:Find:MatchSoundsLike = False
oSel:Find:MatchAllWordForms = False
oSel:Find:Execute Replace:=wdReplaceAll
return nil
Re: Suchen und Ersetzen in Word - hat das jemand schon gemacht ?
Verfasst: Mi, 22. Nov 2017 9:50
von brandelh
Hi,
im Alaska Forum habe ich einen LINK auf die VBA Hilfe von Word 2003 gefunden, dort kann man die nötigen Parameter von EXECUTE() nachlesen.
Damit habe ich das hier so übersetzt und es funktioniert:
Code: Alles auswählen
#include "activex.ch"
#include "word2010.ch"
//////////////////////////////////////////////////////////////////////
// Main()-Prozedur der Anwendung
//////////////////////////////////////////////////////////////////////
PROCEDURE main
LOCAL cDir,cFile, cSaveAs
LOCAL oWord,oDoc, aReplace
set charset to ansi
aReplace := { { "{Inhalt1}" , "Test-Dokument" } ,;
{ "Suchen" , "Ersetzen" } }
set alternate to Ersetzen-LOG.TXT
set alternate on
// Ermitteln des voll-qualifizierten Pfades
// oder fix vorgeben
cDir := CurDrive()+":\"+CurDir()
cFile := cDir+"\Test.docx"
cSaveAs := cDir+"\Test1.docx"
// Erzeugen einer ActiveX-Komponente
oWord := CreateObject("Word.Application")
IF Empty( oWord )
MsgBox( "Microsoft Word ist nicht installiert" )
ENDIF
oWord:visible := .T.
// Oeffnen eines Word-Dokuments
oWord:documents:open( cFile )
oDoc := oWord:ActiveDocument
aEval( aReplace, {|aR| WordReplace(oWord,aR[1],aR[2]) } )
// Speichern des Ergebnisses
IF(ValType(cSaveAs)=="C")
oDoc:saveas(cSaveAs)
ENDIF
/*
IF(ValType(lPrint)=="L" .AND. lPrint)
oDoc:PrintOut()
ENDIF
*/
// Schliessen des Dokuments und zerstoeren das
// ActiveX-Objektes.
oDoc:close()
oWord:Quit()
oWord:destroy()
? "Dokument", cSaveAs, "erzeugt"
set alternate to
inkey(20)
RETURN
function WordReplace(oWord,cVon,cNach)
local oSel, lOK
oSel := oWord:selection
oSel:Find:ClearFormatting()
oSel:Find:Replacement:ClearFormatting()
// aus der Word2003 Hilfedatei stammt die Parameterzeile
lOK := oSel:Find:Execute( cVon,; // FindText
.f.,; // MatchCase
.f.,; // MatchWholeWord
.f.,; // MatchWildcards
.f.,; // MatchSoundsLike
.f.,; // MatchAllWordForms
.t.,; // Forward
wdFindContinue,; // Wrap
.f.,; // Format
cNach,; // ReplaceWith
wdReplaceAll,; // Replace
.f.,; // MatchKashida
.f.,; // MatchDiacritics
.f.,; // MatchAlefHamza
.f. ) // MatchControl
? "ersetze",cVon,cNach, lOK
return lOK
Re: Suchen und Ersetzen in Word - hat das jemand schon gemacht ?
Verfasst: Mi, 22. Nov 2017 9:57
von brandelh
Und wenn man weiß, dass man ein FIND OBJEKT braucht, das eine EXECUTE METHODE hat, dann findet an sogar auf der deutschen Online Hilfe passende Syntax Infos.
https://msdn.microsoft.com/de-de/vba/wo ... ethod-word
Re: Suchen und Ersetzen in Word - hat das jemand schon gemacht ?
Verfasst: Di, 16. Jan 2018 9:30
von brandelh
Mit einer Google Suche habe ich diesen Beitrag von TOM gefunden, der passt auch zum Thema:
https://xbaseforum.de/viewtopic.php?f=50&t=2963