#1
|
|||
|
|||
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 |
#2
|
|||
|
|||
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 |
#3
|
|||
|
|||
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! |
#4
|
||||
|
||||
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 |
#5
|
|||
|
|||
Andrew,
That makes sense - thank you for the explanation. |
|
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 |
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 |