Solved

Capitalize first letter of words 4 characters or greater if certain styles are applied:

Posted on 2012-04-13
5
268 Views
Last Modified: 2012-04-23
Hello, I have some long documents and I want the formatting to appear uniform.  One of the rules I am trying to apply pertains to Headers and Table titles which have one of several styles applied to them.  I would also like the first letter of hyphenated words before and after the hyphen to be capitalized.

Here is an example (just imagine it has one of the relevant styles applied):

Before:
This is an incorrectly Formatted title with a non-randomly inserted hyphenated word.

After
This is an Incorrectly Formatted Title With a Non-Randomly Inserted Hyphenated Word.


The style names of interest are:
Level1Heading
Level2Heading
Level3Heading
Level4Heading
Level5Heading
Level6Heading
Level7Heading
Level8Heading
ChapterHeading
TableTitle
FrontMatterHead


Thank you for any help you can provide experts and I look forward to talking with you,
Bev
0
Comment
Question by:Bevos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 37846374
I don't see any way to tweak a style to do this.  :-(

However, you could iterate through the different styles you've listed and go to (Find) each item in your document with that style, changing the case of the found range.
Example:
Public Sub RecaseStyles()
    Dim strStyleNames() As Variant
    Dim vItem As Variant
    strStyleNames = Array("Level1Heading", "Level2Heading", "Level3Heading", _
                     "Level4Heading", "Level5Heading", "Level6Heading", "Level7Heading", _
                     "Level8Heading", "ChapterHeading", "TableTitle", "FrontMatterHead")
    For Each vItem In strStyleNames
        With ActiveDocument.Content.Find
            .ClearFormatting
            .Style = vItem
            .Execute Forward:=True, Format:=True
            Do While .Found = True
               .Parent.FormattedText.Case = wdTitleWord
               .Execute Forward:=True, Format:=True
            Loop
        End With
    Next
End Sub

Open in new window

0
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 37846422
After a second reading of your requirements, I realized that formatting the entire sentence wasn't sufficient. We have to iterate the words and check for both length and the hyphenated word.  This isn't a fool-proof routine, so you need to run it against all the possible text layouts in your documents.

Public Sub RecaseStyles()
    Dim strStyleNames() As Variant
    Dim vItem As Variant
    Dim rngWord As Range
    strStyleNames = Array("Level1Heading", "Level2Heading", "Level3Heading", _
                "Level4Heading", "Level5Heading", "Level6Heading", "Level7Heading", _
                "Level8Heading", "ChapterHeading", "TableTitle", "FrontMatterHead")
    For Each vItem In strStyleNames
        With ActiveDocument.Content.Find
            .ClearFormatting
            .Style = vItem
            .Execute Forward:=True, Format:=True
            Do While .Found = True
                For Each rngWord In .Parent.Words
                    If Len(Trim(rngWord.Text)) > 3 Then
                        rngWord.FormattedText.Case = wdTitleWord
                    End If
                    If rngWord.Text = "-" Then
                        rngWord.Previous(wdWord).FormattedText.Case = wdTitleWord
                    End If
                Next
                .Execute Forward:=True, Format:=True
            Loop
        End With
    Next
End Sub

Open in new window

0
 

Author Comment

by:Bevos
ID: 37852302
Hello, I am very excited to try the solution you posed, but when I do I get an MS Word that stops responding (this is why trying on a small ~20 page document).  Is there a reason why this might be happening for me or is it just too long for this to run?

Thanks,
Bev
0
 
LVL 45

Expert Comment

by:aikimark
ID: 37852388
please post the document you are using for your test.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 37882273
thanks for the points.  Did you resolve the stops-responding behavior?
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

695 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