Solved

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

Posted on 2012-04-13
5
262 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
  • 4
5 Comments
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
please post the document you are using for your test.
0
 
LVL 45

Expert Comment

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
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 …

762 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

12 Experts available now in Live!

Get 1:1 Help Now