Log in

View Full Version : Hide image in section header



BertusX
03-04-2013, 05:29 AM
Hi everyone, I'm new to the forum, searched the archives but no answer found to this case:

Upon printing a document, I need to temporarily hide any shape in the header of a specific section.

I have a document, containing 4 sections.
section 1 2 and 3 are DIN A4 papersize, portrait oriented
section 4 is landscape oriented

section 1-3 will be printed on company paper, so the logo image in the headers 1-3 must be hidden, while in section 4 it must be shown

So, on 3 sections, the company logo needs to be hidden while printing, and shown again afterwards. Let's worry about the "shown again" part later.

Here's my code:
Sub PrintPreviewAndPrint()

Dim sc As Section
Dim booShow As Boolean
Dim sp As Shape

For Each sc In ActiveDocument.Sections

With sc.PageSetup
booShow = Not (.Orientation = wdOrientPortrait And .PaperSize = wdPaperA4)
End With

For Each sp In sc.Headers(wdHeaderFooterPrimary).Shapes
sp.Visible = booShow
Next sp

Next sc

End Sub

So the code loops through sections. Come section 1, images in all section headers are hidden. Same goes for sections 2 and 3. Come section 4 images in all sections are shown again. I'd expect that in the end, images in first 3 sections are hidden, and in section 4 is displayed.

Could anybody please tell me what I'm doing wrong here?

Second: how do I get to display all images again after the document was printed?

Please help me out.
Many thanks
Bart

gmaxey
03-04-2013, 05:45 AM
Something like:
Sub ScratchMacroHide()
'A basic Word macro coded by Greg Maxey
Dim i As Long, j As Long
Dim oSec As Section
For i = 1 To 4
Set oSec = ActiveDocument.Sections(i)
For j = 1 To 3
oSec.Headers(j).LinkToPrevious = False
Next j
Next i
For i = 1 To 3
Set oSec = ActiveDocument.Sections(i)
For j = 1 To 3
On Error Resume Next
oSec.Headers(j).Range.ShapeRange.Visible = False
Next j
Next i
End Sub
Sub ScratchMacroShow()
'A basic Word macro coded by Greg Maxey
Dim i As Long, j As Long
Dim oSec As Section
For i = 1 To 3
Set oSec = ActiveDocument.Sections(i)
For j = 1 To 3
On Error Resume Next
oSec.Headers(j).Range.ShapeRange.Visible = True
Next j
Next i
End Sub

BertusX
03-05-2013, 12:00 AM
Thanks Greg! This works great.
I would like to know what I did wrong. If you have the time,could you explain why my code didn't work? I understand the LinkToPrevious part. Headers weren't linked anyway.

I wonder about the essential difference between my
Section.Headers(wdHeaderFooterPrimary).Shapes(i).Visible = False
and your
oSec.Headers(j).Range.ShapeRange.Visible = False
But only if you have the time :)

Bart

gmaxey
03-05-2013, 04:20 AM
Bart,

I've pulled out mounds of hair dealing with shapes over the years and I can't offer a clear explanation ;-). Consider your original code modified as shown below. Run it and observe the count values. It appears that the Shape count in section 1 includes all the shapes in all subsequent sections so on.


Sub PrintPreviewAndPrint()
Dim sc As Section
Dim booShow As Boolean
Dim sp As Shape
For Each sc In ActiveDocument.Sections

With sc.PageSetup
booShow = Not (.Orientation = wdOrientPortrait)
End With
Debug.Print sc.Headers(wdHeaderFooterPrimary).Shapes.Count
Debug.Print sc.Headers(wdHeaderFooterPrimary).Range.ShapeRange.Count
For Each sp In sc.Headers(wdHeaderFooterPrimary).Shapes
sp.Visible = booShow
Next sp

Next sc

End Sub