Share via

Replace direct formatting with styles while retaining initial font size

Anonymous
2016-03-22T13:22:30+00:00

As part of a macro, I want to replace plain text tagged with <em> tags with the style 'emphasis'. The problem with this is that if the point size varies between different paragraphs, the italicised text may be the wrong size. I can't find a way to prevent the application of the style also modifying the font size (to Default Paragraph Font presumably). Is there a way?

I've tried choosing 'Style based on: underlying properties' which sounds promising - but doesn't help.

The same issue is perhaps simpler to demonstrate by using a find/replace. Create some lines of text of different point sizes and italicise (using direct formatting) a word or two in each. Then:

Find: italic text

Replace: style 'emphasis'.

The result is unlikely to be what anybody would ever want.

I would be grateful for any suggestions. I apologise if this question has already been asked (I expect it may well have been but I couldn't find an answer).

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Jay Freedman 207.7K Reputation points Volunteer Moderator
2016-03-23T02:46:06+00:00

The problem you're having stems from the order in which Word applies the various sources of attributes to determine the final formatting of a piece of text.

The order for font attributes, where each later source overrides the ones before it if there is a different value, is: Default Font, Paragraph Style, Character Style, Direct Formatting.

If a piece of text has a size applied by direct formatting, and then you apply a character style (even though the character style doesn't explicitly involve the font size), the direct formatting is removed -- you can't slip the character style in "under" the direct formatting. So the size then is determined by the paragraph style (or, if that style doesn't specify the size, then it's determined by the default font size).

You can modify the macro to get the existing font size of each matched text before applying Emphasis style, and then reapply that size afterward...

Sub x()

    Dim rg As Range

    Dim fSize As Single

    Set rg = ActiveDocument.Range

    With rg.Find

        .Format = True

        .Font.Italic = True

        While .Execute

            fSize = rg.Font.Size

            rg.Style = ActiveDocument.Styles("Emphasis")

            If fSize <> wdUndefined Then

                rg.Font.Size = fSize

            Else

                rg.Select

                MsgBox "mixed sizes in " & rg.Text

            End If

            rg.Collapse wdCollapseEnd

        Wend

    End With

End Sub

Note that if the range of the found text includes more than one font size, the value of rg.Font.Size will be wdUndefined (= 9999999). If necessary, in that case you could examine the range before applying Emphasis and store the size of each character in an array, to be reapplied later.

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2016-03-24T08:27:19+00:00

    Many thanks  - this certainly explains the problem and could be a useful solution in some situations. I'm glad this wasn't something simple I was missing.

    In this case there may or may not be different font sizes; if there are, I imagine the macro would be quite complicated (certainly beyond my rather basic VBA skills). The font size wouldn't vary within paragraphs, but if it would be necessary to examine each character nevertheless, I imagine the process might slow the macro down; there could be documents of 50-100 pages of text. 

    The macro is contained in a document which has buttons linked to other documents/templates which will become mail merge documents (once the macro has mail merge-enabled them). These templates contain a number of paragraphs, consisting of merge fields, which are formatted with various styles. My idea was that the initial document will not be modified by users, but the templates can be (that's why the font sizes are not known and could vary). If the text used in the merge contains tags representing bold/italics, I thought it would be best to use styles rather than direct formatting - apart from anything else, to be more useful if the resulting document is then imported into InDesign for example (which might only happen infrequently).

    I think the simplest solution would be not to use styles for the bold/italics, but to use direct formatting. I think this idea may have been misconceived, particularly following your explanation of how things work. If documents were destined for InDesign, styles could be applied to the formatted document, using find/replace. It probably wouldn't matter that the sizes might be 'wrong' in the context of the relevant paragraphs, given new styles would be applied/mapped in InDesign (but I don't know - I know InDesign less than I know VBA).

    Thanks again.

    Was this answer helpful?

    0 comments No comments