Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-04-2019, 08:38 AM
jeffreybrown jeffreybrown is offline VBA to convert a line of text to title case Windows 10 VBA to convert a line of text to title case Office 2016
Expert
VBA to convert a line of text to title case
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default 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
Reply With Quote
  #2  
Old 09-04-2019, 03:22 PM
macropod's Avatar
macropod macropod is offline VBA to convert a line of text to title case Windows 7 64bit VBA to convert a line of text to title case Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

See: https://www.msofficeforums.com/word/...itle-case.html
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 09-04-2019, 03:55 PM
jeffreybrown jeffreybrown is offline VBA to convert a line of text to title case Windows 10 VBA to convert a line of text to title case Office 2016
Expert
VBA to convert a line of text to title case
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

Hi Paul,

This converts everything in the selection to title case. I was hoping to pinpoint only the text after the em dash?
Reply With Quote
  #4  
Old 09-04-2019, 03:57 PM
macropod's Avatar
macropod macropod is offline VBA to convert a line of text to title case Windows 7 64bit VBA to convert a line of text to title case Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 09-04-2019, 04:02 PM
jeffreybrown jeffreybrown is offline VBA to convert a line of text to title case Windows 10 VBA to convert a line of text to title case Office 2016
Expert
VBA to convert a line of text to title case
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

Okay thanks, I'll attempt this.

Quote:
Or modify the macro to collapse the selection's start to the em-dash's position
Reply With Quote
  #6  
Old 09-05-2019, 03:08 PM
jeffreybrown jeffreybrown is offline VBA to convert a line of text to title case Windows 10 VBA to convert a line of text to title case Office 2016
Expert
VBA to convert a line of text to title case
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

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
Reply With Quote
  #7  
Old 09-05-2019, 04:01 PM
gmaxey gmaxey is offline VBA to convert a line of text to title case Windows 10 VBA to convert a line of text to title case Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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?
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #8  
Old 09-05-2019, 04:51 PM
jeffreybrown jeffreybrown is offline VBA to convert a line of text to title case Windows 10 VBA to convert a line of text to title case Office 2016
Expert
VBA to convert a line of text to title case
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

Thank you Greg. Your time is truly appreciated.

Quote:
I can't see any relevance between the code you have posted and the object you are after.
I wasn't trying to imply there was a relevance. I'm just trying to learn. This Word VBA has been quite the challenge to grasp, but I truly appreciate your guidance.

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:
What is all that gibberish about If .Find.Found
This is something in code Paul has posted in the past, but surely not saying it was necessary here.

Can you explain the purpose of the .Collapse when the line before, .Case, execute the title case?

Quote:
What does "^+" have to do with Em dashes?
I thought that was the symbol for the Em dash!
Reply With Quote
  #9  
Old 09-05-2019, 05:02 PM
jeffreybrown jeffreybrown is offline VBA to convert a line of text to title case Windows 10 VBA to convert a line of text to title case Office 2016
Expert
VBA to convert a line of text to title case
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

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
Reply With Quote
  #10  
Old 09-05-2019, 06:08 PM
macropod's Avatar
macropod macropod is offline VBA to convert a line of text to title case Windows 7 64bit VBA to convert a line of text to title case Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #11  
Old 09-05-2019, 07:34 PM
gmaxey gmaxey is offline VBA to convert a line of text to title case Windows 10 VBA to convert a line of text to title case Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #12  
Old 09-06-2019, 02:44 PM
jeffreybrown jeffreybrown is offline VBA to convert a line of text to title case Windows 10 VBA to convert a line of text to title case Office 2016
Expert
VBA to convert a line of text to title case
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

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
I don't have any periods in these paragraphs so I would suppose the part in Red is not necessary?
Attached Files
File Type: docm TestFile.docm (26.3 KB, 6 views)
Reply With Quote
  #13  
Old 09-06-2019, 04:01 PM
macropod's Avatar
macropod macropod is offline VBA to convert a line of text to title case Windows 7 64bit VBA to convert a line of text to title case Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #14  
Old 09-08-2019, 12:24 AM
gmayor's Avatar
gmayor gmayor is offline VBA to convert a line of text to title case Windows 10 VBA to convert a line of text to title case Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
or using Find
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
Reply With Quote
  #15  
Old 09-09-2019, 05:37 AM
jeffreybrown jeffreybrown is offline VBA to convert a line of text to title case Windows 10 VBA to convert a line of text to title case Office 2016
Expert
VBA to convert a line of text to title case
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

Thank you Graham. This works great also.
Reply With Quote
Reply

Thread Tools
Display Modes


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
VBA to convert a line of text to title case 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
VBA to convert a line of text to title case True Title Case for First Row of All Tables Marrick13 Word VBA 14 12-11-2013 09:12 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:28 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft