PDA

View Full Version : Replace all images into new ones



maciej_wroc
08-03-2012, 03:03 PM
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. It is on bottom of a page, but it is not a footer yet.

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.

Thanks in advance for your help.

macropod
08-04-2012, 09:41 PM
Cross-posted at:
http://www.excelforum.com/word-formatting-and-general/850369-replace-all-images-into-new-ones.html
http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/43ed81d5-1187-4e4d-bb8c-ef84fcf98ec4
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

macropod
08-05-2012, 04:45 PM
Found another cross-post: http://www.office-forums.com/replace-all-images-into-new-ones-t2342302.html

Note: problem solved at http://social.msdn.microsoft.com/Forums/en/worddev/thread/43ed81d5-1187-4e4d-bb8c-ef84fcf98ec4?prof=required