Word and VBA: Formatting text inside a character style

Posted on 2011-09-14
Last Modified: 2012-05-12
    In a different question, I wondered whether it would be possible select some formatted text and apply a character format style to it without destroying the formatting. In comment 3644204, Graham Skan correctly assured me that it would be impossible to do so.
     I subsequently looked at some documents that I had massaged with some long-since lost code, and the documents show formatted text with a character style applied to them. I use the character style to hide or show certain words. For example, the attached document, an excerpt from one of my old documents, hides or shows a comment. Shift + Alt + J hides the blue text that has a character formatting of “LB Mont Comment”. Shift + Alt + Y exposes the “LB Mont Comment” text. The text that is either displayed or hidden has some italics.
     Apparently there is a way to have text formatted accomplish that through code. I know that if some words already have a specific character style, one can format the words manually without losing the character style. I suppose that before I have the computer apply a character style to some words, I can have it search for things like italics, underlined text, or boldface text, and have the code put unique marks at the start and end of such areas. Then after I subsequently apply the character style that destroys formatting, I could have the computer search for those markings and reformat the text according to what is needed. To avoid having the computer mark other formatted text in the document, I think I would have to make a new document, cut and paste the text to be reformatted into the new document, make my style marking there, and then copy and paste the marked text back into the original document, apply the character style to it, and then apply the formatting as per what markings the text has.
     I suspect there might be an easier way to do the same thing, but if necessary, I will do it the hard way. However, I would appreciate some suggestions as to how those tasks could be done more easily.

Question by:JohnRobinAllen
  • 2
LVL 13

Accepted Solution

Stacy Brown earned 150 total points
ID: 36542763
Selection.Font.Reset is what you are looking for, I think.  You don't need to copy text out of a document to apply a character style.  Sometimes it is wise to reset the font formatting though.  Selection.Font.Reset is the same as selecting text and hitting CTRL+Spacebar to remove any Character formatting.

You can also do a find/replace to apply formatting.  See the code below.

'This finds the word Lorem and formats it with the Character style I named Lorem Bold Italics

    Selection.Find.Replacement.Style = ActiveDocument.Styles( _
        "Lorem Bold Italics")
    With Selection.Find
        .Text = "Lorem "
        .Replacement.Text = "^&"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = False
        .MatchFuzzy = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Open in new window


Assisted Solution

JohnRobinAllen earned 0 total points
ID: 36557488
My basic problem was how to attach a character style to text without changing the formatting already on the text. There appears to be no way to do that, but there is a way to record aspects of the style, apply the character formatting that destroys the formatting, and then go back and reformat the text as recorded. In a subsequent comment that I will post to this question after I return in a couple of weeks, I will include the code that does that. Here is a description of what the code does:

We first find six odd characters that do not appear in the text. These will mark the starting points and ending points of text formatted as italics, underlining, and boldface. We then go through the text that will soon have a character format style and search for those three styles, to mark the locations with those unique characters. We then apply the character style, and finally go through the text to reformat specified areas and delete the special marks.

It looks like a clumsy kludge to a problem, but it works very quickly and does the job. Finalword’s solution does not solve that problem, but the solution I wrote uses some of Finalword’s  ideas, so he should get half the credit with my thanks.

Author Closing Comment

ID: 36585523
I will later send the code that does the work.

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

810 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