Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #5  
Old 09-28-2020, 07:38 AM
gmaxey gmaxey 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: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
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
 



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

All times are GMT -7. The time now is 10:59 PM.


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