PDA

View Full Version : Header/footer script problem



Doneltac
07-20-2011, 11:28 PM
Hello everybody,

My first post on this forum, so a little introduction:

My name is Taco, I'm from the Netherlands. Thirtynine years old and my occupation is ICT manager of the Kremer Group (kremer.nl). I'm responsible for most of the ICT related issues, internal (roadmaps and purchasing) and external (sales and consultancy).

Regarding my question: I created a header/footer script with a nice form from which employes of our different departments can choose their own header/footer with corresponding logo's.

This works well on my own pc, and on some of the other pc's. But not on all of them. In a new document this script will put the header logo into the header, but it puts also the footer in the header. This happens only in a new document. Excisting document with more then two pages are no problem.

I checked the Word versions (we are talking about Word 2010) and upgraded them to the latest version (14.6032.1000 32bit), but the problem still exist.

This is the part of the script which is responsible of placing the header and footer:

Private Sub CommandButton1_Click()
With ActiveDocument.Sections(1)

.PageSetup.DifferentFirstPageHeaderFooter = True

With ActiveDocument
.Sections(1).Headers(wdHeaderFooterFirstPage).Shapes.AddPicture ("header image.jpg")
With .Sections(1).Headers(wdHeaderFooterFirstPage).Shapes(1)
.Width = 530
.Left = -50
.Top = -25
End With
.Sections(1).Footers(wdHeaderFooterFirstPage).Shapes.AddPicture ("footer image.jpg")
With .Sections(1).Footers(wdHeaderFooterFirstPage).Shapes(2)
.Width = 530
.Left = -50
.Top = 15
End With
.Sections(1).Headers(wdHeaderFooterPrimary).Shapes.AddPicture ("header image other pages.jpg")
With .Sections(1).Headers(wdHeaderFooterPrimary).Shapes(3)
.Width = 170
.Left = -50
.Top = -25
End With
End With


End With
End Sub


Can somebody tell me what I did wrong, or how I can solve this problem?

Best regards,

Taco

Frosty
07-27-2011, 06:54 PM
In cleaning up a recorded macro, you probably lost some of some implied settings. I think the setting you're missing (at least in this approach) is the .RelativeVerticalPosition property. I don't know what your .Top is based off of, but I'm assuming it's some kind of "relative to my cursor" kind of thing, which gets tricky when you're inserting shapes with code.

I've revamped your code to be a little better, as well as added the property I think will help you. See if this helps.

However, I'm not sure how you can put "Header image.jpg" as the full path for the picture you want to add. Typically that has to be a full filepath, not just the name of the file. But I've left that as is, in the assumption that you posted code that works sometimes.

As you'll see in the revamped code, if you are creating something-- use a variable and have that as the pointer. You can't create a shape and then assume that will be Shape 1 (or Shape 2 or Shape 3)... what if there are other shapes? Thus the use of the:
Set oShape = blahblah.Shapes.AddPicture("C:\picturename.jpg") thing.

Also... I assume you have a way of deleting these shapes? Because in some cases, when you start messing around with placement-- the shapes will be there, but they will be hidden from view (i.e., if you set .Top = -1600 or something). But once you start with relative margin settings, -25 is enough to make a shape disappear.

So you may very well be doing exactly what you want to do with your original code-- except that you have left over shapes, so you are modifying Shape 3 when you really just inserted Shape 14.

Private Sub CommandButton1_Click()
Dim oSec As Section
Dim oShape As Shape
Dim sPicPath As String

'we're going to work with the first section
Set oSec = ActiveDocument.Sections(1)

'now work with our section
With oSec

'make sure we have different first page
.PageSetup.DifferentFirstPageHeaderFooter = True

'add our first shape to the first page header
'sPicPath = "C:\Test.png" 'I used this for testing purposes to make sure it worked
sPicPath = "header image.jpg"
Set oShape = .Headers(wdHeaderFooterFirstPage).Shapes.AddPicture(sPicPath)
'and modify it
With oShape
.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
.Width = 530
.Left = -50
.Top = -25
End With

'add our second shape to the first page footer
sPicPath = "footer image.jpg"
Set oShape = .Footers(wdHeaderFooterFirstPage).Shapes.AddPicture(sPicPath)
With oShape
.RelativeVerticalPosition = wdRelativeVerticalPositionBottomMarginArea
.Width = 530
.Left = -50
.Top = 15
End With

'add our third shape to the primary header
sPicPath = "header image other pages.jpg"
Set oShape = .Headers(wdHeaderFooterPrimary).Shapes.AddPicture(sPicPath)
With oShape
.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
.Width = 170
.Left = -50
.Top = -25
End With

End With
End Sub

Doneltac
07-28-2011, 12:44 AM
Dear Frosty,

Thank you very much for your help. Indeed, I put the full relative path in the code, but I removed it before posting it here to make the code more clear (the path is very long).

To erease the header/footer I use this script:

Private Sub CommandButton7_Click()
Dim oSct As Section
Dim oHdr As HeaderFooter
Dim oDcm As Document
Dim oShp As shape
Set oDcm = ActiveDocument
For Each oSct In oDcm.Sections
For Each oHdr In oSct.Headers
For Each oShp In oHdr.Shapes
oShp.Delete
Next
Next
Next
End Sub

And that seems to work very well.

Still I have a question, perhapse you can help me with this also: If I use the script the placement of the header is relative to the margins. This means that some times the header is placed much to low. Is it possible to place the header relative to the top of the A4 instead of the margins?

Greetings from Holland,

Taco

Frosty
07-28-2011, 04:40 AM
Greetings from the states!

Your delete script won't work entirely, because you're only searching in the header. You need to add a For Each...loop for the footer too.

Yes, you can use .top based on the page. You'll run into issues on documents which are landscape instead of portrait. I'd play with the various options for the .relativeverticalposition and test the variety of scenarios.