![]() |
|
#1
|
|||
|
|||
|
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
|
|
#2
|
|||
|
|||
|
Hi! What about this one? This code will insert a line above the doc's last paragraph.
Code:
Selection.EndKey wdStory Selection.InlineShapes.AddHorizontalLineStandard |
|
#3
|
|||
|
|||
|
That did it!
Thank you. |
|
#4
|
|||
|
|||
|
You are welcome!
|
|
#5
|
|||
|
|||
|
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
|
|
#6
|
|||
|
|||
|
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
|
|
#7
|
||||
|
||||
|
It must be a slow day.
Here's another wayCode:
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 |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Creating a horizontal line in a document
|
jjjoverthere | Word | 2 | 11-03-2018 05:39 PM |
Horizontal line I can't delete
|
goingvirtual | Word | 2 | 09-09-2018 02:58 AM |
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 |
Putting a horizontal line in a Word Document -- should be simple, right?
|
biotechguy | Word | 4 | 05-10-2010 11:58 AM |