Word-Makro: Wie werden Zeichenfolgen in einem Text ersetzt, basierend auf einer 2-Spaltentabelle?

4512
Paulie

Ich habe also eine Reihe von Zeichenfolgen, die ich in Word-Dokumenten suchen und ersetzen möchte.

Meine Zeichenfolgen werden als zweispaltige Tabelle angezeigt. Ich würde die Tabelle wahrscheinlich mit Word erstellen und dann manuell in TXT oder CSV oder was auch immer funktionieren, konvertieren. Ich brauche diesen Teil nicht zu automatisieren. Die Tabelle würde ungefähr so ​​aussehen:

English word French word dog chien cat chat London Londres 

Du hast die Idee...

Ich brauche ein Makro, das die in der ersten Spalte enthaltenen Begriffe durchsucht und durch den Inhalt der zweiten Spalte ersetzt.

Kannst du helfen? Vielen Dank!

3

2 Antworten auf die Frage

1
Raystafarian
Selection.Find.ClearFormatting With Selection.Find .Text = "English Word" .Replacement.Text = "French Word" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute 

Erstellen Sie eine für jedes Wort, aber Ihre Tabelle wird nicht gelesen

1
horatio

I took the liberty of borrowing Raystafarian's snippet and expanding on it. You would change the line filename="c:\folder\file.txt with the path and filename of a plain text file, where the contents are a word or phrase, followed by a tab, followed by a word or phrase, followed by end of line. I didn't bother with error checking, so if you don't have an end of line on the last line, the text for that line will not be replaced. Also if you don't include a tab on each line, this macro will probably break. Back up your word document to facilitate easy reversion.

Sub Macro1() Dim ff As Long Dim x As Long Dim filename As String Dim buffer As String Dim charbuffer As String * 1 filename = "c:\folder\file.txt" ff = FreeFile Open filename For Binary As ff buffer = "" charbuffer = "" For x = 1 To LOF(ff) Get #ff,, charbuffer If charbuffer <> vbCr And charbuffer <> vbLf Then buffer = buffer & charbuffer Else If buffer <> "" Then processBuffer buffer buffer = "" End If Next x Close ff End Sub Sub processBuffer(buffer As String) Dim varArray As Variant varArray = Split(buffer, vbTab) makeReplacements varArray(0), varArray(1) End Sub Sub makeReplacements(ByVal strToReplace As String, ByVal strReplacement As String) 'MsgBox strToReplace & " will be replaced by " & strReplacement Selection.Find.ClearFormatting With Selection.Find .Text = strToReplace .Replacement.Text = strReplacement .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute End Sub 
Beachten Sie, dass Sie in Ihrem Textdokument keine Tabellenüberschrift (dh "englisches Wort") enthalten möchten. horatio vor 12 Jahren 0