Solved

Word and VBA: Formatting text inside a character style

Posted on 2011-09-14
3
944 Views
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.
     --JRA

Demo-text.doc
0
Comment
Question by:JohnRobinAllen
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
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.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    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

0
 

Assisted Solution

by:JohnRobinAllen
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.
JRA
0
 

Author Closing Comment

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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

759 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

18 Experts available now in Live!

Get 1:1 Help Now