Log in

View Full Version : Replacing & Bookmarking an Image



kbrooks
07-27-2015, 09:42 AM
Hi,

I have the following code:

Sub ExtractChart(Location As String, BookmarkToUpdate As String, wb As WorkBook)

If ActiveDocument.Bookmarks.Exists(BookmarkToUpdate) = True Then

Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range

BMRange.InlineShapes(1).Delete

wb.Sheets("Extraction_Charts").Range(Location).Copy
BMRange.PasteSpecial Link:=False, DataType:=wdPasteEnhancedMetafile, _
Placement:=wdInLine, DisplayAsIcon:=False
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange

End If

End Sub

Its function is to reference a bookmark which contains only an image and replace the image, with the bookmark remaining intact. The problem I'm having is that the bookmark, when being re-set, is empty and right after the image, and does not encapsulate the image.

I think the error probably lies in the InlineShapes function, or in the Bookmarks.Add part.

Anyone have any thoughts?

gmayor
07-27-2015, 09:04 PM
Add the line
BMRange.Start = BMRange.Start - 1
immediately before re-setting the bookmark.

kbrooks
07-28-2015, 01:26 PM
Add the line
BMRange.Start = BMRange.Start - 1
immediately before re-setting the bookmark.

Fantastic! Thanks for the assistance. I figured it would be something simple, but I'm new to this.