PDA

View Full Version : Strange LinkToPrevious Timing bug



murphy84
11-07-2007, 02:13 AM
I'm attempting to create a page break to separate the last page from the second to last page in the document. I then need to unlink the previous footer from the blank page section because I want to remove the footer information from the footer details on the blank page but keep the existing footer on the last page.

My VBA code is as follows...


Private Sub SetupDuplexEL()
Dim current_page_no As Integer
Dim current_section_no As Integer
'Goto Bookmark and work out page number
ActiveDocument.Bookmarks("BREAK_ELCERT").Select
current_page_no = Selection.Information(wdActiveEndPageNumber)
'Stop
' Application.Visible = True
'Work out if page is odd or even
If current_page_no Mod 2 = 0 Then
' Its Even
' Goto BREAK_ELCERT_2 and insert page breaks
ActiveDocument.Bookmarks("BREAK_ELCERT_2").Select
'Insert Page Breaks
Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.InsertBreak Type:=wdSectionBreakNextPage

'Set view to the el footer
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
'Unlink from previous so old footer doesnt become deleted
Selection.HeaderFooter.LinkToPrevious = False

ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument ' Reset View

'Move up
ActiveDocument.Bookmarks("BREAK_ELCERT_2").Select
Selection.MoveDown Unit:=wdLine, Count:=1

'Goto the header and unlink from previous
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.HeaderFooter.LinkToPrevious = False
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
'Select contents in footer and remove it from the blank page
Selection.WholeStory
Selection.Copy
Selection.Delete Unit:=wdCharacter, Count:=1

' ********* Word Hack *********** Footer details get deleted so put back in!
ActiveWindow.ActivePane.View.NextHeaderFooter
'Goto the footer
Selection.HeaderFooter.LinkToPrevious = False
Selection.WholeStory
Selection.Paste
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument ' Reset view
End If

End Sub

If I run the code as it is, both the last footer and the footer on the blank page are removed, but if I uncomment the stop command and application.visible = true commands then re-run the code line by line it works perfectly.

Has anyone come across this before? I'm assuming it's an issue with the LinkToPrevious command not unlinking correctly when run through without a break. Is this a timing issue?? Do I need to tell the code to wait at a certain point, if so how would I do that?

As you can see I've even tried to reinsert the footers again with no effect. Any help is greatly appreciated - it appears this one is a bit of a puzzling headwrecker (the best and worst kinds!).

Thanks in advance.

murphy84
11-07-2007, 03:01 AM
I appear to have found out a way to sort the problem thanks to Greg Maxey at the following url:-

http://gregmaxey.mvps.org/Add_Section_Break_Unlink_Headers.htm

Using an adapted version of his AddSectionAndKillLinkToPrevious routine I adapted it for my own use. The following code now works:-


Sub KillLinkToPrevious()
Dim i As Long
Dim j As Long
Dim oDoc As Word.Document
Dim myRng As Word.Range
Set oDoc = ActiveDocument
'Get the index number of the added section
i = oDoc.Range(0, Selection.Sections(1).Range.End).Sections.Count
With oDoc.Sections(i)
For j = 1 To 3
.Headers(j).LinkToPrevious = False
.Footers(j).LinkToPrevious = False
Next j
End With
'Note: j provides the constant value to unlink the primary page
'(and the first/even page [if exists]) header/footer in the new section
End Sub

Private Sub SetupDuplexEL()
Dim current_page_no As Integer
Dim current_section_no As Integer
'Goto Bookmark and work out page number
ActiveDocument.Bookmarks("BREAK_ELCERT").Select
current_page_no = Selection.Information(wdActiveEndPageNumber)
'Stop
' Application.Visible = True
'Work out if page is odd or even
If current_page_no Mod 2 = 0 Then
' Its Even
' Goto BREAK_ELCERT_2 and insert page breaks
ActiveDocument.Bookmarks("BREAK_ELCERT_2").Select
'Insert Page Breaks
Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.InsertBreak Type:=wdSectionBreakNextPage

KillLinkToPrevious 'Sort LinkToPrevious problem
'Set view to the el footer
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
'Unlink from previous so old footer doesnt become deleted
Selection.HeaderFooter.LinkToPrevious = False

ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument ' Reset View

'Move up
ActiveDocument.Bookmarks("BREAK_ELCERT_2").Select
Selection.MoveDown Unit:=wdLine, Count:=1

KillLinkToPrevious 'Sort LinkToPrevious problem
'Goto the header and unlink from previous
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.HeaderFooter.LinkToPrevious = False
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
'Select contents in footer and remove it from the blank page
Selection.WholeStory
Selection.Copy
Selection.Delete Unit:=wdCharacter, Count:=1

KillLinkToPrevious 'Sort LinkToPrevious problem
' ********* Word Hack *********** Footer details get deleted so put back in!
ActiveWindow.ActivePane.View.NextHeaderFooter
'Goto the footer
Selection.HeaderFooter.LinkToPrevious = False
Selection.WholeStory
Selection.Paste
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument ' Reset view
End If

End Sub

I've probably overcooked the use of KillLinkToPrevious but it doesn't have a particularly significant effect on the processing time for it for my purposes and in this case "if it ain't broke" comes to mind.

Hope this helps others out there when dealing with the LinkToPrevious issues!

fionabolt
11-07-2007, 04:05 AM
Murphy,

Well done for solving your own problem. Congratulations. Very satisfying isn't it?

One big improvement you could make to your code would be to use ranges instead of selection all the time.

This would also mean that your users would not be subjected to a flickering screen during run time....

So, to take the first part of your code, try this instead:
Private Sub SetupDuplexEL()
Dim current_page_no As Integer
Dim current_section_no As Integer
Dim bkmRange1 As Range
Dim bkmRange2 As Range

'Goto Bookmark and work out page number
Set bkmRange1 = ActiveDocument.Bookmarks("BREAK_ELCERT").Range

current_page_no = bkmRange1.Information(wdActiveEndAdjustedPageNumber)
'Stop
' Application.Visible = True
'Work out if page is odd or even
If current_page_no Mod 2 = 0 Then
' Its Even
' Goto BREAK_ELCERT_2 and insert page breaks
Set bkmRange2 = ActiveDocument.Bookmarks("BREAK_ELCERT_2").Range
'Insert Page Breaks
bkmRange2.InsertBreak Type:=wdSectionBreakNextPage
bkmRange2.InsertBreak Type:=wdSectionBreakNextPage


:thumb