make selection in a whole document - vba

in a file MS WORD I wish to select a "/" and make bold all characters till the following ^p. The following code works well only for the first occurrence. In the file I have many occurrences and I am not able to apply this in the whole file. I tried several times with "for...next" and others, unfortuntely without success.
Many thanks for your help! Gianluca
Sub bold_title()
Set myRange = ActiveDocument.Content
Selection.Find.ClearFormatting
With Selection.Find
.Text = "/"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.Text = "^p"
.Forward = True
.Wrap = wdFindAsk
.Format = False
End With
Selection.Find.Execute
Selection.Font.Bold = True
End Sub

The trick is to "collapse" the Selection- think of it like pressing the right-arrow key - so that the next search starts after the "found" text:
Selection.Collapse wdCollapseEnd
Usually, a Do While...Loop construct is used in order to repeat the Find until the end of the document. You'll find lots and lots of examples if you search here and elsewhere on the Internet.
For this to work successfully, make sure you set Wrap to wdFindStop (and not wdFindContinue as is currently in the code shown).

Related

How can I set the FindReplace box in VBA to allow Word Document editing while the box is still active?

I'm trying to code some routines in VBA for Word that use regular expression to search for certain elements in documents. I got this code (please disregard for now that it's coded using Selection, it's just a mock-up to see if the workflow is sound):
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
End With
With Selection.Find
.Text = "[0-9]{5,}"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
If Selection.Find.Found = True Then
Dialogs(wdDialogEditReplace).Show
End If
and it works in the sense that it does find 5+ digit numbers. However I would like the user to have the ability to edit the found number or any other part of text while the box is still active, to continue to search once the editions are finished. I'd be grateful for any tips on how to solve this issue.
The closest you will get is to use your code to set the Find parameters and then execute the Find dialog button, as shown below.
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[0-9]{5,}"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Application.CommandBars.ExecuteMso ("FindDialog")
This will leave the user to press Find Next to get the first match but will allow the dialog to work as normal, enabling edits.

How to make VBA code highlighting show up as a tracked revision in word?

My goal is to make a VBA Code that I can use on multiple documents that will highlight words of my choice so that I can review them. I have code that "works"(ie: Highlights all the words I put in a txt file), but I have come to the issue that these highlighting changes are not effected by the track revisions in word.
If I preform other actions (such as replacing) while track revisions is on, word works as expected. Is their any way I can make the change in highlight also be tracked in the same way?
Here is the main body of my code I am using to highlight the words:
With ActiveDocument.Content.Find
.Format = True
.MatchWholeWord = True
.MatchAllWordForms = False
.MatchWildcards = False
.Wrap = wdFindContinue
.Forward = True
For i = 0 To UBound(Split(StrFind, "~"))
.text = Split(StrFind, "~")(i)
.Replacement.Highlight = True
'.Replacement.text = Split(StrRepl, "~")(i) (Commented out)
.Execute Replace:=wdReplaceAll
Next i
End With
At the moment, regardless of whether track revisions is on or off, all the words are highlighted as expected, but I do not need to approve/reject it (it just happens)
I know how to turn Track revisions on and off within VBA already. I am just trying to figure out how to track highlighting changes. Any help would be greatly appreciated
This works for me:
Dim StrFind As String, i As Long
StrFind = "Lorem ipsum~Maecenas porttitor~Fusce~Nunc viverra~Vivamus"
Options.DefaultHighlightColorIndex = wdBrightGreen
With ActiveDocument
.TrackRevisions = True
With .Range.Find
.Format = True
.MatchWholeWord = True
.MatchAllWordForms = False
.MatchWildcards = False
.Wrap = wdFindContinue
.Forward = True
.Replacement.Highlight = True
For i = 0 To UBound(Split(StrFind, "~"))
.Text = Split(StrFind, "~")(i)
.Replacement.Text = "^&"
.Execute Replace:=wdReplaceAll
Next i
End With
.TrackRevisions = False
End With

Search for text and add text after

I have a document that contains the following text format that occurs throughout:
5:43-64
I want to search and replace so that the text reads like so:
5:43-64 indicates:
Fortunately, the - only appears in this type of text. The numbers change in each instance. So I don´t think I have to worry about some complicated search pattern. I can just search for the - character.
I want to then take whatever is after the the - and then save it as a variable then insert the text indicates afterward. I need this to loop through the whole document making these changes at any occurrence of the -.
Here is the code that I have up to this point that kind of half works:
Sub placeWordAfterDash()
Selection.Find.ClearFormatting
With Selection.Find
.Text = "-"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
If Selection.Find.Execute Then
Dim selectedString As String
Selection.Select
selectedString = Selection.Next(Unit:=wdWord, Count:=1).Text
Selection.Text = "-" & selectedString & " indicates: "
End If
End Sub
This code only makes the change in one instance and also leaves me with:
5:43-64 indicates: 64
Which isn´t quite what I want.
You don't need to use vba to do this find-replace, you can do it with a simple wildcard find-replace.
press CTRL+H, find (-<*>), Replace with \1 indicates: (make sure to check "Use Wildcards")
If you do want to use vba:
Selection.Find.ClearFormatting
With Selection.Find
.Text = "(-<*>)"
.Replacement.Text = "\1 indicates:"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll

Replace 'Enter' symbol to a blank space in MS Word Macro

I want to record a macro such that it will replace all 'line break' symbol after the current cursor position to a blank space.
For example if the text is:
My name is Sumit. I
love
Stackoverflow
Now if the cursor is just next to 'love' and I press the macro button it should come like this:
My name is Sumit. I
love Stackoverflow
I searched internet and wrote this (but this is not working)
Sub Replace()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
I would guess that you'd want to change
.Wrap = wdFindContinue
to
.Wrap = wdFindStop
and
.MatchWildcards = True
to
.MatchWildcards = False
The first change should prevent the search from going back around and replacing content before the cursor and the second change turns off Wildcards (^p is not a wildcard character).
But it might help to know exactly how it's not working, if those changes aren't sufficient.

Cannot get word to find "If..."

I cannot execute a find in VBA for word to find "If..." Word doesn't seem to like finding the "If." part. Any ideas?
Sub Macro2()
Selection.Find.ClearFormatting
Selection.Find.Highlight = False
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "If..."
.Replacement.Text = "If..."
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Chances are Word replaced the three periods with the single character ellipsis.
As a bonus, the ellipsis should have a space before and after it, it can be a half or thin space, but certainly a non breaking space so it won't be forced onto a new line.