J
JSM
Hi,
I have inheritied an old macro which adds a document footer at the bottom of
each page (see below). The problem is, just recently we have started getting
two or three footers in each section. I've tried to get around this by
checking if the selected section is either the first section or is linked to
previous. If it is not linked to previous or is the first section then the
footer should be inserted.
The problem is this:
1. Selection.Sections(1).Index always returns "1" when the selection is in a
footer.
2. LinkToPrevious returns "True" for the first section, even though it is
not (because it can't be). The weird thing about this one is that if you
display a message box showing the value of
Selection.HeaderFooter.LinkToPrevious it returns "True", however if you
place a breakpoint at this line and check the value in the Immediate window
it returns "False". If you test this value in an IF statement within the
code it returns "True".
Can somebody please tell me what's going on !!!
The code is below. I acknowledge that the coding is a bit sloppy but like I
said - I have inherited this code and haven't had time to rewrite it:
Dim myRange As Range
Dim intSections As Integer
'Inserts docref 8 points left aligned adjust alignment etc as required the
first place it
'has been inserted and the next section - where it is inserted from
thereafter.
On Error GoTo errorhandler 'close footer - return to main doc
'ensure doc is in page view
ActiveWindow.ActivePane.View.Type = wdPageView
'ensure top first page
Selection.HomeKey Unit:=wdStory
'edit first page footer
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
'search for non breaking space in first page footer, if not exist insert
docref
Selection.HomeKey Unit:=wdStory
With Selection.Find
.ClearFormatting
.Text = "^s"
.Replacement.Text = ""
.Forward = True
End With
If Selection.Find.Execute = False Then
Selection.WholeStory 'text in footer insert extra return b4
inserting ref
If Selection <> vbCr Then
Selection.EndKey Unit:=wdStory
Selection.TypeParagraph
Else 'no text in footer - dont insert extra paramark
Selection.EndKey Unit:=wdStory
End If
Selection.ParagraphFormat.TabStops.ClearAll
Selection.Font.Size = 9
Dim myrng As Range
Set myrng = Selection.Range
myrng.End = Selection.Range.End
myrng.Start = Selection.Range.Start
If ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
Then
ThisDocument.AttachedTemplate.AutoTextEntries("WTFoot_NoPg").Insert myrng,
True
Else
ThisDocument.AttachedTemplate.AutoTextEntries("WTFoot").Insert
myrng, True
Selection.ParagraphFormat.TabStops.Add
(Selection.PageSetup.PageWidth - Selection.PageSetup.RightMargin -
Selection.PageSetup.LeftMargin), wdAlignTabRight
End If
Selection.Collapse wdCollapseStart
End If
'search through remaining sections
On Error Resume Next
Do
'goto next header and check for docref, ie next footer same as previous
ActiveWindow.ActivePane.View.NextHeaderFooter
If Err = 4605 Then
Exit Do
End If
If Selection.HeaderFooter.LinkToPrevious = False Then
'search for non breaking space, if not exist insert docref
With Selection.Find
.ClearFormatting
.Text = "^s"
.Replacement.Text = ""
.Forward = True
End With
If Selection.Find.Execute = False Then
Selection.WholeStory 'text in footer insert extra return b4
inserting ref
If Selection <> vbCr Then
Selection.EndKey Unit:=wdStory
Selection.TypeParagraph
Else 'no text in footer - dont insert extra paramark
Selection.EndKey Unit:=wdStory
End If
Selection.ParagraphFormat.TabStops.ClearAll
Selection.Font.Size = 9
ThisDocument.AttachedTemplate.AutoTextEntries("WTFoot").Insert
Selection.Range, True
Selection.ParagraphFormat.TabStops.Add
(Selection.PageSetup.PageWidth - Selection.PageSetup.RightMargin -
Selection.PageSetup.LeftMargin), wdAlignTabRight
Selection.Collapse wdCollapseStart
End If
End If
Loop
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Exit Sub
errorhandler:
MsgBox "Error: " & Err.Number & vbNewLine & Err.Description
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
I have inheritied an old macro which adds a document footer at the bottom of
each page (see below). The problem is, just recently we have started getting
two or three footers in each section. I've tried to get around this by
checking if the selected section is either the first section or is linked to
previous. If it is not linked to previous or is the first section then the
footer should be inserted.
The problem is this:
1. Selection.Sections(1).Index always returns "1" when the selection is in a
footer.
2. LinkToPrevious returns "True" for the first section, even though it is
not (because it can't be). The weird thing about this one is that if you
display a message box showing the value of
Selection.HeaderFooter.LinkToPrevious it returns "True", however if you
place a breakpoint at this line and check the value in the Immediate window
it returns "False". If you test this value in an IF statement within the
code it returns "True".
Can somebody please tell me what's going on !!!
The code is below. I acknowledge that the coding is a bit sloppy but like I
said - I have inherited this code and haven't had time to rewrite it:
Dim myRange As Range
Dim intSections As Integer
'Inserts docref 8 points left aligned adjust alignment etc as required the
first place it
'has been inserted and the next section - where it is inserted from
thereafter.
On Error GoTo errorhandler 'close footer - return to main doc
'ensure doc is in page view
ActiveWindow.ActivePane.View.Type = wdPageView
'ensure top first page
Selection.HomeKey Unit:=wdStory
'edit first page footer
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
'search for non breaking space in first page footer, if not exist insert
docref
Selection.HomeKey Unit:=wdStory
With Selection.Find
.ClearFormatting
.Text = "^s"
.Replacement.Text = ""
.Forward = True
End With
If Selection.Find.Execute = False Then
Selection.WholeStory 'text in footer insert extra return b4
inserting ref
If Selection <> vbCr Then
Selection.EndKey Unit:=wdStory
Selection.TypeParagraph
Else 'no text in footer - dont insert extra paramark
Selection.EndKey Unit:=wdStory
End If
Selection.ParagraphFormat.TabStops.ClearAll
Selection.Font.Size = 9
Dim myrng As Range
Set myrng = Selection.Range
myrng.End = Selection.Range.End
myrng.Start = Selection.Range.Start
If ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
Then
ThisDocument.AttachedTemplate.AutoTextEntries("WTFoot_NoPg").Insert myrng,
True
Else
ThisDocument.AttachedTemplate.AutoTextEntries("WTFoot").Insert
myrng, True
Selection.ParagraphFormat.TabStops.Add
(Selection.PageSetup.PageWidth - Selection.PageSetup.RightMargin -
Selection.PageSetup.LeftMargin), wdAlignTabRight
End If
Selection.Collapse wdCollapseStart
End If
'search through remaining sections
On Error Resume Next
Do
'goto next header and check for docref, ie next footer same as previous
ActiveWindow.ActivePane.View.NextHeaderFooter
If Err = 4605 Then
Exit Do
End If
If Selection.HeaderFooter.LinkToPrevious = False Then
'search for non breaking space, if not exist insert docref
With Selection.Find
.ClearFormatting
.Text = "^s"
.Replacement.Text = ""
.Forward = True
End With
If Selection.Find.Execute = False Then
Selection.WholeStory 'text in footer insert extra return b4
inserting ref
If Selection <> vbCr Then
Selection.EndKey Unit:=wdStory
Selection.TypeParagraph
Else 'no text in footer - dont insert extra paramark
Selection.EndKey Unit:=wdStory
End If
Selection.ParagraphFormat.TabStops.ClearAll
Selection.Font.Size = 9
ThisDocument.AttachedTemplate.AutoTextEntries("WTFoot").Insert
Selection.Range, True
Selection.ParagraphFormat.TabStops.Add
(Selection.PageSetup.PageWidth - Selection.PageSetup.RightMargin -
Selection.PageSetup.LeftMargin), wdAlignTabRight
Selection.Collapse wdCollapseStart
End If
End If
Loop
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Exit Sub
errorhandler:
MsgBox "Error: " & Err.Number & vbNewLine & Err.Description
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument