2
votes

I'm working on a macro which will find a text pattern and apply styles.

Document Content:

Start-Style1Test HeadingEnd-Styles1Start-Style2 -This is the paragraph.End-Styles2

Here, my macro would search for "Start-Style1 * End-Style1" and apply the style1. Here in our case, "Test Heading" would have style1 applied. Similarly, it would do the same for style2. My macro is working fine if my Heading and context are in different lines.

But if it's the same line. Only one style is applied. On diving deep into the issue, I found that it's due to the Style type as 'Paragraph' for both the styles.

If I change it to 'Character' as style type, I am able to get the expected output. I want to apply styles without changing the style type. Could you please let me know if it's possible.

Here's my code:

Application.ScreenUpdating = False
Dim RngStory As Range
Dim StrStart
Dim StrEnd
Dim Styles
StrStart = Array("Start-Style1", "Start-Style2")
StrEnd = Array("End-Style1", "End-Style2")
Styles = Array("Style1", "Style1")
For i = 0 To 1
Set RngStory = ActiveDocument.Range
With RngStory.Find

  .ClearFormatting
  .Text = StrStart(i) & "*" & StrEnd(i)
  .Forward = True
  '.Wrap = wdFindStop
  .MatchCase = False
  .MatchWholeWord = False
  .MatchWildcards = True
  .MatchSoundsLike = False
  .MatchAllWordForms = False
  .Replacement.Text = ""
  Do While .Execute
    With RngStory.Duplicate
      .Start = .Start + Len(StrStart(i))
      .End = .End - Len(StrEnd(i))
      .Style = ActiveDocument.Styles(Styles(i))
       .End = .End - Len(StrEnd(i))
      .Collapse (wdCollapseEnd)
    End With
  Loop
End With
Next
2

2 Answers

0
votes

Unfortunately, the only way to combine two different style types in same line (which is effectively a paragraph, or part thereof) is by using a character style, but it's not likely what you want anyway.

In a Word document you generally use paragraph styles for major parts of the text like headings, and well, paragraphs and then if required character styles on top of paragraph styles. Eg the built in Word character styles are used for footnotes and page numbers.

You can read more here: http://office.microsoft.com/en-us/word-help/understanding-paragraph-character-list-and-table-styles-HA001187614.aspx

Do you have a reason for wanting to combine different styles on the same line?

0
votes

Combining 2 styles on the same line is something we do very often in legal documents. Example: Heading 1 through Heading 9 would be linked to numbering. In addition, the Heading 2 style might need to be set up with a format we called "lead-in emphasis" (also known as "run in heading.") Finally the rest of the paragraph would consist of a body text style. To achieve this format we would use either (1) a style separator in from of the body text style or (2) a paragraph mark formatted with the hidden attribute. Here's an example of this format:

                   Article I <--Heading 1 style

Section 1.1 Intro: The numbering for Section 1.1 is achieved with the Heading 2 style but the remaining text of this paragraph is actually the body text style.

Section 1.2 More Text: The numbering for Section 1.2 is achieved with the Heading 2 style but the remaining text of this paragraph is actually the body text style.

                   Article II <--Heading 1 style

Section 2.1 Lead-In Emphasis: The numbering for Section 2.1 is achieved with the Heading 2 style but the remaining text of this paragraph is actually the body text style.

Section 2.2 More Text: The numbering for Section 2.1 is achieved with the Heading 2 style but the remaining text of this paragraph is actually the body text style.