I want to understand some RegEx code - vba

Copied below is some RegEx code that I have found on Internet. Certain RegEx related search patterns that begin with .Text and likewise their .Replacement.Text usage could not be properly understood by me. Is there anyone who could explain these bunch of RegEx related lines of code to me for my understanding.
Also advise on the use of .Execute Replace with two variants wdReplaceOne and wdReplaceAll which even though sound self explanatory in Word but in VBA, these also need clarification as far as their used is concerned.
Your help would be much appreciated. Here is the code:
Sub StatementReformat()
Application.ScreenUpdating = False
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
'First Page
.Text = "REPORT*EXTRACT FILE : [A-Z0-9]{1,}^13^12"
.Replacement.Text = ""
.Execute Replace:=wdReplaceOne
'Column Headers (except New First Page)
.Text = "^12*VEND^13*[-]{1,}^13"
.Execute Replace:=wdReplaceAll
'Header Underline on First Page
.Text = "[-]{1,}^13"
.Execute Replace:=wdReplaceAll
'Last Page
.Text = " TOTAL #*^12REPORT*{1,255}PAGE*{1,255}FUND TOTALS*[=]{1,}*^13*^13"
.Execute Replace:=wdReplaceAll
'Unwanted Header Lines - First Page
.Text = "REPORT*CHECK^13"
.Execute Replace:=wdReplaceAll
'Empty Paragraphs
.Text = "[ ]{1,}^13"
.Execute Replace:=wdReplaceAll
'Header Line Wrap - First Page
.Text = "(STATUS)^13"
.Replacement.Text = "\1"
'Record Line Wraps
.Execute Replace:=wdReplaceAll
.Text = "(OUTSTANDING)^13"
.Execute Replace:=wdReplaceAll
.Text = "(CLEARED)^13"
.Replaceme`enter code here`nt.Text = "\1 "
.Execute Replace:=wdReplaceAll
.Text = "(VOIDED)^13"
.Replacement.Text = "\1 "
.Execute Replace:=wdReplaceAll
'Pad Records with Multiple Rows
'Pad Records with Multiple Rows
.Text = "(^13)([ ]{20})([ ]{1,8}[0-9]{1,8}.[0-9]{2})"
.Replacement.Text = "\1\2\2\2\2\2\2\2 \3"
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub

Related

Interacting with a document without using selection

I have a transliteration function (from cyrillic to latin). I will use this function in a unviersal subroutine (with text of any lenght). This sub must to copy the source text, transliterate (from cyrillic to latin) and paste it below without any formatting changes and without using selection. The next step is reverse transliteration (again copy and paste below). There must be 3 textes in the final. I kinda know how to realize it, but i don't know what i should use instead of selection.
*
P.S. i tried use For Each word In ActiveDocument.Range.Words but it works bad with reverse transliteration (exactly that. without it, the function works perfectly in debugging)
P.P.S. sorry for mistakes in the text, i'm not a native speaker
Since you haven't posted any actual transliteration code, I'll leave you to add the cyrillic and latin character sets to the code below:
Sub Transliterate()
Application.ScreenUpdating = False
Dim p As Long, i As Long, StrLng1, StrLng2
'Insert the character codes for the cyrillic characters here
StrLng1 = Array(ChrW(&H430), ChrW(&H431), ChrW(&H432))
'Insert the corresponding latin characters here
StrLng2 = Array("a", "b", "c")
With ActiveDocument.Range
Do While .Characters.Last.Previous = vbCr
.Characters.Last.Previous.Delete
Loop
.InsertAfter vbCr
'Duplicate Content
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Wrap = wdFindContinue
.MatchWildcards = True
.Text = "^13"
.Replacement.Text = "^l"
.Execute Replace:=wdReplaceAll
.Font.Bold = True
.Text = "[!^l]#^l"
.Replacement.Text = "^p^&"
.Execute Replace:=wdReplaceAll
.ClearFormatting
.Text = "^l^13"
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
.Execute Replace:=wdReplaceAll
.Text = "[!^13]#^13"
.Replacement.Text = "^&^&^p"
.Execute Replace:=wdReplaceAll
End With
.Characters.Last.Previous.Delete
.Characters.First.Delete
'Loop through duplicated paragraphs
For p = .Paragraphs.Count - 1 To 2 Step -3
With .Paragraphs(p).Range
.Font.Italic = True
'Transliterate paragraph
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Wrap = wdFindStop
.MatchWildcards = False
.MatchCase = True
.Font.Bold = False
For i = 0 To UBound(StrLng1)
.Text = StrLng1(i)
.Replacement.Text = StrLng2(i)
.Execute Replace:=wdReplaceAll
Next
End With
'Duplicate translated paragraph
.Characters.Last.Next.FormattedText = .FormattedText
End With
Next
.Characters.Last.Previous.Delete
'Loop through duplicated paragraphs
For p = .Paragraphs.Count To 3 Step -3
With .Paragraphs(p).Range
.Font.Underline = wdUnderlineSingle
'Reverse Transliterate paragraph
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Wrap = wdFindStop
.MatchWildcards = False
.Font.Bold = False
.MatchCase = True
For i = 0 To UBound(StrLng1)
.Text = StrLng2(i)
.Replacement.Text = StrLng1(i)
.Execute Replace:=wdReplaceAll
Next
End With
End With
Next
End With
Application.ScreenUpdating = True
End Sub

Word VBA: Why is my macro leaving the first letter of its target word italicised?

I'm trying to write a macro to change "In-vitro", "in-vitro", and their italic counterparts into just "in vitro" with no italics or hyphen. It works, but it leaves the "i" as italics. Weirdly, the exact same code works perfectly for other latin terms like "in vivo". Here is an excerpt from the code code with the "in vivo" bits included to show how it works for that, but not for "vitro":
(Note, we are editing with tracked changes turned on)
Sub latintest()
With Selection.Find
.ClearFormatting
.Text = "([Ii])n-vitro"
.MatchWildcards = True
.Replacement.Font.Italic = False
.Replacement.Text = "\1n vitro"
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With
With Selection.Find
.Font.Italic = True
.Text = "In[ -]vitro"
.MatchWildcards = True
.Replacement.Font.Italic = False
.Replacement.Text = "In vitro"
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With
With Selection.Find
.Font.Italic = True
.Text = "in[ -]vitro"
.MatchWildcards = True
.Replacement.Font.Italic = False
.Replacement.Text = "in vitro"
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With
With Selection.Find
.ClearFormatting
.Text = "([Ii])n-vivo"
.MatchWildcards = True
.Replacement.Font.Italic = False
.Replacement.Text = "\1n vivo"
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With
With Selection.Find
.Font.Italic = True
.Text = "In[ -]vivo"
.MatchWildcards = True
.Replacement.Font.Italic = False
.Replacement.Text = "In vivo"
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With
With Selection.Find
.Font.Italic = True
.Text = "in[ -]vivo"
.MatchWildcards = True
.Replacement.Font.Italic = False
.Replacement.Text = "in vivo"
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With
End Sub
The code is literally exactly the same, but one changes "in vitro" to "in vitro" while the other changes "in vivo" to "in vivo", as intended.
Has anybody got any ideas?
Cheers
I managed to get it working just by changing the formatting of the replace-all code to the following:
With Selection.Find
.Text = "in-vivo"
.Replacement.Text = "in vivo"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
and so on.
Basically just moving the .Execute Replace:=wdReplaceAll outside of the With block solved it for some reason. It does means having a few more "blocks" of each bit of this code for each different incorrect formatting of "in vivo" etc., which is a slight issue in terms of efficiency, but it's better than it not working at all so gonna stick with it.

To find and replace a text in the whole document in MS Word 2010 (including tables)

I have an MS Word document including a table. I am trying to find and replace text via VBA using the following code:
If TextBox1.Text <> "" Then
Options.DefaultHighlightColorIndex = wdNoHighlight
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
With Selection.Find
.Text = "<Customer_Name>"
.Replacement.Text = TextBox1.Text
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.ClearFormatting
With Selection.Find.Font
.Italic = True
End With
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.Font
.Italic = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End If
This works fine for replacing all my content which is outside of the table. But it will not replace any of the content within the table.
If your goal is to perform replacements in the whole documents (it looks so from the code, but it is not explicit), I would suggest you use Document.Range instead of the Selection object. Using Document.Range will make sure everything is replaced, even inside tables.
Also, it is more transparent to the user, as the cursor (or selection) is not moved by the macro.
Sub Test()
If TextBox1.Text <> "" Then
Options.DefaultHighlightColorIndex = wdNoHighlight
With ActiveDocument.Range.Find
.Text = "<Customer_Name>"
.Replacement.Text = TextBox1.Text
.Replacement.ClearFormatting
.Replacement.Font.Italic = False
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
End If
End Sub
I have used the following code and it works like charm..... for all the occurances that are found in the document.
stringReplaced = stringReplaced + "string to be searched"
For Each myStoryRange In ActiveDocument.StoryRanges
With myStoryRange.Find
.Text = "string to be searched"
.Replacement.Text = "string to be replaced"
.Wrap = wdFindContinue
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Highlight = False
.Execute Replace:=wdReplaceAll
End With
Next myStoryRange

VBA word macro / automatically find and replace specific characters for whole document

I'm trying to do the following using the macro:
Upon opening the document, automatically search whole document for brackets {{ }} and delete them including the text inside.
It doesn't do the job well, just operates on the text selected, not the whole document.
Sub SelectToBracketsDelete()
With Selection.Find
.ClearFormatting
.Text = "{{"
.Forward = False
.Wrap = wdFindStop
.Execute
End With
Selection.Extend
With Selection.Find
.Text = "}}"
.Forward = True
.Execute
.Text = ""
End With
Selection.Text = ""
End Sub
Is this what you are looking for?
Word 2007 -> stackoverflow
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "\{\{*\}\}"
.Replacement.Text = ""
.Forward = True
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

How do i change the default highlight in word using vba?

I am writing a sub that finds specific text by whole word and highlights it. The problem is that the user wants the text in grey (wdGrey25) rather than the default of yellow. Here is my sample code:
Public Sub HighlightStrings()
Dim rng As Range
Set rng = ActiveDocument.Range(Start:=0, End:=0)
With rng.Find
.Forward = True
.Wrap = wdFindStop
.MatchWholeWord = True
.Text = "Claimant's name"
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll
.Text = "date"
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll
.Text = "he/she"
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll
.Text = "describe incident"
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll
.Text = "describe condition(s)"
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll
.Text = "describe occupational disease"
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll
End With
End Sub
So far, it works perfectly to find and highlight without throwing alignment and positioning off, as with previous versions, but the highlight color is too painful for our older users to look at. Anyone got a fix for that?
Thanks in advance,
-C§
This is what you are looking for:
Options.DefaultHighlightColorIndex = wdGrey25
You need to set it at the beginning of your code.