How to replace a picture inserted at bookmark with another

M

mr

I'm trying to replace a picture I inserted into a document at a bookmark using InsertFile with another picture. I'm having trouble selecting the existing picture. Below is the code I am using. I believe my problem is with the Selection.MoveEndUntil. I appreciate any help.

ActiveDocument.Bookmarks("BM1").Select
Selection.MoveEndUntil vbCrLf
Selection.Delete
ActiveDocument.Bookmarks("BM1").Select
Selection.InsertFile strFilename
 
J

Jonathan West

mr said:
I'm trying to replace a picture I inserted into a document at a bookmark
using InsertFile with another picture. I'm having trouble selecting the
existing picture. Below is the code I am using. I believe my problem is
with the Selection.MoveEndUntil. I appreciate any help.
ActiveDocument.Bookmarks("BM1").Select
Selection.MoveEndUntil vbCrLf
Selection.Delete
ActiveDocument.Bookmarks("BM1").Select
Selection.InsertFile strFilename

If the picture is inserted inline, then you can replace the first three
lines with the following line

ActiveDocument.Bookmarks("BM1").Range.InlineShapes(1).Delete

In addition, you can then replace the last 2 lines with this line

ActiveDocument.Bookmarks("BM1").Range.InsertFile strFileName

All done without moving the Selection at all!
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?bXI=?=,
I'm trying to replace a picture I inserted into a document at a bookmark using
InsertFile with another picture. I'm having trouble selecting the existing picture.
Below is the code I am using. I believe my problem is with the Selection.MoveEndUntil. I
appreciate any help.
ActiveDocument.Bookmarks("BM1").Select
Selection.MoveEndUntil vbCrLf
Selection.Delete
ActiveDocument.Bookmarks("BM1").Select
Selection.InsertFile strFilename
You don't specify what else might be in the bookmark, nor whether the bookmark needs to be
retained, so it's difficult to give you exact code. Jonathan's suggestion would delete the
bookmark, and possibly anything else *within* the bookmark. So you can see, it's very
important that you describe the circumstances exactly :) Assuming a "worst case
scenario", I'd do this:

Dim rngBookmark as Word.Range
Dim rngInlineShape as Word.Range

Set rngBookmark = ActiveDocument.Bookmarks("BM1").Range
Set rngInlineShape = rngBookmark.InlineShapes(1).Range

rngInlineShape.InlineShapes(1).Delete
rngInlineShape.InsertFile strFileName

'If the bookmark was around the inlineshape, only,
'recreate the bookmark
'In this case, rngInlineShape is not needed
'simply delete the inlineshapes(1) within rngBookmark
ActiveDocument.Bookmarks.Add rngBookmark, "BM1"

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply in the
newsgroup and not by e-mail :)
 
M

mr

I'm not familiar with InlineShapes, but I believe I am not using them because I tried your code and when it runs the Set rngInlineShape code it generates the error "The requested member of the collection does not exist". The bookmark should only contain the picture because it was inserted using the code below and then the document is saved. At a different time the document is opened so that the picture at the BM1 bookmark can be replaced. I would also like to retain the bookmark in case another replacement of the picture is needed at a later time. Is this possible? Thanks.
ActiveDocument.Bookmarks("BM1").Select
Selection.InsertFile strFilename
 
M

mr

I am not familiar with InlineShapes, but I don't believe I am using them because when I run your code I get an error at the Set rngInlineShape line that says "The requested member of the collection does not exist." The bookmark only contains a picture that was inserted using the code:
ActiveDocument.Bookmarks("BM1").Select
Selection.InsertFile strFilename
The document is saved, then at a later time it is opened so that the picture can be replaced. I would also like to retain the bookmark in case the picture needs to be replaced again at a later time. Is this possible? Thanks.
 
M

mr

I am not familiar with InlineShapes, but I don't believe I am using them because when I run your code I get an error at the Set rngInlineShape line that says "The requested member of the collection does not exist." The bookmark only contains a picture that was inserted using the code:
ActiveDocument.Bookmarks("BM1").Select
Selection.InsertFile strFilename
The document is saved, then at a later time it is opened so that the picture can be replaced. I would also like to retain the bookmark in case the picture needs to be replaced again at a later time. Is this possible? Thanks.
 
M

mr

Sorry for the multiple replies. I kept getting an error that the post failed. It finally said successful after the third try.
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?bXI=?=,
I'm not familiar with InlineShapes, but I believe I am not using them because I tried
your code and when it runs the Set rngInlineShape code it generates the error "The
requested member of the collection does not exist". The bookmark should only contain the
picture because it was inserted using the code below and then the document is saved. At a
different time the document is opened so that the picture at the BM1 bookmark can be
replaced. I would also like to retain the bookmark in case another replacement of the
picture is needed at a later time. Is this possible? Thanks.
ActiveDocument.Bookmarks("BM1").Select
Selection.InsertFile strFilename
Interesting...

OK. I'm going to assume there's only the one picture in the document, but you know how
many there are, so can interpret the results of the following accordingly. We need to find
out if this is an InlineShape (and just not within the bookmark), or if it's a Shape.
could you check the count of the pictures in the document, please? I usually use the
Immediate Window:
?ActiveDocument.Shapes.Count

?ActiveDocument.InlineShapes.Count

Then, in order to get a better idea about your project, a couple of follow-up questions:
1. Why are you using InsertFile to insert a picture? Is the pictue in another Word
document?

2. Is this a document whose basic structures you can change? IOW, if the situation I
suspect pertains, the bookmark is not "holding" the picture, but is "beside" where the
picture is (InlineShape). I'd like to have it "hold" the picture, if your concept allows
that.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply in the
newsgroup and not by e-mail :)
 
M

mr

I am using InsertFile because the pictures are in Word documents. I can change the structure if needed. I must make a correction for a previous answer. These are InlineShapes, which I found out after running the commands you asked for in the immediate window. I must have run the code on some old test documents. There are actually 2 bookmarks in these documents, and both are filled using InsertFile to insert a picture that is in another Word document. It looks like sometimes the bookmark is holding the picture, and sometimes it is beside the picture. It depends on how the picture Word document was created. I found that my original code works if the picture Word document only has a paragraph mark after the picture. The code fails if a paragraph mark was placed before the picture. I do not create the picture documents, so I need a solution that will work for both situations, bookmark holding the picture, and bookmark beside the picture. Thanks.
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?bXI=?=,
I am using InsertFile because the pictures are in Word documents. I can change the
structure if needed. I must make a correction for a previous answer. These are
InlineShapes, which I found out after running the commands you asked for in the immediate
window. I must have run the code on some old test documents. There are actually 2
bookmarks in these documents, and both are filled using InsertFile to insert a picture
that is in another Word document. It looks like sometimes the bookmark is holding the
picture, and sometimes it is beside the picture. It depends on how the picture Word
document was created. I found that my original code works if the picture Word document
only has a paragraph mark after the picture. The code fails if a paragraph mark was
placed before the picture. I do not create the picture documents, so I need a solution
that will work for both situations, bookmark holding the picture, and bookmark beside the
picture.Thanks for the more detailed information. I think we can work something out based on it
:) How does this approach sound to you:

1. You use insert/file to bring in the Word document, which also contains a picture
(InlineShape)

2. We suppose that the bookmark will be either beside, or around the picture, but this
isn't 100% certain. Could we be sure, however, that the bookmark is in the same paragraph
with the picture, and that only the one picture is in the paragraph?

Then

Dim sBookmarkName as String
Dim rngInlineShape as Word.Range

Set rngInlineShape =
doc.Boomkarks(sBookmarkName).Range.Paragraphs(1).Range.InlineShapes(1).Range

rngInlineShape.InsertFile strFileName
'Make sure the bookmark still exists, even if it was removed by inserting the new picture
doc.Bookmarks.Add rngInlineShape, sBookmarkName

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply in the
newsgroup and not by e-mail :)
 
M

mr

Thanks. That works for me. I just had to change the order of the parameters on the last line.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top