Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-28-2020, 12:11 AM
delboy delboy is offline I need to capitalize first word of every line Windows 7 I need to capitalize first word of every line Office 2007
Novice
I need to capitalize first word of every line
 
Join Date: Mar 2010
Posts: 11
delboy is on a distinguished road
Default I need to capitalize first word of every line

I have a 200 page document and need to capitalize the first word in every line. They are not sentences so I can't use the existing format option. Is there macro that will do this for me?



I know I can go into options and have it done automatically but that is only for new typing and won't work on existing text.
Reply With Quote
  #2  
Old 09-28-2020, 12:35 AM
gmayor's Avatar
gmayor gmayor is offline I need to capitalize first word of every line Windows 10 I need to capitalize first word of every line Office 2016
Expert
 
Join Date: Aug 2014
Posts: 3,301
gmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to all
Default

What constitutes a 'line' in the document? There are no 'lines' in a Word document. Formatting is by text flow between the current margins. If the 'lines' are merely an artefact of text flow then capitalising the first Word on each displayed line is likely to reflow the document as Capitals can take up more space then the lower case letters they replace.
If the 'lines' are actually paragraphs then the matter is rather more straightforward.
Code:
Dim oPara As Paragraph
    For Each oPara In ActiveDocument.Paragraphs
        oPara.Range.Characters(1).Case = wdUpperCase
    Next oPara
    Set oPara = Nothing
__________________
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
  #3  
Old 09-28-2020, 12:47 AM
delboy delboy is offline I need to capitalize first word of every line Windows 7 I need to capitalize first word of every line Office 2007
Novice
I need to capitalize first word of every line
 
Join Date: Mar 2010
Posts: 11
delboy is on a distinguished road
Default

The bad news is I didn't understand a word of what you said but the good news is your macro word brilliantly! Thanks for the amazingly quick response.

It is for a collection of song lyrics.
Reply With Quote
  #4  
Old 09-28-2020, 04:17 AM
gmayor's Avatar
gmayor gmayor is offline I need to capitalize first word of every line Windows 10 I need to capitalize first word of every line Office 2016
Expert
 
Join Date: Aug 2014
Posts: 3,301
gmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to all
Default

One out of two is not that bad
__________________
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
  #5  
Old 09-28-2020, 07:38 AM
gmaxey gmaxey is online now I need to capitalize first word of every line Windows 10 I need to capitalize first word of every line Office 2016
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 1,106
gmaxey has a spectacular aura aboutgmaxey has a spectacular aura aboutgmaxey has a spectacular aura about
Default

delboy,


Graham already knows this, but for the sake of completeness and for others looking to do something similar, I'm going to mention it.

Graham is spot on about lines and pages being sort of an illusion in a Word document, but interestingly enough, in "Print Layout" view there are things called "PAGES, RECTANGLES AND LINES" that can be leveraged with VBA for useful things.

Not to mention any names, but lets say we have an elderly, bumbling and forgetful politician giving a speech and we want to use a PC with an open Word document as his (or her) teleprompter. We want to avoid gaffs so we are going to automatically scroll through the document line by line highlighting the text and pausing for say half a second on each LINE.


Code:
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private bStop As Boolean

Sub StopScroll()
  bStop = True
End Sub
Sub ScrollByLine()
Dim oPage As Page
Dim lngRec As Long, lngLine As Long
Dim lngHLCI As Long
Dim oRng As Word.Range
  bStop = False
  For Each oPage In ActiveDocument.ActiveWindow.ActivePane.Pages
    For lngRec = 1 To oPage.Rectangles.Count
      For lngLine = 1 To oPage.Rectangles(lngRec).Lines.Count
        lngHLCI = oPage.Rectangles(lngRec).Lines(lngLine).Range.HighlightColorIndex
        oPage.Rectangles(lngRec).Lines(lngLine).Range.Select
        Set oRng = Selection.Range
        oRng.Characters(1) = UCase(oRng.Characters(1))
        Selection.Collapse wdCollapseEnd
        oRng.HighlightColorIndex = wdYellow
        Doze 500 'half second
        DoEvents
        ActiveDocument.ActiveWindow.SmallScroll Down:=1 'one line
        oRng.HighlightColorIndex = lngHLCI
        Application.ScreenRefresh
        If bStop Then GoTo lbl_Exit
      Next lngLine
    Next lngRec
  Next oPage
lbl_Exit:
  Exit Sub
End Sub

Sub Doze(ByVal lngPeriod As Long)
  DoEvents
  Sleep lngPeriod
End Sub

Graham's code worked perfectly for you because your document is set up where each line is a separate paragraph. Had you used line breaks (Shift+Enter) to create new lines or if you have simply typed several lines of text and "still" wanted each of those lines capitalized, then by extension of what you have seen above, you could have used this:

Code:
Sub CapFirstLine()
Dim oPage As Page
Dim lngRec As Long, lngLine As Long
Dim lngHLCI As Long
Dim oRng As Word.Range
  bStop = False
  For Each oPage In ActiveDocument.ActiveWindow.ActivePane.Pages
    For lngRec = 1 To oPage.Rectangles.Count
      For lngLine = 1 To oPage.Rectangles(lngRec).Lines.Count
        oPage.Rectangles(lngRec).Lines(lngLine).Range.Characters(1) = _
           UCase(oPage.Rectangles(lngRec).Lines(lngLine).Range.Characters(1))
      Next lngLine
    Next lngRec
  Next oPage
lbl_Exit:
  Exit Sub
End Sub

Again, as Graham has pointed out, a CAP letter takes up more space than a lower case letter so the result of running that code on multi-line paragraphs could be disappointing.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
I need to capitalize first word of every line Capitalize one to two words if they are the only words on a line jrhlavac Word VBA 1 10-08-2015 08:19 PM
Create exceptions to the "Capitalize Each Word" command TonyTyner@comcast.net PowerPoint 0 03-13-2015 09:13 AM
Newbie - Need help with VBA code to capitalize last line of addresses jjreg Word VBA 2 11-07-2014 10:19 PM
I need to capitalize first word of every line Only Capitalize First Letter & Delete Last Name zulhfreelancer Excel 5 12-10-2012 07:51 AM
capitalize first letter of sentences norco1 Word 0 06-25-2006 12:37 PM

Other Forums: Access Forums - Senior Forums

All times are GMT -7. The time now is 06:25 AM.


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