PDA

View Full Version : Inserting Image to each page in word 2007



massoudshake
06-21-2012, 04:08 PM
Dear All,
I wrote a small VBA routine to insert an image to each page of a word document, and it worked fine on Word 2010. But when I tested that with word 2007, it advances page by page, but inserts the image to the first page, instead of inserting on the next page.
Here is the code:
For i = CurPage To CurPage + NumOfPages - 1
Set WORD_Image = Word.ActiveDocument.Shapes.AddPicture(ImageName, , True, 0, 0, 10, 10)
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Count:=1
Next

Would you please show me how to make it work on 2007?
thanks
massoud

macropod
06-22-2012, 12:33 AM
Hi massoud,

If you want the image to appear on every page, why not put just one image in the page header/footer?

massoudshake
06-22-2012, 11:51 AM
Hi,
Thanks for the response. I want to insert an image on each page in different places, not at the same position, so I cannot do this in header.
I removed the rest of the code to be brief.

macropod
06-22-2012, 03:23 PM
You could use something based on:
Sub Demo()
Dim Rng As Range, i As Long, Shp As Shape, ImageName As String
ImageName = "C:\Users\" & Environ("UserName") & "\Documents\Pictures\Alice in Wonderland.gif"
With ActiveDocument
Set Rng = .Range(0, 0)
For i = 1 To .ComputeStatistics(wdStatisticPages)
Set Rng = Rng.GoTo(What:=wdGoToPage, Name:=i)
Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\page")
Rng.Collapse wdCollapseStart
Set Shp = .InlineShapes.AddPicture(FileName:=ImageName, SaveWithDocument:=True, Range:=Rng).ConvertToShape
With Shp
.Left = 0
.Top = 0
.Width = 10
.Height = 10
End With
Next
End With
End Sub
Simply adjust the dimensions and any other relevant paramenters via the With .Shp ... End With block.

massoudshake
06-23-2012, 12:38 AM
Hi Paul,
Thank you very much. The code works fine for me.
That was a definite help.
Thanks again
Massoud