0

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
Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
SmIqbal
  • 89
  • 9
  • You do not have a regex here, these are MS Office *wildcard* patterns. They are similar to regular expressions though. – Wiktor Stribiżew Oct 14 '16 at 12:10
  • Well, it is not a dupe of [Reference - What does this regex mean?](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) because there is no regex in the question. I retagged the question. – Wiktor Stribiżew Oct 14 '16 at 12:23
  • Wiktor, thanks for your feedback and confirming the fact that the code resembles with RegEx. The VBA Object browser also has RegEx libraries so I thought this was RegEx. – SmIqbal Oct 17 '16 at 07:18
  • Wiktor, plz clarify a few lines as copied below. CODE#1 .Text = " TOTAL #*^12REPORT*{1,255}PAGE*{1,255}FUND TOTALS*[=]{1,}*^13*^13" Q-1: I need your help as to what all those curly braces and numbers mean? CODE#2 .Text = "(CLEARED)^13" .Replacement.Text = "\1 " .Execute Replace:=wdReplaceAll Q-2: Last two lines need some help. CODE#3 '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 Q3: Plz explain 3 lines. – SmIqbal Oct 17 '16 at 08:06
  • Please read http://word.mvps.org/faqs/general/UsingWildcards.htm or similar, say, https://support.office.com/en-us/article/Find-and-replace-text-or-formatting-in-Word-for-Mac-ac12f262-e3cd-439a-88a0-f5a59875dcea. – Wiktor Stribiżew Oct 17 '16 at 08:10

0 Answers0