Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-13-2025, 08:45 AM
turnsouth turnsouth is offline How do I insert a horizontal line at the end of a document? Windows 11 How do I insert a horizontal line at the end of a document? Office 2021
Novice
How do I insert a horizontal line at the end of a document?
 
Join Date: Apr 2025
Posts: 4
turnsouth is on a distinguished road
Default How do I insert a horizontal line at the end of a document?

I've been trying to get this macro to work correctly.



It's for adding a new journal entry. It is suppose to go to the end of the document, insert a horizontal line, and then the date and time.

The date and time work correctly, but for some reason I can only get it to insert the line at the top of the document instead of the end.

I imagine that it has something to do with the range parameter of AddHorizontalLineStandard. But for the life of me, no matter what VBA reference or example I look at, I can not find what the values of 'range" can or is suppose to be.

Thanks,
Paul


Code:
Sub New_Journal_Entry()
'
' Macro2 Macro
'
'
    Selection.EndKey Unit:=wdStory
    ActiveDocument.InlineShapes.AddHorizontalLineStandard
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.InsertDateTime DateTimeFormat:="dddd, MMMM d, yyyy", _
        InsertAsField:=False, DateLanguage:=wdEnglishUS, CalendarType:= _
        wdCalendarWestern, InsertAsFullWidth:=False
    Selection.TypeParagraph
    Selection.InsertDateTime DateTimeFormat:="h:mm am/pm", InsertAsField:= _
        False, DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _
        InsertAsFullWidth:=False
        Selection.TypeParagraph
        Selection.TypeParagraph
End Sub
Reply With Quote
  #2  
Old 04-13-2025, 10:37 AM
vivka vivka is offline How do I insert a horizontal line at the end of a document? Windows 7 64bit How do I insert a horizontal line at the end of a document? Office 2016
Expert
 
Join Date: Jul 2023
Posts: 293
vivka is on a distinguished road
Default

Hi! What about this one? This code will insert a line above the doc's last paragraph.
Code:
   
Selection.EndKey wdStory
Selection.InlineShapes.AddHorizontalLineStandard
Reply With Quote
  #3  
Old 04-13-2025, 10:41 AM
turnsouth turnsouth is offline How do I insert a horizontal line at the end of a document? Windows 11 How do I insert a horizontal line at the end of a document? Office 2021
Novice
How do I insert a horizontal line at the end of a document?
 
Join Date: Apr 2025
Posts: 4
turnsouth is on a distinguished road
Default

That did it!

Thank you.
Reply With Quote
  #4  
Old 04-13-2025, 10:54 AM
vivka vivka is offline How do I insert a horizontal line at the end of a document? Windows 7 64bit How do I insert a horizontal line at the end of a document? Office 2016
Expert
 
Join Date: Jul 2023
Posts: 293
vivka is on a distinguished road
Default

You are welcome!
Reply With Quote
  #5  
Old 04-13-2025, 11:58 AM
Italophile Italophile is offline How do I insert a horizontal line at the end of a document? Windows 11 How do I insert a horizontal line at the end of a document? Office 2021
Expert
 
Join Date: Mar 2022
Posts: 538
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Instead of adding a shape and a bunch of empty paragraphs it would be better to create two new styles - Journal Date with a top border set above the paragraph by a suitable distance (20pts in the screenshot) and a suitable space before (12pt in the screenshot), and Journal Date with a suitable space after (12pt in the screenshot).

Screenshot 2025-04-13 195654.png

Your code can then be:
Code:
Sub NewJournalEntry()
    With ActiveDocument.Paragraphs
        With .Last.Range
            'ensure that there is an empty paragraph at the end of the document
            .InsertParagraphAfter
        End With
        With .Last.Range
            .InsertDateTime DateTimeFormat:="dddd, MMMM d, yyyy", _
                InsertAsField:=False, DateLanguage:=wdEnglishUS, CalendarType:= _
                wdCalendarWestern, InsertAsFullWidth:=False
            .Style = "Journal Date"
        End With
        .Last.Range.InsertParagraphAfter
        With .Last.Range
            .InsertDateTime DateTimeFormat:="h:mm am/pm", InsertAsField:= _
                False, DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _
                InsertAsFullWidth:=False
            .Style = "Journal Time"
        End With
        .Last.Range.InsertParagraphAfter
        .Last.Style = wdStyleNormal
    End With
    
End Sub
Reply With Quote
  #6  
Old 04-14-2025, 06:00 AM
gmaxey gmaxey is offline How do I insert a horizontal line at the end of a document? Windows 10 How do I insert a horizontal line at the end of a document? Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,598
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

As an alternative, you can use a buildingblock and be macro free.


Select an existing Journal entry or better yet create a new one along the lines suggested by Italophile (with or without creating styles), select it, press ALT+F3 to create the BuildingBlock.


Now you can insert a new journal entry header by simply typing "jour..." and pressing Enter when the autocomplete tip text line appears.


Advantages:
Macro free
No shapes
No empty paragraphs


Disadvantages:
If you share the document with others, you must share the template as well


If you want to use some keyboard shortcut with a macro:


Code:
Sub InsertJE()
  On Error GoTo Err_NotFound
  ActiveDocument.AttachedTemplate.BuildingBlockEntries("journalentry").Insert _
               Selection.Range, RichText:=True
  Exit Sub
lbl_Exit:
  Exit Sub
Err_NotFound:
  MsgBox "The building block ""journalentry"" was not found in " _
          & ThisDocument.Name & " attached template.", vbInformation + vbOKOnly, _
          "MISSING BUILDING BLOCK"
  Resume lbl_Exit
End Sub
Attached Images
File Type: jpg BB Text.jpg (39.1 KB, 8 views)
File Type: jpg BB Definition.jpg (52.4 KB, 8 views)
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old 04-14-2025, 07:10 AM
gmayor's Avatar
gmayor gmayor is offline How do I insert a horizontal line at the end of a document? Windows 10 How do I insert a horizontal line at the end of a document? Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

It must be a slow day. Here's another way
Code:
Sub Macro1()
Dim oRng As Range
    Set oRng = ActiveDocument.Range
    oRng.Collapse 0
    oRng.Text = vbCr & vbCr
    With oRng.ParagraphFormat.Borders(wdBorderBottom)
        .LineStyle = wdLineStyleSingle
        .LineWidth = wdLineWidth225pt
    End With
    oRng.Collapse 0
    oRng.Text = Format(Date, "dddd, MMMM d, yyyy") & vbCr & Format(Time, "h:mm am/pm")
lbl_Exit:
    Set oRng = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I insert a horizontal line at the end of a document? Creating a horizontal line in a document jjjoverthere Word 2 11-03-2018 05:39 PM
How do I insert a horizontal line at the end of a document? Horizontal line I can't delete goingvirtual Word 2 09-09-2018 02:58 AM
How do I insert a horizontal line at the end of a document? MS Word Horizontal Line Disappears after pressing Enter from end of line MikeWhite Word 5 01-20-2017 03:39 PM
How to change the default features for AutoShape horizontal line (button 'Insert horizontal line') 534tomasz Word 6 10-13-2016 02:33 AM
How do I insert a horizontal line at the end of a document? Putting a horizontal line in a Word Document -- should be simple, right? biotechguy Word 4 05-10-2010 11:58 AM

Other Forums: Access Forums

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