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. The makro should then just jump to the definition. (I'll add a part later to jump back to the reading positon, but that part works fine).
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
""selectiontest
This is what the variable content looks like
while I would need it to be
"selectiontest"
I've tried it with inserting the quotes by adding the quotes through
&"""
or through
"([" & ChrW(8220) & "-" & ChrW(8221) & _
"])" & MySearchTrim & "([" & ChrW(8220) & "-" & ChrW(8221) &"])"
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
Your code only searches for terms in plain quotes. And, in any event can be both simplified and made more efficient. For example:
Sub GehZuDefinition()
Application.ScreenUpdating = False
Options.DefaultHighlightColorIndex = wdBrightGreen
With ActiveDocument.Range.Find
.ClearFormatting
.Text = """" & Trim(Selection.Text) & """"
.Replacement.Text = "^&"
.Replacement.Highlight = True
.Format = True
.Forward = True
.MatchCase = True
.MatchSoundsLike = False
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub
Since you don't say what you want to do with the found terms, the above code simply highlights them in green.
Related
I can find the words starting with a capital letter using the following command
Selection.Find.Text = "<([A-Z][0-9A-Z\a-z]{1,15})>"
What exactly I'm struggling with is to take each word one by one and finding similar words in the document first one word and find all duplicates and next second word. I hope I had expressed what I'm trying to create.
Any help is much appreciated
Your questions is poorly worded and, hence, it is difficult to understand what you're trying to achieve. Try:
Sub Demo()
Application.ScreenUpdating = False
Options.DefaultHighlightColorIndex = wdBrightGreen
Dim StrFnd As String, i As Long
StrFnd = "|"
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "<[A-Z][A-Za-z]#>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
End With
Do While .Find.Execute
If InStr(StrFnd, "|" & .Text & "|") = 0 Then StrFnd = StrFnd & .Text & "|"
.Collapse wdCollapseEnd
Loop
With .Find
.Wrap = wdFindContinue
.MatchCase = False
.MatchWildcards = False
.MatchWholeWord = True
.Replacement.Highlight = True
For i = 0 To UBound(Split(StrFnd, "|"))
.Text = Split(StrFnd, "|")(i)
.Replacement.Text = "^&"
.Execute Replace:=wdReplaceAll
Next
End With
End With
Application.ScreenUpdating = True
End Sub
I am trying to find all instances of strings "^=" or "=^" or "^+" and replace them by " - " (space-hyphen-space). So far I have tried using the Regex code from another one of the stackoverflow posts but I am really struggling and trying to match expressions. Please find my attempts and share how I can write VB macro code to automate the process. Thank you for your help!
With Selection.Find
.Text = "^+"
.Replacement.Text = " -- "
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
You probably don't need a macro for that. Try a wildcard Find/Replace where:
Find = [^94=][^94=+]
Replace = ^32-^32
To lengthen the hyphenation, you could use and of:
Replace = ^32--^32
Replace = ^32^=^32; or
Replace = ^32^+^32,
the latter two of which change the hyphen to an en-dash or em-dash, respectively.
If you want to keep the hyphenation together with whatever precedes it, change the first ^32 to ^s.
Or, as a macro:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[^94=][^94=+]"
.Replacement.Text = " - "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End With
Application.ScreenUpdating = True
End Sub
As with manual wildcard Find/Replace you could lengthen the hyphenation with any of:
.Replacement.Text = " -- "
.Replacement.Text = " ^= "
.Replacement.Text = " ^+ "
Again, if you want to keep the hyphenation together with whatever precedes it, change the first replacement space in the code to ^s.
I have a document that has plain text and I want to do some preformatting before I move it to Access. Currently, I'm in Word trying to separate the formatting into titles and text. The document has hundreds of titles and after each title small explanation text (it's an error book with explanations for one machine).
I am trying to put a unique string at the end of a lane that starts with "start-of-title" unique string.
I want to make a macro that finds that string, then goes to the end of the lane and writes " end-of-title" and do that till there are results found.
What I've done so far, and works once, is the following:
Sub test3()
Selection.Find.ClearFormatting
With Selection.Find
.Text = "startoftitle "
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.EndKey Unit:=wdLine
Selection.TypeText Text:=" endoftitle"
End Sub
I've tried doing loops, but sadly I wasn't able to do the right syntax. The problem is that I can't make it loop till there are no results found...
This should do it
Sub test3()
Const STARTTOKEN = "startoftitle "
Const ENDTOKEN = " endoftitle"
For i = 1 To ThisDocument.Paragraphs.Count
If ThisDocument.Paragraphs(i).Range.Style = "Title" _
And Left(ThisDocument.Paragraphs(i).Range.Text, Len(STARTTOKEN)) <> STARTTOKEN Then
ThisDocument.Paragraphs(i).Range.Text = STARTTOKEN & ThisDocument.Paragraphs(i).Range.Text & ENDTOKEN
End If
Next i
End Sub
I managed to solve it before checking what you've written. Thank you for your help!
Here is the code for anyone with the same problem:
Sub test3()
'
' test3 Macro
'
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = " startoftitle "
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute = True
Selection.EndKey Unit:=wdLine
Selection.TypeText Text:=" endoftitle"
Loop
End Sub
I'm trying to create a macro in word to find particular cells in a table and replace certain strings there. For example:
(word1 or word2 or word3).ab,ti.
Should be replaced by
word1[TIAB] or word2[TIAB] or word3[TIAB]
So, what I've done so far is a simple replaceAll to delete the initial brackets and replace the suffix ").ab,ti." by "[TIAB]. But that doesn't append the endings to word1 and word2, of course.
Sub Makro6()
'
' Makro6 Makro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ".ab,ti."
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
I guess what I need is to embed a loop in the replaceAll sub, which runs from the first position to the end of the current row and replaces the string " or " by "[TIAB] or ". However, I'm completely new to VBA so I somehow can't figure out how to do this. Any suggestions?
Thanks for your help!
Leni
This code performs the actions you want:
Sub Makro6()
Dim maxCount, curCount As Integer
maxCount = 3
curCount = 0
Do
curCount = curCount + 1
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = Chr(40) & "word" & curCount & Chr(41) & ".ab,ti."
.Replacement.Text = "word" & curCount & "[TIAB]"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With
Loop While (curCount < maxCount)
End Sub
Note that I had to rely on ASCII codes (Chr(40) & Chr(41)) to account for the parenthesis because surprisingly (at least, for me), the macro wasn't able to find the target string. I did some tests and the problem only happens with parenthesis followed by another character (?!).
I am trying to remove all the whitespace before and after all table objects in my word document.
This is the code I have so far:
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^p^p"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
This seems to delete some new line breaks but not all of them. After I have run this macro, when I click to Show all hidden characters it still shows some of the |P tags.
Try change
.Text = "^p^p"
to
.Text = Chr(32)
To find out which characters to replace, select your text and run the following macro.
Sub DebugAscCode()
For i = 1 To Len(Selection.Text)
Debug.Print Asc(Mid(Selection.Text, i))
Next i
End Sub
Using replace on VBA
Sub ReplaceAscCode()
Dim tmpArray As Variant
tmpArray = Array(7, 13, 32)
For j = LBound(tmpArray) To UBound(tmpArray)
Selection.Text = Replace(Selection.Text, ASC(tmpArray(j)), "")
Next j
End Sub
I've used in my work and it works perfectly this way, each character will always be represented by an ASC code
[]'s