Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-20-2023, 04:58 PM
MDom MDom 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 2013
Novice
Macro to bold and center the first line of each page.
 
Join Date: Apr 2022
Posts: 4
MDom is on a distinguished road
Default Macro to bold and center the first line of each page.

Hi all,



Totally new to VBA but trying my best...I've searched for the answers but evidently dealing with pages is more challenging than I think it should be.

I have the following macro that bolds and centers the first line of the document, then goes through the first four pages of a document and selects and bolds the first line (which for me, is always a title). Then it goes back to the first page. So far it works great.

I believe a loop would be more efficient (especially since not every document is always four pages), but I'm failing miserably even getting started in building the loop. Can anyone help me turn what I have into a macro that loops through the document and bolds and centers the first line of each page? Thank you in advance!

Sub FirstLineCenter()
'
Dim ParaRange As range
Dim StartRange As range

Set StartRange = Selection.range

Set ParaRange = ActiveDocument.Paragraphs(1).range
ParaRange.Select
Selection.Font.Bold = True
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter

Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Count:=1
Set ParaRange = ActiveDocument.Bookmarks("\Page").range.Paragraphs (1).range
ParaRange.Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.Font.Bold = True

Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Count:=1
Set ParaRange = ActiveDocument.Bookmarks("\Page").range.Paragraphs (1).range
ParaRange.Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.Font.Bold = True

Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Count:=1
Set ParaRange = ActiveDocument.Bookmarks("\Page").range.Paragraphs (1).range
ParaRange.Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.Font.Bold = True

Selection.HomeKey Unit:=wdStory

End Sub
Reply With Quote
  #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
  #3  
Old 02-21-2023, 12:57 PM
MDom MDom 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 2013
Novice
Macro to bold and center the first line of each page.
 
Join Date: Apr 2022
Posts: 4
MDom is on a distinguished road
Default Thank you!

Brian,

This works, and I can follow most of the script. I modified a few things to fit my needs, but your scripts works great.
One questions if I could, what do these lines do:

' Begin undo record
Set bhhUndo = Application.UndoRecord
bhhUndo.StartCustomRecord (scriptName)

I tested them by commenting them out (and the next to last line) and it still worked. Am I risking anything by not including them?

I really appreciate the time you took to help me - thank you!
Reply With Quote
  #4  
Old 02-21-2023, 03:12 PM
Guessed's Avatar
Guessed Guessed 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 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,993
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

The UndoRecord is not necessary for your macro but it is handy if there is a chance you want to UNDO all the steps of the macro with a single keystroke.

Word's undo command steps backwards through the most recent document changes. When a macro performs a lot of changes to a document it takes a lot of consecutive undo commands to reverse out all the changes made by that macro. Those lines in Brian's code groups all of those single steps into a single undo command.

So if you don't expect to undo the macro's changes then it will make no difference to the code if you include or don't include those lines.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 02-21-2023, 09:43 PM
MDom MDom 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 2013
Novice
Macro to bold and center the first line of each page.
 
Join Date: Apr 2022
Posts: 4
MDom is on a distinguished road
Default

Andrew,

That makes sense - thank you for the explanation.
Reply With Quote
Reply



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 09:41 AM.


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