Consulting

Results 1 to 8 of 8

Thread: HeaderFooter.LinkToPrevious Issue

  1. #1
    VBAX Contributor
    Joined
    Aug 2012
    Posts
    120
    Location

    HeaderFooter.LinkToPrevious Issue

    I've seen a couple of old threads on this but I'm not sure I'm fully understanding especially since my previous macro worked fine until I made some changes and now I'm getting some odd results when using HeaderFooter.LinkToPrevious.

    I'm creating a book with this macro. There are something like 80 individual Word documents that can be used in the book as chosen by the User using a checkbox list control. So the macro goes through the list and determines if the chapter is selected by the user and, if so, it goes to the end of the document, creates a section break (Next Page) and the does the following:

    ActiveDocument.Sections.Last.Headers(wdHeaderFooterFirstPage).LinkToPrevious = False
    ActiveDocument.Sections.Last.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
    ActiveDocument.Sections.Last.Footers(wdHeaderFooterFirstPage).LinkToPrevious = False
    ActiveDocument.Sections.Last.Footers(wdHeaderFooterPrimary).LinkToPrevious = False
    Then, the new chapter is inserted like so:
    Selection.InsertFile FileName:=FileSourceName, Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False
    At this point, the new chapter's first page header is the same header as the previous chapter. I change the new chapter's header like so:
    With ActiveDocument.Sections.Last.Headers(wdHeaderFooterFirstPage).Range
    Set oRange = ActiveDocument.Sections.Last.Headers(wdHeaderFooterFirstPage).Range.Paragraphs.Last.Range
    oRange.Collapse wdCollapseStart 'Isolate the range from the anchor
    oRange.InsertAfter ChptrNm
    oRange.Expand unit:=wdParagraph
    'Formatting takes place here
    End With
    If I do the same book (identical chapter selections) it will fail exactly the same way. Sometimes the linktoprevious = false method works, sometimes it doesn't. Interestingly, the footer linktoprevious = false seems to work fine.

    From the other threads it sounded like this can be a tricky thing as to the timing of turning off the link and referencing the Section/Header properly.

    Anyone have an idea what could be going on? Thanks.

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Contributor
    Joined
    Aug 2012
    Posts
    120
    Location
    Thank you. However, I'm still having a fairly baffling problem. When I step through the macro, it works fine. When I run it full speed, it fails. And it's turning prior sections' LinkToPrevious to True! Have you ever heard of such behavior?

  4. #4
    VBAX Contributor
    Joined
    Aug 2012
    Posts
    120
    Location
    OK, something weird is happening. When I close Word and reopen it to run the macro it works flawlessly. It's when I run the macro again without closing Word is where the strange behavior occurs. So, something - a setting or variable must be "saved" in between that is changing the behavior on subsequent runs of the macro.

  5. #5
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    You haven't posted enough code for anyone to know for sure what the cause is. In any event, the code in the link I posted does not suffer the defect you mention.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  6. #6
    VBAX Contributor
    Joined
    Aug 2012
    Posts
    120
    Location
    Yeah, I know. It's a large macro with the code spread over several UserForms and Modules. I wouldn't know how to begin.

    I'm emptying all of my arrays at the end now but that didn't do it.

    I think the problem may lie with some Collections that I started using a while back. Maybe those get saved in memory between runs, I don't know. At least it works fine after shutting down Word completely and then restarting. Kind of annoying but it's good for now.

    Do you know anything about the initiative Microsoft has undertaken to incorporate JavaScript for use with the on-line version of Office as a replacement for VBA? Thanks.

  7. #7
    VBAX Contributor
    Joined
    Aug 2012
    Posts
    120
    Location
    OK, I'm back as the problem persists. There are a couple of problems but I will focus on the first error instance alone for simplicity sake (Table of Contents and the chapter that follows).

    As I say, this macro creates a book, each Section being a chapter. The Cover Page is created without issue. The Table of Contents is always next and is inserted as described above (it's essentially a blank page). The header and footer are created without issue. A Section Break (Next Page) is inserted and that creates the same header/footer (Table of Contents) in the new section (I'll call this Section, the 3rd, "Site Data"). The 2nd and 3rd Sections are pretty much identical at this point.

    The next chapter is almost always the same. This chapter is not inserted as a blank page but is "built" on the page created by the Section Break. It starts out like so:
    Documents(FullDoc).ActivateActiveDocument.Paragraphs.Last.Range.Select
    Selection.End = Selection.End - 1
    'Configure First Page Header/Footer while on the first page
    Selection.InsertBreak (2)
    Selection.Bookmarks.Add "InsertionPoint"
    ActiveDocument.Sections.Last.PageSetup.DifferentFirstPageHeaderFooter = True
    Call Copy_And_Paste_To_Compilation.Turn_Off_Link_To_Previous 'Turns off LinkToPrevious
    This is where things get weird. Here is the code:

    Sub Turn_Off_Link_To_Previous()
        Dim HdFt As HeaderFooter
            For Each HdFt In ActiveDocument.Sections.Last.Headers
                With HdFt
                    .LinkToPrevious = False
                    .PageNumbers.RestartNumberingAtSection = True
                End With
            Next
            For Each HdFt In ActiveDocument.Sections.Last.Footers
                With HdFt
                    .LinkToPrevious = False
                    .PageNumbers.RestartNumberingAtSection = True
                End With
            Next
    End Sub
    If I step through, the code executes as it should without altering anything but setting LinkToPrevious = False. However, when I run the code full speed, it deletes both the 2nd Section's header and footer (Table of Contents). It also restores the 2nd Section's setting LinkToPrevious = True. It also deletes the 3rd Section's ("Site Data") header and footer which at this point is a repeat of the 2nd Section's header and footer (Table of Contents). Also, the 3rd Section's LinkToPrevious setting is also set to True (after just having been set to false?).

    Every time I step through it, it executes properly. Every time I run it at full speed, it errors as described above.

    I don't get it. I can't imagine what might cause it to behave this way.

  8. #8
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    The problem may well be due to your use of ActiveDocument.


    Since you have no control over which document might be active when the code runs that process, you really need to be more explicit about which document Word is to act on. As you'll see from the link I posted, my code defines the target document as the document to be updated right from the outset, via:
    Set wdDocTgt = ActiveDocument
    Each of the source documents from which the content is to be obtained are then defined via:
    Set wdDocSrc = Documents.Open
    so that Word always knows which is which.


    In any event, aside from choosing which files to combine, the code in the link I posted does essentially all you're trying to do. You could even do away with the 'LayoutTransfer' sub and the call to it if all the documents have the same page layout.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

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