View Single Post
 
Old 09-28-2020, 07:38 AM
gmaxey gmaxey is offline Windows 10 Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
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

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