Assigning styles to all text in a Word document .. Part 2

Posted on 2012-08-16
Last Modified: 2012-08-22
I got much closer to solution of a problem with this interchange recently:

I'm creating a Word document by taking a very large page of html in a browser, selecting everything, copying it, and then pasting it into Word.  Then, I want to assign different styles to the different fonts in the new Word document, so that I can easily change the appearance of the document later.

The VBA code in the above link worked great to search for a font name and size, and assign a style to those paragraphs.  But there is still a problem: If, for example, I search for Verdana 10pt and assign it a style "Book_normal", now the Verdana text has the style, but it still *appears* as Verdana 10pt, even though "Book_normal" is Garamond.

I found that the Style Inspector now shows that the changed text is of the Style Book_normal, but *with* Text Level Formatting "Plus Verdana 10 pt."

If I clear the text level formatting, then we're fine, but I need to do that programmatically.  I tried using the Styles box to select all 4000 instances of "Book_normal" and then pressing "Ctrl-space" to clear the text level formatting, but it's not reliable because there are so many instances, and I suspect that this can easily be done with VBA.  I have not been able to find the code to do that.

Any help would be appreciated.  I hope I described the problem well.
Question by:StevenMiles
    LVL 76

    Expert Comment

    I'm not sure where we are with this. I think that I may have posted a partially-formed answer previously.

    However, when you apply a paragraph style to a paragraph, existing character text that does not have the default character formatting keeps that formatting

    If you want to set that font for the whole paragraph, you will have to set it explicitly, e.g. : = "Garamond"

    Author Comment

    Thanks for the comment.
    Here's what I'm doing, based on the previous question/answer:

    Sub Set_Style_Verdana_to_Book_normal()
        With ActiveDocument.Range.Find
            .Font.Name = "Verdana"
            .Font.Size = 10
            .Replacement.Style = "Book_normal"
            .Execute Replace:=wdReplaceAll
        End With
    End Sub

    That works great.  When the sub runs, wherever it finds Verdana 10 pt, it assigns the style Book_normal to that text.  But now the Style Inspector says:

    Paragraph formatting:
    Plus: <none>

    Text level formatting:
    Default Paragraph Font
    Plus: Verdana, 10pt

    I'd like to assign the style to the text and also have a way to clear the "Text level formatting." There are too many instances to do this manually, and there are other font/Style assignments I want to do, so I'd love to do it programmatically.

    Hope that's clearer?
    --Steve D.

    Accepted Solution

    I found my own solution.  After the styles have been assigned, but the text still has that irritating text level formatting, you can select some text that has a style, then go to the style window, click the down arrow to the right of the style, and select Update [Style Name] to Match Selection.  That will clear the text formatting everywhere that style appears.

    Surprisingly, you have to do that manually.  If you put it into a macro, it doesn't work!

    Author Closing Comment

    Didn't get solution elsewhere

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    MS Word line spacing 24 50
    word 2013 3 45
    Word 2013 custom ribbon to toggle an advanced option on and off 5 57
    Mail Merge/EMail 10 44
    The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
    A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
    This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
    Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now