Search for text in Word Endnotes - ms-word

Hello: I am trying to perform either a regular search or wildcard search in Word, specifically in the Endnotes.
Here is what I am searching for:
[Endnote Reference][space][text....][:]
Here is an example of what the endnote might look like:
12 Text in my endnote ending with a colon: More text but not what I want.
So what I want to do is select all the text (plus colon) following the Endnote Reference + space (i.e., Text in my endnote ending with a colon:) and add bold/italics.
I've tried using the advance search where I search for:
^e^?: --> doesn't work (I'd like to make the "any character" a bunch of characters until the : is reached)
Wildcard search does not allow the use of ^e so I tried:
*L --> that gives way too much and then also doesn't work.
Any feedback is much appreciated. I could accomplish this in Perl, but not in Word.
thanks in advance!

You don't need Regex for this. All you need is a wildcard Find, where:
Find = ^2 [!^13]#:
Using VBA for what you're trying to do:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.StoryRanges(wdEndnotesStory)
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^2 [!^13]#:"
.Replacement.Text = ""
.Forward = True
.Format = False
.Wrap = wdFindStop
.MatchWildcards = True
End With
Do While .Find.Execute
.Start = .Endnotes(1).Range.Start
.Style = "MyBoldItalicCharacterStyle"
.Collapse wdCollapseEnd
Loop
End With
Application.ScreenUpdating = True
End Sub

Related

Wild card for end of line in MS Word?

My match pattern has 4 parts :
<1st part><2nd part><3rd part><4th part>
Here,
<1st part> = Fixed string "Fedora"
<2nd part> = A 2 digit number "[0-9][0-9]"
<3rd part> = Followed by a ":" symbol.
<4th part> = one or more strings till the end of the current line.
NOTE : <4th part> ends with the end of current line and contains only alphabets.
I've reached till here :
Fedora[0-9][0-9]?[a-z]*[A-Z]*^l>
But the last part - searching the end of the line - is not yielding the expected result. Note that I'm trying to get the end of the line when Word breaks the line automatically.
Where am I going wrong ?
It seems to me you need:
Find = Fedora[0-9]{2}:*^l
or:
Find = Fedora[0-9]{2}:*[^l^13]
There is no way to use Word's built-in Find to locate the end of a line that's been generated by Word's automatic layout. The only kind of "end-of-line" that can be searched is the manual line break inserted by pressing Shift+Enter. The manual line break corresponds to the special Find character ^l.
If you need to Find and extend to the end of a line then you need to use a macro (VBA). The following sample code does what you need. Please note that with the code as it stands only the last occurrence of the search term will be selected when the macro finishes. You need to build the final result into it that you're looking for.
Or, simply remove the Do While and Loop lines and the macro will find the first term.
Sub FindThenToEndOfLine()
Dim r As Range, rDoc As Word.Range
Dim bFound As Boolean
bFound = True
Set r = ActiveDocument.content
Set rDoc = r.Duplicate
r.Find.ClearFormatting
Do While bFound
With r.Find
.text = "Fedora[0-9][0-9]:[a-z]*[A-Z]*"
.Forward = True
.wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
bFound = .Execute
End With
If bFound Then
r.Select
Selection.EndKey Unit:=wdLine, Extend:=True
End If
Loop
End Sub

Finding text AND fields with variable content in Word

I need to find and delete every occurrence of the following pattern in a Word 2010 document:
RPDIS→ text {INCLUDEPICTURE c:\xxx\xxx.png" \*MERGEFORMAT} text ←RPDIS
Where:
RPDIS→ and ←RPDIS are start and end delimiters
Between the start and end delimiters there can be just text or text and fields with variable content
The * wildcard in the Word Find and Replace dialog box will find the pattern if it contains text only but it will ignore patterns where text is combined with fields. And ^19 will find the field but not the rest of the pattern until the end delimiter.
Can anyone help, please?
Here's a VBA solution. It wildcard searches for RPDIS→*←RPDIS. If the found text contains ^19 (assuming field codes visible; if objects are visible instead of field codes, then the appropriate test is text contains ^01), the found text is deleted. Note that this DOES NOT care about the type of embedded field --- it will delete ANY AND ALL embedded fields that occur between RPDIS→ and ←RPDIS, so use at your own risk. Also, the code has ChrW(8594) and ChrW(8592) to match right-arrow and left-arrow respectively. You may need to change that if your arrows are encoded differently.
Sub test()
Dim wdDoc As Word.Document
Dim r As Word.Range
Dim s As String
' Const c As Integer = 19 ' Works when field codes are visible
Const c As Integer = 1 ' Works when objects are visible
Set wdDoc = ActiveDocument
Set r = wdDoc.Content
With r.Find
.Text = "RPDIS" & ChrW(8594) & "*" & ChrW(8592) & "RPDIS"
.MatchWildcards = True
While .Execute
s = r.Text
If InStr(1, s, chr(c), vbTextCompare) > 0 Then
Debug.Print "Delete: " & s
' r.Delete ' This line commented out for testing; remove comments to actively delete
Else
Debug.Print "Keep: " & s
End If
Wend
End With
End Sub
Hope that helps.

How to change the font color of a string which starts with "$" and end with "F" or " A" in Ms word using a VBA code

I m beginner and trying to learn and understand the codes. currently I m struck with changing the Font color for the string "$1mF" to green and "$1mA" to red in the MS word document. The start and end are constant i.e starts with $ and ends with either A or F. The purpose is that string ending with F denotes a favorable and positive impact(i.e green) and string ending with A denotes unfavorable and negative impact(i.e red) that too bold. I doing it manually as of now. I did try few examples i found on this site and tweaked them a bit as well but dint quite get through the desired results.
Any quick help on this would be grateful.
This code will probably do the trick
Sub ChangeColorRedGreen()
With Selection.Find
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.MatchWildcards = True
.Replacement.ClearFormatting
.Execute FindText:="$*F"
.Replacement.Font.Color = wdColorGreen
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.MatchWildcards = True
.Replacement.ClearFormatting
.Execute FindText:="$*A"
.Replacement.Font.Color = wdColorRed
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
also read: https://msdn.microsoft.com/en-us/library/office/ff193977(v=office.14).aspx
a similar (not identical) case here: Find all instances of yellow text and change font color to red

Microsoft Word macro for highlighting 500+ words, with no limit to TargetList

I've tried using the code shared in this stackoverflow thread about creating a macro that highlights specific words, and that code works for up to 68 words and phrases, but then it seems like I reach a limit and it won't allow me to enter additional terms. I need to be able to search for and highlight 500+ terms.
Is there normally a limit to search terms? Or is there a way for me to refer the macro to a separate document that contains all the words I want it to identify, as opposed to entering each term into the code? Thanks very much for any guidance.
I have already tried this code with up to 68 targets, at which point the code stops letting me enter new terms:
Sub HighlightTargets()
'
' HighlightTargets Macro
'
'
Dim range As range
Dim i As Long
Dim TargetList
TargetList = Array("target 1", "target 2", "target 3", ... "target 68")
For i = 0 To UBound(TargetList)
Set range = ActiveDocument.range
With range.Find
.Text = TargetList(i)
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute(Forward:=True) = True
range.HighlightColorIndex = wdYellow
Loop
End With
Next
End Sub

How to delete all text after certain character in MS Word?

let's say I have text like this:
Something something: what what what
another something something: another whhaa
So I want to delete text after character : on each line, so I end up with:
Something something
another something something
is that possible?
It's possible, but if we're talking about lines (as opposed to paragraphs) not 100% reliable. Word doesn't consider a "line" as a tangible object since it's always recalculating the page flow (where things break across lines/pages). Paragraphs are objects since ANSI 13 defines the end of a paragraph.
Here's some VBA code that demonstrates the literal interpretation of your request. The part through Find.Execute was made by recording using Word's Find functionality. The last three lines extend the selection to the end of the current line, move it back by one character to leave the new line / end of paragraph intact. then delete the selection. This should give you a starting point.
Selection.Find.ClearFormatting
With Selection.Find
.Text = ":"
.Replacement.Text = ""
.Forward = True
.wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.CorrectHangulEndings = False
.HanjaPhoneticHangul = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveEnd wdLine, 1
Selection.MoveEnd wdCharacter, -1
Selection.Delete