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

Posted on 2012-08-16
Medium Priority
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
  • 3
LVL 76

Expert Comment

ID: 38302615
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. :

Para.font.name = "Garamond"

Author Comment

ID: 38302841
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

StevenMiles earned 0 total points
ID: 38305905
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

ID: 38319675
Didn't get solution elsewhere

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
Suggested Courses

850 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