(MSWord VBA) Add " symbol to search string
I'm trying to get a simple macro to work in MS word. The macro is supposed find a definition in contracts. These are typically marked within quotes (e.g. "Definition"). Hence, I want to select a word that I want to search the definition for and execute a search for the selected term in quotes.
However, for some reason, I can't get it to work reliably. I've gone through the code debugging it, but the MySearchTrim variable ends up just containing
""selectiontext
while I would need it to be
"selectiontext"
I've tried it with inserting the quotes by adding the quotes through
&"""
but it only worked in 30% of the cases, which I find very confusing.
Can anyone help me spot the error?
Thanks!
Sub GehZuDefinition()
Dim MySearchterm
Dim MySearchTrim As String
Dim myWindow As Window
MySearchterm = Selection.Text
MySearchTrim = Chr(34) & Trim(MySearchterm) & Chr(34) ' trimming spaces after searchterm that are typically selected
Selection.Find.ClearFormatting
With Selection.Find
.Text = MySearchTrim
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
End Sub
Comments
Post a Comment