Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-16-2025, 12:53 PM
Younan Younan is offline Line above first line Windows 10 Line above first line Office 2016
Novice
Line above first line
 
Join Date: Feb 2023
Posts: 12
Younan is on a distinguished road
Default Line above first line

Hi,

I need a VBA to make a line on the top of every page in a word document ( more than 250 pages ) . it can be done by writing 3 hyphens and click enter afterwards but it would take long to do it over the whole document.
Any hep appreciated, thanks in advance.



Younan
Reply With Quote
  #2  
Old 12-16-2025, 01:50 PM
macropod's Avatar
macropod macropod is offline Line above first line Windows 10 Line above first line Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,527
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

So why not do it once in the page header? No VBA required.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 12-17-2025, 12:07 PM
Younan Younan is offline Line above first line Windows 10 Line above first line Office 2016
Novice
Line above first line
 
Join Date: Feb 2023
Posts: 12
Younan is on a distinguished road
Default

Hi,

Thanks for your reply, i am asking for VBA cause above this line i will write different text almost in every page, which as i think cant be done with header.
bEst regards

Younan
Reply With Quote
  #4  
Old 12-17-2025, 01:18 PM
Italophile Italophile is offline Line above first line Windows 11 Line above first line Office 2021
Expert
 
Join Date: Mar 2022
Posts: 566
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Then apply a style to the text that you will write at the top of each page that includes a bottom border.
If you insert the line using VBA at the top of each page and then add text before it the line on subsequent pages will not be at the top of the page any more.
Reply With Quote
  #5  
Old 12-17-2025, 02:37 PM
Younan Younan is offline Line above first line Windows 10 Line above first line Office 2016
Novice
Line above first line
 
Join Date: Feb 2023
Posts: 12
Younan is on a distinguished road
Default

Hi

I ll show a picture on how it appears in my file if i use hyphen to insert a line.
The text in line with number one is what will change almost in every page. Any ideas ?
Attached Images
File Type: jpg Sample.jpg (23.4 KB, 10 views)
Reply With Quote
  #6  
Old 12-17-2025, 02:46 PM
macropod's Avatar
macropod macropod is offline Line above first line Windows 10 Line above first line Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,527
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

If the changing text is just the page number, then all you need to do is add a page field to the header along with your static text. Otherwise, you could use something like:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim p As Long, Shp As Shape, sWdth As Single
With ActiveDocument
    With .PageSetup
      sWdth = .PageWidth - .LeftMargin - .RightMargin - .Gutter
    End With
  Do While p < .ComputeStatistics(wdStatisticPages)
    p = p + 1
    Set Shp = .Shapes.AddTextbox(msoTextOrientationHorizontal, 0, 0, sWdth, 24, .GoTo(What:=wdGoToPage, Name:=p))
    With Shp
      .LockAnchor = True
      .Line.Visible = False
      .RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
      .Left = wdShapeCenter
      .RelativeVerticalPosition = wdRelativeVerticalPositionMargin
      .Top = -18
      With .TextFrame.TextRange.ParagraphFormat
        .SpaceBefore = 0
        .SpaceAfter = 0
        .Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
      End With
    End With
  Loop
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 12-18-2025, 11:54 AM
Charles Kenyon Charles Kenyon is offline Line above first line Windows 11 Line above first line Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,601
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Quote:
Originally Posted by Younan View Post
Hi,

Thanks for your reply, i am asking for VBA cause above this line i will write different text almost in every page, which as i think cant be done with header.
bEst regards

Younan

Look into the StyleRef Field. That lets you have a single header that has different text on every page depending on the content of the page.
You can have a bottom border for the paragraph. This would best be done as a modification of the Header style.
By the way, when you type the thee hyphens --- and press Enter, what you get is a bottom paragraph border created by AutoFormat As You Type.

See also Word Doesn't Know What a Page Is by Daiya Mitchell, MVP( https://wordmvp.com/Mac/PagesInWord.html).
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
1st line of 4-line poem centrally aligned; how to get lines 2-4 to start at same location on page Swarup Word 6 09-16-2022 11:07 AM
Line above first line Making mail merge blank fill a line to highlight that line rgm60527 Mail Merge 2 02-22-2022 11:13 AM
Line above first line word erases line bottom in tight line spacing when new line is added ozzzy Word 2 01-21-2021 06:41 AM
Usability of space between final line of body text and footnote separator line Swarup Word 6 07-28-2018 12:51 PM
Line above first line MS Word Horizontal Line Disappears after pressing Enter from end of line MikeWhite Word 5 01-20-2017 03:39 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:42 AM.


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