none
MS WORD: Replace all images into new ones (VBA) RRS feed

  • Question

  • Hello, 

    I am quite fresh to Word VBA object model, therefore my code examples may be far away of proper solution. I will appreciate any help, hint which can help.
    Here is my problem:

    On my Word document I have many pages. On each page I have picture inserted.
    I was able to remove old picture and add new one in the same place where the old was. Here is my routine:

    Set oWrd = wrdApp.Documents.Open(sPath)
      For i = oWrd.Shapes.Count To 1 Step -1
         
        With oWrd.Shapes(i)
           .RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
           .RelativeVerticalPosition = wdRelativeVerticalPositionPage
            dTop = .Top
            dLeft = .Left
            dHeight = .Height
            dWidth = .Width
            .Delete
         End With
    
         Set sh = oWrd.Shapes.AddPicture(sPathPicture, , True, dLeft - oWrd.PageSetup.LeftMargin, dTop - oWrd.PageSetup.TopMargin, dWidth, dHeight)
      
      Next i
    oWrd.Close True


    I've noticed that new pictures are added always on the first page. I believe that 'top' is being measured from the top of the 'current' page. 

    I've googled a little bit and found this piece of code:

    Set sh = oWrd.Shapes.AddPicture(sPathPicture, , True, dLeft - oWrd.PageSetup.LeftMargin, dTop - oWrd.PageSetup.TopMargin, dWidth, dHeight, oWrd.Range.GoTo(1, 2, PageNumber))

    but I have no idea how to check the 'pagenumber' of respective shape.

    To sum up:
    I would like to delete all pictures from all pages (above routines do the job) and add new picture at the same coordinates on the same pages. 

    Thanks in advance for your help


    Saturday, August 4, 2012 8:05 AM

Answers

All replies

  • I shall do the experiment for you in the straight way.But would you look from a diff angle.

    I suppose you are filling word doc with some pic.Can you use text box instead of pic directly.

    Then fill it using the path of Picture file ? That will reduce the coding

    Const sPathPicture = "New Picture.bmp" Dim owrd As Document Dim I As Long Set owrd = ActiveDocument For I = owrd.Shapes.Count To 1 Step -1 owrd.Shapes(I).Fill.UserPicture sPathPicture Next I

     

    ---------------------------------------------------------------------------------------------

    Please do not forget to click “Vote as Helpful” if any post helps you and Mark as Answer if it solves the issue.


    Asadulla Javed

    Saturday, August 4, 2012 12:10 PM
    Answerer
  • Hello, 

    it might be the idea. However I'm not sure if with that routine our problem with moving the next page remain. I'll test of course. 

    Anyway - could you please guide me how can I approach the problem using 

    oWrd.Shapes.AddPicture

    ?

    Saturday, August 4, 2012 12:31 PM
  • No.Because we are not changing the shapes height/top ect.Only changing the fill. Check and see.This will keep your code minimum.

    Regarding AddPicture I will do the research.Or you may post in Word Developr forum also.


    Asadulla Javed, Kolkata -----------------------

    Please do not forget to click “Vote as Helpful” if any post helps you and "Mark as Answer" if it solves the issue.


    Saturday, August 4, 2012 12:34 PM
    Answerer
  • Hello, 

    I have posted on Word Developer forum and I get an answer!

    Please find it here: direct link

    Sunday, August 5, 2012 6:57 PM