Results 1 to 5 of 5
  • Thread Tools
  1. 4 Star Lounger
    Join Date
    Jan 2001
    Location
    Tacoma, Washington, USA
    Posts
    401
    Thanks
    0
    Thanked 1 Time in 1 Post

    Section break trips up letter macro; how do I get around it?

    Below is an old macro we've used for years that adds a second page header to letters with more than one page. It's worked well until we upgraded some users for Word 2003 to Word 2007. Now somehow I have a couple users who occasionally someone get a continuous section break on the line with the bookmark and this causes the macro to crash with a run-time error 5684. The bookmark is suppose to be on the line where the addressee's name is located. On their letters it's the next line below the bookmark. The macro goes to the bookmark and tries to copy the line and freaks because it includes a section break.

    How to work around this? One idea I had was to figure out how to write into the macro to look at the content of the line on which the bookmark is located. If there's no text to move down one line before capturing content and moving forward. No idea how to write that If statement or if it's even possible. The other idea I had was for add something where the macro would look at the letter and if it sees more than one continuous section break (there's one next to the date that needs to stay there) to delete the line bookmark line and move forward. I like the first idea of analyzing the content of the line then for the macro to make a decision over the second.

    Where do I go from here?

    Code:
    Sub ltrSecondPageHeader()
        On Error GoTo -1: On Error GoTo Trap
    
    WordBasic.FileSummaryInfo Update:=1
    Dim dlg As Object: Set dlg = WordBasic.DialogRecord.DocumentStatistics(False)
    WordBasic.CurValues.DocumentStatistics dlg
    
    If dlg.Pages = "1" Then
        WordBasic.MsgBox "This letter does not need a second page header" + Chr(13) + "because it is only one page long.", " ", 48
        GoTo OutOfHere
    End If
    
        Selection.GoTo What:=wdGoToBookmark, Name:="EnvStart"
        Selection.Find.ClearFormatting
        With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.EndKey Unit:=wdLine, Extend:=wdExtend
        Selection.Copy
        Selection.EndKey Unit:=wdStory
        If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
            ActiveWindow.Panes(2).Close
        End If
        If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
            ActivePane.View.Type = wdOutlineView Then
            ActiveWindow.ActivePane.View.Type = wdPrintView
        End If
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
        Selection.PasteAndFormat (wdPasteDefault)
        Selection.InsertDateTime DateTimeFormat:="MMMM d, yyyy", InsertAsField:= _
            False, DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _
            InsertAsFullWidth:=False
        Selection.TypeParagraph
        Selection.TypeText Text:="Page "
        Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage
        Selection.TypeText Text:=vbTab
        Selection.HomeKey Unit:=wdLine
        Selection.EndKey Unit:=wdLine, Extend:=wdExtend
        If Selection.Font.Underline = wdUnderlineNone Then
            Selection.Font.Underline = wdUnderlineSingle
        Else
            Selection.Font.Underline = wdUnderlineNone
        End If
        Selection.EndKey Unit:=wdLine
        Selection.TypeParagraph
        Selection.TypeParagraph
        If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
            ActiveWindow.Panes(2).Close
        End If
        If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
            ActivePane.View.Type = wdOutlineView Then
            ActiveWindow.ActivePane.View.Type = wdPrintView
        End If
        ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
        Selection.HomeKey Unit:=wdStory
    
    GoTo NoMore
    
    OutOfHere:
        WordBasic.MsgBox "Second Page Header Cancelled!", " ", 48
        GoTo Complete
    
    NoMore:
        GoTo Complete
    
    Trap:
    If Err.Number = 102 Then
        WordBasic.MsgBox "An Error Occurred - Header Cancelled!", " ", 48
        Err.Number = 0
        GoTo Complete
    ElseIf Err.Number = 509 Then
        WordBasic.MsgBox "An Error Occurred - Header Cancelled!", " ", 48
        Err.Number = 0
        GoTo Complete
    Else
        Error Err.Number
    End If
    
    Complete:
        WordBasic.MsgBox "Second Page Header Complete.", " ", 48
        End Sub
    Daisy

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. 4 Star Lounger
    Join Date
    Jan 2001
    Location
    Tacoma, Washington, USA
    Posts
    401
    Thanks
    0
    Thanked 1 Time in 1 Post
    Got it. I now have it go to the bookmark and then use ^$ to move up to the next letter which is the addressee. This change will also correct any problems there were when someone added lines between the bookmark and the addressee line.
    Daisy

  4. Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,658
    Thanks
    0
    Thanked 148 Times in 138 Posts
    Hi Daisy,

    Why don't you simply use a template with a 'different first page' layout and have the 2nd page header already present? There'd be no need for a macro.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. 4 Star Lounger
    Join Date
    Jan 2001
    Location
    Tacoma, Washington, USA
    Posts
    401
    Thanks
    0
    Thanked 1 Time in 1 Post
    They want the addressee's name included in the second page header.
    Daisy

  6. Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,377
    Thanks
    3
    Thanked 107 Times in 101 Posts
    Including the addressee's name in the second page header can be done by using a styleref field from the same information in the body of the document. It doesn't require a section break.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •