#1
|
|||
|
|||
VBA to convert a line of text to title case
I found this link which seems in the right direction; however, looking to start the proper case change after an em dash. I know the change is required in the Blue sections of the code below, but not understanding how to change it. The part about looking for the period I don't even need as there are no periods in the sentence.
Before AFMAN—AIR FORCE MANUAL AO—ACTION OFFICER CAF—CENTRAL ADJUDICATION FACILITY AFTER AFMAN—Air Force Manual AO—Action Officer CAF—Central Adjudication Facility Code:
Sub MakeTitle() Application.ScreenUpdating = False Dim StrTmp As String With Selection.Range StrTmp = Trim(.Text) While Right(StrTmp, 1) = "." StrTmp = Left(StrTmp, Len(StrTmp) - 1) Wend While InStr(StrTmp, " ") > 0 StrTmp = Replace(StrTmp, " ", " ") Wend StrTmp = TitleCase(StrTmp, bCaps:=False, bExcl:=False) .Text = StrTmp End With Application.ScreenUpdating = True End Sub |
#2
|
||||
|
||||
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Hi Paul,
This converts everything in the selection to title case. I was hoping to pinpoint only the text after the em dash? |
#4
|
||||
|
||||
So select only the content after the em-dash...
Or modify the macro to collapse the selection's start to the em-dash's position.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Okay thanks, I'll attempt this.
Quote:
|
#6
|
|||
|
|||
Hi Paul,
Okay I've found something to start with, but can't figure out how to more one past the em dash. Again, I'm going to start with something simple and then try to move on to the bigger task. This code below will allow me to bold the first part of this paragraph all that which is before the em dash. I have one line in my document for starters. ADPE—AUTOMATED DATA PROCESSING EQUIPMENT Running the macro below the .start is 5 and then the .end equals 4 so the paragraph collapses to the first 4 characters and bolds. Not sure how to get the start to be 6 and then proceed to the end and collapse? Code:
Sub Bold_Terms2() With Selection.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "^+" .Replacement.Text = "" .Forward = True .Format = False .MatchWildcards = False .Wrap = wdFindStop .Execute End With Do While .Find.Found If .Find.Found = True Then With .Duplicate .Start = .Paragraphs.First.Range.Start .End = .End - 1 .Font.Bold = True End With .Collapse wdCollapseEnd .Find.Execute End If Loop End With End Sub |
#7
|
|||
|
|||
I can't see any relevance between the code you have posted and the object you are after. What does "^+" have to do with Em dashes? What does .Font.Bold = True have to do with Title Case? What is all that gibberish about If .Find.Found?
If your text is like your example (all terms constitute as single paragraph then: Code:
Sub ConvertToTC() Dim oRng As Range Set oRng = ActiveDocument.Range With oRng.Find .Text = ChrW(8212) While .Execute With oRng .End = .Paragraphs(1).Range.End .Start = oRng.Start + 1 .Case = wdTitleWord .Collapse wdCollapseEnd End With Wend End With lbl_Exit: Exit Sub End Sub BREAK Paul, what makes an "Expert" in this forum? |
#8
|
|||
|
|||
Thank you Greg. Your time is truly appreciated.
Quote:
Sorry for the confusion. I was just trying to bite off a small piece like isolating everything to the right of the em dash and turning that portion bold. Quote:
Can you explain the purpose of the .Collapse when the line before, .Case, execute the title case? Quote:
|
#9
|
|||
|
|||
Hi Greg,
Okay, think I just found the answer to the .collapse. The range is collapsed to the end of the found range and then starts again |
#10
|
||||
|
||||
With the code in the link I supplied, after the line:
With Selection.Range insert: Code:
Do While InStr(.Text, Chr(151)) > 0 .Start = .Start + InStr(.Text, Chr(151)) Loop
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
Jeffrey,
Forgive the delay on reply. I was a little choked on feathers after dining on crow. I thought that was the symbol for the Em dash! You are right ^+ is the find symbol for the Em dash. I've just never used it. |
#12
|
|||
|
|||
No worries Greg. Thanks for the follow-up.
@Paul, I suppose I'm still doing something wrong here. I placed that bit of text after the line: With Selection.Range, but it only fixes the last entry in the selection. I stepped thru the code and can see it looping, but it doesn't loop one paragraph at a time. Code:
Sub MakeTitle() Application.ScreenUpdating = False Dim StrTmp As String With Selection.Range Do While InStr(.Text, Chr(151)) > 0 .Start = .Start + InStr(.Text, Chr(151)) Loop StrTmp = Trim(.Text) While Right(StrTmp, 1) = "." StrTmp = Left(StrTmp, Len(StrTmp) - 1) Wend While InStr(StrTmp, " ") > 0 StrTmp = Replace(StrTmp, " ", " ") Wend StrTmp = TitleCase(StrTmp, bCaps:=False, bExcl:=True) .Text = StrTmp End With Application.ScreenUpdating = True End Sub |
#13
|
||||
|
||||
It works that way because that's what post #6 implied was the requirement. You mentioned nothing there about multiple paragraphs being selected. You really do need to be clearer about your requirements.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#14
|
||||
|
||||
Given the example document, I would use
Code:
Sub Macro1() Dim oPara As Paragraph Dim oRng As Range For Each oPara In ActiveDocument.Paragraphs Set oRng = oPara.Range oRng.End = oRng.End - 1 oRng.MoveStartUntil Chr(151) oRng.Start = oRng.Start + 1 oRng.Case = wdTitleWord Next oPara Set oPara = Nothing Set oRng = Nothing End Sub Code:
Sub Macro2() Dim oRng As Range Set oRng = ActiveDocument.Range With oRng.Find Do While .Execute(Chr(151)) oRng.End = oRng.Paragraphs(1).Range.End - 1 oRng.Case = wdTitleWord oRng.Collapse 0 Loop End With Set oRng = Nothing End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#15
|
|||
|
|||
Thank you Graham. This works great also.
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How do I convert a line of text to title case? | neilfxb | Word | 21 | 02-22-2023 12:47 PM |
Macro that formats (true title) case by Heading style | Marrick13 | Word VBA | 13 | 09-20-2015 06:29 PM |
Customising a style that uses Title Case formatting | Madanjeet | Word | 6 | 05-18-2015 10:11 AM |
Stop review query when small case at beginning of line | dsrose | Word | 2 | 01-22-2014 12:19 AM |
True Title Case for First Row of All Tables | Marrick13 | Word VBA | 14 | 12-11-2013 09:12 PM |