PDA

View Full Version : Chaos in Headers/Footers



Mavila
09-21-2016, 11:47 AM
Is there any reason why a line shape in the header of a page would get deleted when I insert a table in the footer of the same page?

I put the shape into the header like so:


Set ShpLine = ActiveDocument.Sections(ActiveDocument.Sections.Count).Footers(wdHeaderFoot erPrimary).Shapes.AddLine(72, 60, 568, 60)
With ShpLine
.Line.ForeColor.RGB = RGB(0, 0, 0) 'Black line
.Line.Weight = 3
End With


I use the following to insert a table into the footer and PRESTO! the header shape (line) disappears:


Set myTable = ActiveDocument.Tables.Add(Range:=ActiveDocument.Sections(ActiveDocument.Sec tions.Count).Footers(wdHeaderFooterPrimary).Range.Paragraphs(ActiveDocument .Sections(ActiveDocument.Sections.Count).Footers(wdHeaderFooterPrimary).Ran ge.Paragraphs.Count).Range, numrows:=3, numcolumns:=3, AutoFitBehavior:=wdAutoFitFixed)

gmaxey
09-21-2016, 12:23 PM
Because you are killing the shape anchor when you insert the table in the same range.


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim ShpLine
Dim oRng As Range
Dim myTable
Set ShpLine = ActiveDocument.Sections(ActiveDocument.Sections.Count).Footers(wdHeaderFoot erPrimary).Shapes.AddLine(72, 60, 568, 60)
With ShpLine
.Line.ForeColor.RGB = RGB(0, 0, 0) 'Black line
.Line.Weight = 3
End With
Set oRng = ActiveDocument.Sections(ActiveDocument.Sections.Count).Footers(wdHeaderFoot erPrimary).Range.Paragraphs(ActiveDocument.Sections(ActiveDocument.Sections .Count).Footers(wdHeaderFooterPrimary).Range.Paragraphs.Count).Range
oRng.Collapse wdCollapseStart
Set myTable = ActiveDocument.Tables.Add(oRng, 3, 3, wdAutoFitFixed)
lbl_Exit:
Exit Sub
End Sub


When you post code, please include variable declarations.

Mavila
09-21-2016, 02:58 PM
Awesome! Thank you!