Solved

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

Posted on 2012-04-13
5
265 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
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending Attachment via CDO 3 69
Don't Convert Time to Time-of-Day in Mail Merge 2 24
Problem to cell option 1 28
VB Script: Correction backup files and folders v3 11 25
Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

777 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