0
votes

QUESTION: I would like to know how to create a loop to find/select a range of data and apply a character style. Each range is identifiable by a start and end tag. For example: aCS1a the text between these aCS1a and zCS1z tags is formatted with a Character Style named cs1 that already exists in the Word document zCS1z

The process so far: (1) A database exports a text file with "tags" that are used to identify formatting for paragraph styles and character styles. (2) A macro in Microsoft Word formats each paragraph with a specific Paragraph style.

Help required please: (3) Next I want to select each instance of aCS1aThe text to be formatted goes herezCS1z and apply the Character style with name "cs1" to the text between the aCS1a and zCS1z tags.

For example: Candidates presented by the aCS1aDean of the Krusty Clown SchoolzCS1z, aCS2aProfessor Homer SimpsonzCS2z, aCS1aBSc(Hons)zCS1z aCS2aSpringfieldzCS2z, aCS1aPhDzCS1z aCS2aShelbyvillezCS2z.

Sorry I had to change the "tags" to aCS1a and zCS1z because angle brackets enclosing the tag got mangled in the preview.

Where: cs1 = a specific Character Style in the Microsoft Word document and cs2 = a different Character Style in the Microsoft Word document

The loop in the macro can remove the aCS1a zCS1z, aCS2a zCS2z "tags" as they are formatted with the appropriate Character Style or I can record a Find/Replace macro to remove the tags as the final step of the processing.

1

1 Answers

0
votes

Using similar questions/answers here at Stackflow and other sites such as http://www.pcreview.co.uk/forums/word-select-text-search-delete-macro-t3868065.html I was able to piece together a long-winded and cumbersome macro to do the trick. Apologies the angle brackets have disappeared around my starting cs1 and ending /cs1 tags.

Sub TaggedTextIsCS1()
'
'Find <cs1>text inbetween</cs1> and replace with Character Style CS1
    Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Style = ActiveDocument.Styles("CS1")
    With Selection.Find
        .Text = "\<cs1\>*\</cs1\>"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = True
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

    'Find <start> tag and remove
    Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("CS1")
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "<cs1>"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

    'Find <end> tag and remove
    Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("CS1")
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "</cs1>"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

End Sub