Formatting Word document created from Excel VBA

svendfj

Board Regular
Joined
Mar 19, 2011
Messages
85
Office Version
  1. 2016
Platform
  1. Windows
Hello,
I am developing VBA code that has two steps:
- Step 1) Import Excel data into a Word document. This section of code works perfectly.
- Step 2) Reformat all words of structure: "ID-??". (Note that here, "?" is the Wildcard that Word uses to represent 'Any Character'. The words have five characters, including a prefix of "ID-".)
I do not yet have working code that does Step 2 in the newly created Word document. Specifically, I want the VBA code to find and reformat all 5-character words with prefix "ID-". Below is the latest iteration of this section of code .
- In other words, I can manually complete the desired reformatting with a Wildcard search in Word of: "ID-??" A Macro recorded when manually doing the formatting within Word is at the bottom of this message.

VBA code segment intended to reformat all five character words of "ID-??".
VBA Code:
With myDoc.Range.Find
  .ClearFormatting
  With .Replacement
  .ClearFormatting
  .Text = "ID-??"
  With .Font
    .Bold = True
    .Color = RGB(200, 200, 0)
    End With
  End With
.Format = True
.Forward = True
.MatchWildcards = True
End With
----------------------------------------------
Recorded Macro:
VBA Code:
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Color = -671039489
With Selection.Find
.Text = "ID-??"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With


Thank you for any insight you can please share.
Sven
 

Some videos you may like

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Watch MrExcel Video

Forum statistics

Threads
1,127,931
Messages
5,627,690
Members
416,266
Latest member
stevenvanroeden

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top