Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 02-20-2023, 06:29 PM
BrianHoard BrianHoard is offline Macro to bold and center the first line of each page. Windows 10 Macro to bold and center the first line of each page. Office 2019
Advanced Beginner
 
Join Date: Jul 2022
Location: Haymarket, VA USA
Posts: 85
BrianHoard is on a distinguished road
Default

Your script looks a whole lot like my first VBA script before I knew how to make a loop. One challenge I think you are going to face with trying to access the first line of a page, is that may not be a reliable thing to find as paragraphs move about and text flows from page to page.

But, here is a start which at least shows you how you can loop through the document and get close. Unless you have some more consistent/fixed way to find the first line of each page. In this script, I also made this text red to make it easy to see what it's doing. In my test, what was originally the first line of a page, was moved, so subsequent "first lines" were unpredictable.
Code:
Sub formatFirstLine()

    Dim rngPageStart As Range
    Dim rngFirstLine As Range
    Dim i As Integer
    Dim scriptName As String
    
    scriptName = "formatFirstLine"
        
    Application.ScreenUpdating = False
    
    ' Begin undo record
    Set bhhUndo = Application.UndoRecord
    bhhUndo.StartCustomRecord (scriptName)
    
    'Loop through all pages in the document
    For i = 1 To ActiveDocument.BuiltInDocumentProperties("Number of Pages")
    
        'Go to the top of each page
        Set rngPageStart = ActiveDocument.GoTo(wdGoToPage, wdGoToAbsolute, i)
        
        'Select the first paragraph of the page
        Set rngFirstLine = rngPageStart.Paragraphs(1).Range
   '     rngFirstLine.End = rngFirstLine.End + 1 'extend the range to the next line
        
        'Apply formatting
        With rngFirstLine.ParagraphFormat
            .Alignment = wdAlignParagraphCenter
            .SpaceBeforeAuto = False
            .SpaceBefore = 0
            .SpaceAfterAuto = False
            .SpaceAfter = 0
            .LineSpacingRule = wdLineSpaceSingle
        End With
        
        With rngFirstLine.Font
            .Bold = True
            .ColorIndex = wdRed
        End With
    Next i
    
    Application.ScreenUpdating = True
    Application.ScreenRefresh
    bhhUndo.EndCustomRecord ' End undo
    
End Sub
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to make a LINE BOLD under cursor Retko Word VBA 4 01-28-2022 03:38 AM
Macro to bold and center the first line of each page. Justify left center and right center on same line DiGug Word 3 10-02-2014 02:43 PM
Format Bold in one line makes all lines bold Nitte Word 2 02-07-2013 12:34 AM
How to Center One Line Vertically on Blank Page SQLUSA Word 1 08-29-2012 08:14 PM
Macro that will go to line # and center text marge Word VBA 0 09-10-2010 12:30 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:23 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