Adding a Picture to each page.

G

George Lima

The following code does go to each page but adds all the pictures to
the first page. I can do this process manually but not through a
macro.

Any help would be appreciated.

Dim sFilename As String

For x = 1 To ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)
sFilename = "C:\Pictures\Page" + chr$(x) + ".WMF"
ActiveDocument.Bookmarks("\Page").Range.Select
Selection.GoTo What:=wdGoToPage, which:=wdGoToAbsolute, Count:=x
ActiveDocument.Shapes.AddPicture(FileName:= _
sFilename , LinkToFile:=False, SaveWithDocument:=True).Select
Selection.ShapeRange.Fill.Visible = msoFalse
Selection.ShapeRange.Fill.Transparency = 0#
Selection.ShapeRange.Line.Weight = 0.75
Selection.ShapeRange.Line.DashStyle = msoLineSolid
Selection.ShapeRange.Line.Style = msoLineSingle
Selection.ShapeRange.Line.Transparency = 0#
Selection.ShapeRange.Line.Visible = msoFalse
Selection.ShapeRange.LockAspectRatio = msoFalse
Selection.ShapeRange.Height = 100#
Selection.ShapeRange.Width = 100#
Selection.ShapeRange.PictureFormat.Brightness = 0.85
Selection.ShapeRange.PictureFormat.Contrast = 0.15
Selection.ShapeRange.PictureFormat.CropLeft = 0#
Selection.ShapeRange.PictureFormat.CropRight = 0#
Selection.ShapeRange.PictureFormat.CropTop = 0#
Selection.ShapeRange.PictureFormat.CropBottom = 0#
Selection.ShapeRange.RelativeHorizontalPosition = _
wdRelativeHorizontalPositionPage
Selection.ShapeRange.RelativeVerticalPosition = _
wdRelativeVerticalPositionPage
Selection.ShapeRange.Left = InchesToPoints(0)
Selection.ShapeRange.Top = InchesToPoints(0)
Selection.ShapeRange.LockAnchor = False
Selection.ShapeRange.WrapFormat.AllowOverlap = True
Selection.ShapeRange.WrapFormat.Side = wdWrapBoth
Selection.ShapeRange.WrapFormat.Type = wdWrapNone
Selection.ShapeRange.ZOrder msoSendBehindText
Next
 
M

Mark Tangard

Hi George,

Add this:

Anchor:=Selection.Range

to the parameter list in the AddPicture line.

(Word doesn't technically require you to specify an anchor, but
if you don't, it guesses. And it's not a very good guesser.)
 
G

George Lima

Thanks this works great.

The only issue is with tables that span pages or if any page starts
with a table. It appears that the picture does not have anything to
anchor to.
 
M

Mark Tangard

Correct, and it's a pain. I've faced that problem only once,
and being in a hurry, did a quick cheat instead of pondering
the answer. I'm in a similar hurry now, but you might try
testing to see if the Selection is in a table at the start
of the loop:

If Selection.Information(wdWithinTable) Then...

and if so, branch to a slightly different AddPicture line
that anchors it to Selection.Cells(1).Range instead.

Of course, both AddPicture lines will come back to bite you
if the document's text changes enough to push the anchor to
a different page, so even if that works, you'll have to be
fairly sure your editing is really done!

(I assume the pictures really do differ graphically, so you
couldn't use the header layer, eh? Or a sneaky textbox/etc
that uses the {PAGE} field? That's what I do in a macro that
prints big ole fancy page numbers in a green box.)
 
G

George Lima

This will actually be done through another application that generates
the graphics, via OLE. The document will not be edited after this
process because the processes will save the document under a different
name, leaving the original.

I will look at the use of a textbox.
 

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