Populate footer

G

Gob

Hi
I have a blank document template.
I have written some code to dynamically load in images into the header and
footer depending on the selection in a user form. My problem is that I need
different images on the first page to the rest of the pages. As my blank
document only contains 1 page initially, how can I insert the image into the
subsequent footers when new pages are added?

Many thanks
Gordon O'Brien
 
P

Peter Hewett

Hi Gob

Ok, you need to take a slightly different approach. Add all the images at the
same time even though the initial document only has one page! Run this code
on a new empty document and then add text until you have 2 or more pages,
you'll see that the headers are correct.

Public Sub AddHeaders()
Dim rngHeader As Word.Range

With ActiveDocument.Sections(1)

.PageSetup.DifferentFirstPageHeaderFooter = True

' Add something to the First Page Header
Set rngHeader = .Headers(wdHeaderFooterFirstPage).Range
rngHeader.Text = "First Page Header story"

' Add something to the Primary Page Header -
' the page(s) that follow the first page
Set rngHeader = .Headers(wdHeaderFooterPrimary).Range
rngHeader.Text = "Primary Header story"
End With
End Sub

Just use the range objects to add your pictures/text etc.

HTH + Cheers - Peter
 
G

Gob

Hi Peter
the functionality of the code you supplied was spot on!

I still have a problem that I can't seem to resolve...
When I load in an image into the first page header, It resizes / positions
it to fit within the margins. I need it to display 100% size and positioned
with a zero top, left and right margin.

I have used my old friend the 'Macro recorder' to try and script the actions
required for the task, however when I add the actions to my script, I get
'Method or data member not found' on some of the methods.
My latest script is included below. The commented lines are the methods that
are not recognised.

Any further comments would be appreciated.
Thanks
Gordon

Dim rngHeader As Word.Range
With ActiveDocument.Sections(1)
.PageSetup.DifferentFirstPageHeaderFooter = True

' Add image to the First Page Header
Set rngHeader = .Headers(wdHeaderFooterFirstPage).Range
With rngHeader.InlineShapes.AddPicture(IMAGE_PATH & strColour &
".jpg", LinkToFile:=False, SaveWithDocument:=True)
.Fill.Visible = msoFalse
.Fill.Solid
.Fill.Transparency = 0#
.Line.Weight = 0.75
.Line.DashStyle = msoLineSolid
.Line.Style = msoLineSingle
.Line.Transparency = 0#
.Line.Visible = msoFalse
.LockAspectRatio = msoTrue
'.Rotation = 0#
.PictureFormat.Brightness = 0.5
.PictureFormat.Contrast = 0.5
.PictureFormat.ColorType = msoPictureAutomatic
.PictureFormat.CropLeft = 0#
.PictureFormat.CropRight = 0#
.PictureFormat.CropTop = 0#
.PictureFormat.CropBottom = 0#
'.Left = 2.55
'.Top = 0#
'.RelativeHorizontalPosition =
wdRelativeHorizontalPositionColumn
'.RelativeVerticalPosition = wdRelativeVerticalPositionPage
'.Left = CentimetersToPoints(-3.26)
'.Top = CentimetersToPoints(0)
'.LockAnchor = False
'.WrapFormat.AllowOverlap = True
'.WrapFormat.Side = wdWrapBoth
'.WrapFormat.DistanceTop = CentimetersToPoints(0)
'.WrapFormat.DistanceBottom = CentimetersToPoints(0)
'.WrapFormat.DistanceLeft = CentimetersToPoints(0.32)
'.WrapFormat.DistanceRight = CentimetersToPoints(0.32)
'.WrapFormat.Type = 3
'.ZOrder 5
'.ScaleWidth 0.75, msoFalse, msoScaleFromBottomRight
'.ScaleHeight 0.75, msoFalse, msoScaleFromTopLeft
'.IncrementLeft -150.5
'.IncrementTop 2.5
'.ScaleWidth 1.34, msoFalse, msoScaleFromTopLeft
'.ScaleHeight 1.34, msoFalse, msoScaleFromTopLeft
'.IncrementTop -9#
'.ScaleWidth 1.01, msoFalse, msoScaleFromTopLeft
'.ScaleHeight 1.01, msoFalse, msoScaleFromTopLeft
End With
End with
 
P

Peter Hewett

Hi Gob

Sorry, this thread has scrolled well up in ny News Reader and I've only just
noticed there's been a new message added to it.

The problems is: the code you are using is for a Shape object. However, the
code is adding an InlineShape object. That's why the Methods and Properties
don't work!!!

This code works but I don't know if it does what you want. By that I mean: I
don't have the image you're using and I don't have the same document with
it's layout settings. You should be able to modify this to do what you want,
it's a fairly crude hack of what you provided. There's redundancy at the end
with multiple "ScaleWidth" and "ScaleHeight" statements:


Public Sub InsertPictureInHeader()
Dim rngHeader As Word.Range
Dim ilsNew As Word.InlineShape
Dim shpNew As Word.Shape

With ActiveDocument.Sections(1)
.PageSetup.DifferentFirstPageHeaderFooter = True

' Add image to the First Page Header
Set rngHeader = .Headers(wdHeaderFooterFirstPage).Range
Set ilsNew = rngHeader.InlineShapes.AddPicture("c:\bath.jpg", _
False, True)
With ilsNew
.Fill.Visible = msoFalse
.Fill.Solid
.Fill.Transparency = 0#
.Line.Weight = 0.75
.Line.DashStyle = msoLineSolid
.Line.Style = msoLineSingle
.Line.Transparency = 0#
.Line.Visible = msoFalse
.LockAspectRatio = msoTrue
'.Rotation = 0#
.PictureFormat.Brightness = 0.5
.PictureFormat.Contrast = 0.5
.PictureFormat.ColorType = msoPictureAutomatic
.PictureFormat.CropLeft = 0#
.PictureFormat.CropRight = 0#
.PictureFormat.CropTop = 0#
.PictureFormat.CropBottom = 0#
End With

' Convert InlineShape object to a Shape object
Set shpNew = ilsNew.ConvertToShape
With shpNew
.Left = 2.55
.Top = 0#
.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
.RelativeVerticalPosition = wdRelativeVerticalPositionPage
.Left = CentimetersToPoints(-3.26)
.Top = CentimetersToPoints(0)
.LockAnchor = False
.WrapFormat.AllowOverlap = True
.WrapFormat.Side = wdWrapBoth
.WrapFormat.DistanceTop = CentimetersToPoints(0)
.WrapFormat.DistanceBottom = CentimetersToPoints(0)
.WrapFormat.DistanceLeft = CentimetersToPoints(0.32)
.WrapFormat.DistanceRight = CentimetersToPoints(0.32)
.WrapFormat.Type = 3
.ZOrder 5
.ScaleWidth 0.75, msoFalse, msoScaleFromBottomRight
.ScaleHeight 0.75, msoFalse, msoScaleFromTopLeft
.IncrementLeft -150.5
.IncrementTop 2.5
.ScaleWidth 1.34, msoFalse, msoScaleFromTopLeft
.ScaleHeight 1.34, msoFalse, msoScaleFromTopLeft
.IncrementTop -9#
.ScaleWidth 1.01, msoFalse, msoScaleFromTopLeft
.ScaleHeight 1.01, msoFalse, msoScaleFromTopLeft
End With
End With
End Sub

HTH + Cheers - Peter
 
G

Gob

I thought I was on to a winner there for a minute!
but then...

Run-time error '-2147467259 (80004005)':
Method 'ConvertToShape' of object 'InlineShape' failed

on line:
Set shpNew = ilsNew.ConvertToShape

Got me beat again!
 
P

Peter Hewett

Hi Gob

I've just tried the code (again) in Word 2k + Word XP using a variety of
different Section 1 header setups. All works perfectly ok for me.

What version of Word are you using and have you modified the code, if so post
your modifed code. Otherwise at the moment I have no idea what's going on!

Cheers - Peter
 
G

Gob

Hi Peter


I am using Word 2002 SP1

I have tried the code below with and without the comments.

Dim rngHeader As Word.Range
Dim ilsNew As Word.InlineShape
Dim shpNew As Word.Shape

With ActiveDocument.Sections(1)
.PageSetup.DifferentFirstPageHeaderFooter = True

' Add image to the First Page Header
Set rngHeader = .Headers(wdHeaderFooterFirstPage).Range
Set ilsNew = rngHeader.InlineShapes.AddPicture(IMAGE_PATH &
strColour & ".jpg", False, True)
' With ilsNew
' .Fill.Visible = msoFalse
' .Fill.Solid
' .Fill.Transparency = 0#
' .Line.Weight = 0.75
' .Line.DashStyle = msoLineSolid
' .Line.Style = msoLineSingle
' .Line.Transparency = 0#
' .Line.Visible = msoFalse
' .LockAspectRatio = msoTrue
' '.Rotation = 0#
' .PictureFormat.Brightness = 0.5
' .PictureFormat.Contrast = 0.5
' .PictureFormat.ColorType = msoPictureAutomatic
' .PictureFormat.CropLeft = 0#
' .PictureFormat.CropRight = 0#
' .PictureFormat.CropTop = 0#
' .PictureFormat.CropBottom = 0#
'End With

' Convert InlineShape object to a Shape object
Set shpNew = ilsNew.ConvertToShape

With shpNew
.Left = 0#
.Top = 0#
.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
.RelativeVerticalPosition = wdRelativeVerticalPositionPage
.Left = CentimetersToPoints(-3.26)
.Top = CentimetersToPoints(0)
.LockAnchor = False
.WrapFormat.AllowOverlap = True
.WrapFormat.Side = wdWrapBoth
.WrapFormat.DistanceTop = CentimetersToPoints(0)
.WrapFormat.DistanceBottom = CentimetersToPoints(0)
.WrapFormat.DistanceLeft = CentimetersToPoints(0.32)
.WrapFormat.DistanceRight = CentimetersToPoints(0.32)
.WrapFormat.Type = 3
.ZOrder 5
.ScaleWidth 0.75, msoFalse, msoScaleFromBottomRight
.ScaleHeight 0.75, msoFalse, msoScaleFromTopLeft
.IncrementLeft -150.5
.IncrementTop 2.5
.ScaleWidth 1.34, msoFalse, msoScaleFromTopLeft
.ScaleHeight 1.34, msoFalse, msoScaleFromTopLeft
.IncrementTop -9#
.ScaleWidth 1.01, msoFalse, msoScaleFromTopLeft
.ScaleHeight 1.01, msoFalse, msoScaleFromTopLeft
End With
End With


Thanks
Gordon
 
P

Peter Hewett

Hi Gordon

I can't see why this should not work, I've been scratching my head about
why an inline shape object can't be converted to a shape.

if you can email me your template and the one or two of the images you're
inserting I'll take a quick look.

Email me at nospaminnerword at xtra dot co dot nz, just remove the nospam.

Cheers - Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top