Consulting

Results 1 to 5 of 5

Thread: Inserting Image to each page in word 2007

  1. #1

    Unhappy Inserting Image to each page in word 2007

    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

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi massoud,

    If you want the image to appear on every page, why not put just one image in the page header/footer?
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    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.

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    You could use something based on:
    [VBA]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[/VBA]
    Simply adjust the dimensions and any other relevant paramenters via the With .Shp ... End With block.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5

    Smile

    Hi Paul,
    Thank you very much. The code works fine for me.
    That was a definite help.
    Thanks again
    Massoud

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •