If you setup a table with the codes in the first column and the associated
styles in the second then you can either select the code and rely on the
fact that replace will apply the style to the whole of the paragraph or you
could find the code and select the paragraph as a range then format that.
The following macro uses the first method to format the paragraph then
remove the code.
The table document - here "D:\My Documents\Test\Codes.doc"
is configured as follows
Col1 Col2
#H1 Heading 1
#H2 Heading 2
#BT Body Text
#N Normal
etc
For testing the codes were added to the start of the paragraph.
Dim ChangeDoc As Document, RefDoc As Document
Dim cTable As Table
Dim rCode As Range
Dim rStyle As Range
Dim i As Long
Dim sFname As String
sFname = "D:\My Documents\Test\Codes.doc"
Set RefDoc = ActiveDocument
Set ChangeDoc = Documents.Open(sFname)
Set cTable = ChangeDoc.Tables(1)
RefDoc.Activate
For i = 1 To cTable.Rows.Count
Set rCode = cTable.Cell(i, 1).Range
rCode.End = rCode.End - 1
Set rStyle = cTable.Cell(i, 2).Range
rStyle.End = rStyle.End - 1
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Style = rStyle
.Execute findText:=rCode, _
ReplaceWith:="", _
Replace:=wdReplaceAll, _
MatchWholeWord:=True, _
MatchWildcards:=False, _
Forward:=True, _
Wrap:=wdFindContinue
.Replacement.ClearFormatting
.Execute findText:=rCode, _
ReplaceWith:="", _
Replace:=wdReplaceAll, _
MatchWholeWord:=True, _
MatchWildcards:=False, _
Forward:=True, _
Wrap:=wdFindContinue
End With
End With
Next i
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>