Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-03-2012, 10:17 AM
Jennifer Murphy's Avatar
Jennifer Murphy Jennifer Murphy is offline Macro to set page borders Windows XP Macro to set page borders Office 2007
Competent Performer
Macro to set page borders
 
Join Date: Aug 2011
Location: Silicon Valley
Posts: 234
Jennifer Murphy is on a distinguished road
Default Macro to set page borders

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:
  1. Page border measured from the text (1 pt all around).
  2. Page border measured from the edge of the page (24 pt all around).
  3. Both 1 & 2.
  4. 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
Reply With Quote
  #2  
Old 11-04-2012, 02:45 AM
macropod's Avatar
macropod macropod is offline Macro to set page borders Windows 7 64bit Macro to set page borders Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

Hi Jennifer,

You can't have option 3. That aside, try:
Code:
Sub Borders()
Application.ScreenUpdating = False
Dim i As Long
With Selection.Sections(1)
  If .Borders(wdBorderLeft).LineStyle = wdLineStyleNone Then
    For i = 1 To 4
      With .Borders(i)
      .LineStyle = wdLineStyleSingle
      .LineWidth = wdLineWidth025pt
      .Color = wdColorAutomatic
      End With
    Next
    With .Borders
      .DistanceFrom = wdBorderDistanceFromText
      .DistanceFromTop = 1
      .DistanceFromLeft = 1
      .DistanceFromBottom = 1
      .DistanceFromRight = 1
    End With
  ElseIf .Borders.DistanceFrom = wdBorderDistanceFromText Then
    With .Borders
      .DistanceFrom = wdBorderDistanceFromPageEdge
      .DistanceFromTop = 24
      .DistanceFromLeft = 24
      .DistanceFromBottom = 24
      .DistanceFromRight = 24
    End With
  Else
    .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
    .Borders(wdBorderRight).LineStyle = wdLineStyleNone
    .Borders(wdBorderTop).LineStyle = wdLineStyleNone
    .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
  End If
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to set page borders Page-crossing borders in a table with hidden between-cell borders tenpaiman Word 2 08-08-2012 07:20 PM
Macro to set page borders merging documents in macro page no Rose_Garden Word 1 03-31-2012 04:50 AM
Macro to insert new page... samanthaj Word 17 01-31-2012 01:53 PM
Page borders vs Orientation Ineedcoffee Word 5 12-06-2011 12:52 PM
Macro to set page borders Page number Macro kimsi Word 3 11-15-2011 11:54 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:56 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft