Could some kind soul provide a
simple macro to cycle among several page border settings? I'd like to assign it to a keyboard shortcut (Alt+B) and have it cycle through these settings each time I invoke it:
- Page border measured from the text (1 pt all around).
- Page border measured from the edge of the page (24 pt all around).
- Both 1 & 2.
- None (all borders off).
The macro would detect the current setting, figure out which of the above it is (best guess), then set it to the next in the list. If #4, start over with #1. If it can't figure out the current setting, set it to #4 (all off).
Using the macro recorder, I was able to create one macro to set it to #1 and one to turn it off (#4). I also have a macro that seems to detect these two, but they seem too complicated. Before I try to add steps 2 & 3, could someone critique what I have or provide better code?
Thanks
Code:
'===========================================================================
' Toggle Page Border (Alt-B)
' Toggle the Page Border (on/off)
' For custom cards and envelopes template, to see where the real text borders are.
'===========================================================================
Sub MyPageBorderToggle()
' Is this the right code or even necessary?
' I'm setting a page border, not a paragraph border.
With Selection.Sections(1)
If .Borders(wdBorderLeft).LineStyle Or _
.Borders(wdBorderRight).LineStyle Or _
.Borders(wdBorderTop).LineStyle Or _
.Borders(wdBorderBottom).LineStyle Then 'If any border is on,
Call MyPageBorderToggleOff 'Turn them all off.
' MsgBox "Page borders off"
Else 'If they are all off,
Call MyPageBorderToggleOn 'Turn them all on.
' MsgBox "Page borders on"
End If
End With
End Sub
Code:
Sub MyPageBorderToggleOff()
With Selection.Sections(1)
.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
.Borders(wdBorderRight).LineStyle = wdLineStyleNone
.Borders(wdBorderTop).LineStyle = wdLineStyleNone
.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
End With
End Sub
Code:
Sub MyPageBorderToggleOn()
With Selection.Sections(1)
'' This seems redundant
With Options
' .DefaultBorderLineStyle = wdLineStyleSingle
.DefaultBorderLineWidth = wdLineWidth100pt
.DefaultBorderColor = wdColorAutomatic
End With
' ' Turn on each edge individually.
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
' .LineWidth = wdLineWidth025pt
' .Color = wdColorAutomatic
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleSingle
' .LineWidth = wdLineWidth025pt
' .Color = wdColorAutomatic
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
' .LineWidth = wdLineWidth025pt
' .Color = wdColorAutomatic
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
' .LineWidth = wdLineWidth025pt
' .Color = wdColorAutomatic
End With
' Relate it to the text, not the page. This must be done last.
With .Borders
.DistanceFrom = wdBorderDistanceFromText
' .AlwaysInFront = True
' .SurroundHeader = True
' .SurroundFooter = True
' .JoinBorders = False
.DistanceFromTop = 0
.DistanceFromLeft = 0
.DistanceFromBottom = 0
.DistanceFromRight = 0
' .Shadow = False
' .EnableFirstPageInSection = True
' .EnableOtherPagesInSection = True
' .ApplyPageBordersToAllSections
End With
End With
End Sub