Ich arbeite an einem größeren Projekt in Excel VBA und festgestellt, das folgende Problem. Ich verwende VBA, damit Excel eine Vorlage in MS Word kompiliert. Ich wollte bestimmte Sätze in der Vorlage gelb hervorheben. Früher hat es funktioniert, aber dann hat es aufgehört, ohne ersichtlichen Grund. Wenn ich jetzt den Code ausführe, wird der Teil ohne Fehler ausgeführt, aber die Wörter werden nicht markiert, obwohl ich sicher bin, dass sie in der Vorlage vorhanden sind. Ich habe debug.print verwendet, um die Werte des .replacement.highlight-Wertes herauszufinden, und es zeigt 9999999 im Immediate-Fenster und -1, wenn ich den Mauszeiger über den Ausdruck im Unterbrechungsmodus halte. Das entsprechende Fragment des Codes. Replacement.highlight = True in MS Word VBA funktioniert nicht
ist unten angegeben:
Dim WdApp As Word.Application
Set WdApp = New Word.Application
With WdApp
.ActiveDocument.Select
With .Selection.Find
'the code creating the document and writing the template continues here
.ClearFormatting
.Text = "{Optional: Please also confirm the terms of transactions and other key information (for example: rights of return, allowances and rebates, special agreements, payment terms, incoterms, etc.) which may affect the accounting for the transactions.}"
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
.ClearFormatting
.Text = PlaceholderAdditionalInfoRequest
.Replacement.Text = .Text
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
.ClearFormatting
.Text = "<<<@@@Client's [email protected]@@>>>"
.Replacement.Text = .Text
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
.ClearFormatting
.Text = "{Optional: A statement of account with the above invoices marked is attached. ALTERNATIVELY: Copies of the above invoices are attached.}"
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
End With
End With
Hat jemand wissen, was los ist? Ich würde mich über alle Hinweise freuen, vor allem, wenn sie in einfachem Englisch ausgedrückt werden :) Ich bin neu in VBA und habe keine formelle Ausbildung in Programmierung.
Mit freundlichen Grüßen,
Tyro
Ich habe Ihren Code nicht ausprobiert, aber haben Sie versucht, ". Replacement.ClearFormatting" sowie .ClearFormatting vor dem Einrichten. Replacement.Highlight? (Nur für den Fall, dass etwas "klebt", wenn es nicht sollte) –