Formatting Word document created from Excel VBA

svendfj

Board Regular
Joined
Mar 19, 2011
Messages
99
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
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Forum statistics

Threads
1,215,035
Messages
6,122,785
Members
449,095
Latest member
m_smith_solihull

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
Back
Top