How to delete all text after certain character in MS Word? - 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

Related

Search for text in Word Endnotes

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

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

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

Getting the text of all check boxes in a tabcontrol to a string

I am extremely new to all of this, and whilst I have tried searching I cant find anything that has helped me achieve what I am after.
I have a form in VB with the following:
1 x tabcontrol
10 x checkboxes which sit in various tabs on the tab control
1 x listbox
When i tick any of the check boxes, I want their text to be added to the listbox, and when I untick, their text to be taken from the listbox.
I can achieve this very easily using if statements for the changedcheck event for each checkbox but I have to do that for every single checkbox which isn't very efficient as potentially i could have 20,30 40+ check boxes. Plus if I add one at a later stage I would have to remember to add its code.
Ideally i want a method that's says: check all the checkboxes in tabcontrol if there value is true write their text to a string, if there value is false, take there text from the string. put the string in the listbox.
I started with something like this...
Dim chk As CheckBox
Dim txt As String = ""
For Each chk In TabControl1.Controls
If chk.Checked = True Then
txt = txt + chk.Text +vbCrLF
Else
txt = replace(txt, chk.text + vbCrLf, "")
End If
Next
End Sub
First problem is that the above obviously doesn't work! so any guidance there is appreciated - i put it together from reading scraps from other code.
Second problem is, i can't get my head round how the list box will be updated, as previously i was using the CheckedChanged event for each control, which if i do what i want, then there wont be a specific CheckedChanged event, as it could be any of the checkboxes (hopefully that makes sense!). I don't want to have to press a button to add the checked checkboxes to the listbox, i want it to be dynamic
any help is very much appreciated.
For your first problem add
Dim chk As Control
Dim txt As String = ""
For Each chk In TabControl1.Controls
If TypeOf chk Is CheckBox
If DirectCast(chk, CheckBox).Checked = True Then
txt = txt + chk.Text +vbCrLF
Else
txt = replace(txt, chk.text + vbCrLf, "")
End If
End If
Next
End Sub
For your second problem in CheckedChanged event you can do something like this:
Private Sub OnCheckedChanged(sender as Object, e as EventArgs) _
Handles CheckBox1.CheckedChanged
Dim chk As CheckBox = TryCast(s, CheckBox)
Dim txt as string
If c.Checked = True Then
txt = chk.Text
EndIf
End Sub