H
Heather Mills
I want to write a macro to toggle the page borders on and off.
I recorded the code for turning them on and off and after some
fiddling, came up with the code below. It seems to work, but I have a
few questions and would appreciate any suggestions for improvements.
I ended up with three macros. The main macro queries whether borders
are on or not and then calls one to turn them on or off.
'=========================================================
' Main Macro
' Query the borders, then call the on or off macro.
'=========================================================
Sub MyPageBorderToggle()
With Selection.Sections(1) (1)
If .Borders(wdBorderLeft).LineStyle Or _ (2)
.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
Questions:
1. Is the Selection.Sections(1) the right code? I'm setting a *page*
border, not a *paragraph* border.
2. Is ".borders.linestyle" the right setting to test?
'=========================================================
' Macro to Turn Page Borders Off
'=========================================================
Sub MyPageBorderToggleOff()
With Selection.Sections(1) (1)
.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
.Borders(wdBorderRight).LineStyle = wdLineStyleNone
.Borders(wdBorderTop).LineStyle = wdLineStyleNone
.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
End With
End Sub
Questions:
1. Apparently, the selection is not passed to the subroutine, so I
need to do the Selection.Sections(1) again, right?
2. Is there anything else I need to do?
'=========================================================
' Macro to Turn Page Borders On
'=========================================================
Sub MyPageBorderToggleOn()
With Selection.Sections(1)
' This code was at the end in the recorded macro (1)
With Options
' .DefaultBorderLineStyle = wdLineStyleSingle (2)
.DefaultBorderLineWidth = wdLineWidth100pt
.DefaultBorderColor = wdColorAutomatic
End With
' Turn on each edge individually. (3)
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
' .LineWidth = wdLineWidth025pt (4)
' .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. (5)
With .Borders
.DistanceFrom = wdBorderDistanceFromText
' .AlwaysInFront = True (6)
' .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
Questions:
1. The With Options code was at the end of the recorded macro. I moved
it to the top, which allowed me to eliminate the LineWidth and Color
statements. Any reason this had to be at the end?
2. The LineStyle statement seems redundant, since I have to do it for
each edge. I tried eliminating that and it failed.
3. I assume there is no "Box" group that will set all 4 edges with one
commend...
4. I eliminated these statements, because they are in the With Options
code above. Any problem with that?
5. I tried moving this to the top, but BorderDistanceFromText didn't
stick.
6. I eliminated a bunch of state,ments that didn't seem to make any
difference for what I'm doing. Any problems with this?
I recorded the code for turning them on and off and after some
fiddling, came up with the code below. It seems to work, but I have a
few questions and would appreciate any suggestions for improvements.
I ended up with three macros. The main macro queries whether borders
are on or not and then calls one to turn them on or off.
'=========================================================
' Main Macro
' Query the borders, then call the on or off macro.
'=========================================================
Sub MyPageBorderToggle()
With Selection.Sections(1) (1)
If .Borders(wdBorderLeft).LineStyle Or _ (2)
.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
Questions:
1. Is the Selection.Sections(1) the right code? I'm setting a *page*
border, not a *paragraph* border.
2. Is ".borders.linestyle" the right setting to test?
'=========================================================
' Macro to Turn Page Borders Off
'=========================================================
Sub MyPageBorderToggleOff()
With Selection.Sections(1) (1)
.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
.Borders(wdBorderRight).LineStyle = wdLineStyleNone
.Borders(wdBorderTop).LineStyle = wdLineStyleNone
.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
End With
End Sub
Questions:
1. Apparently, the selection is not passed to the subroutine, so I
need to do the Selection.Sections(1) again, right?
2. Is there anything else I need to do?
'=========================================================
' Macro to Turn Page Borders On
'=========================================================
Sub MyPageBorderToggleOn()
With Selection.Sections(1)
' This code was at the end in the recorded macro (1)
With Options
' .DefaultBorderLineStyle = wdLineStyleSingle (2)
.DefaultBorderLineWidth = wdLineWidth100pt
.DefaultBorderColor = wdColorAutomatic
End With
' Turn on each edge individually. (3)
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
' .LineWidth = wdLineWidth025pt (4)
' .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. (5)
With .Borders
.DistanceFrom = wdBorderDistanceFromText
' .AlwaysInFront = True (6)
' .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
Questions:
1. The With Options code was at the end of the recorded macro. I moved
it to the top, which allowed me to eliminate the LineWidth and Color
statements. Any reason this had to be at the end?
2. The LineStyle statement seems redundant, since I have to do it for
each edge. I tried eliminating that and it failed.
3. I assume there is no "Box" group that will set all 4 edges with one
commend...
4. I eliminated these statements, because they are in the With Options
code above. Any problem with that?
5. I tried moving this to the top, but BorderDistanceFromText didn't
stick.
6. I eliminated a bunch of state,ments that didn't seem to make any
difference for what I'm doing. Any problems with this?