Selecting Images in Word

MEJP1

New Member
Joined
Apr 10, 2019
Messages
4
I am producing a report which needs to be run daily. I have some pictures in Excel and I want to copy them and paste them into Word. I need them in a certain location. All this needs to be done in VBA.
My proposed way of doing this is by creating template pictures in Word. I assume that I can then copy the pictures from Excel and paste them over the template pictures (i.e. replacing the template pictures). I believe this type of technique is possible with Excel-Powerpoint.
However, I don't know how to select the existing images in Word.

I don't know if this helps but I know that you can name images in Word (see Home > Select > Selection Panel) but I don't know how to actually make a code that selects named images.

I have tried googling for solutions but all the answers seem to revolve around selecting images that have just been pasted into Word from Excel. I want to select images that already exist in Word.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
You don't need to create 'template pictures' in Word; simply insert bookmarks where you want the pictures to go and paste them at those bookmarks. For example:
Code:
wdDoc.Bookmarks("BookmarkName").Range.Paste
That said, if you do use 'template pictures', you can bookmark them and the above code will replace them.
 
Upvote 0
You don't need to create 'template pictures' in Word; simply insert bookmarks where you want the pictures to go and paste them at those bookmarks. For example:
Code:
wdDoc.Bookmarks("BookmarkName").Range.Paste
That said, if you do use 'template pictures', you can bookmark them and the above code will replace them.

Thanks for taking the time to reply.

I need to run this VBA code everyday. I think with your solution, the Word document I am working with will be filled with pictures from the previous days. I suggested using a "template picture" so that I can replace the previous day's images with the latest image.

Sorry for cross-posting. I have read the notice you sent.
 
Upvote 0
I need to run this VBA code everyday. I think with your solution, the Word document I am working with will be filled with pictures from the previous days.
The code I posted will replace the existing bookmarked picture but, under some circumstances, the bookmark range may not be preserved - which would indeed result in the addition of pictures rather than their replacement.

Why are you changing the pictures in an existing document each day instead of creating new documents from a Word template and updating those? That said, the method I posted can be adapted to preserve the bookmarks so they can be updated again later on. For example:
Code:
Sub UpdateBookMark(wdDoc As Object, BmkNm As String)
Dim BmkRng As Object
With wdDoc
  If .Bookmarks.Exists(BmkNm) Then
    Set BmkRng = .Bookmarks(BmkNm).Range
    BmkRng.Paste
    .Bookmarks.Add BmkNm, BmkRng
  End If
End With
Set BmkRng = Nothing
End Sub
 
Upvote 0
The code I posted will replace the existing bookmarked picture but, under some circumstances, the bookmark range may not be preserved - which would indeed result in the addition of pictures rather than their replacement.

Why are you changing the pictures in an existing document each day instead of creating new documents from a Word template and updating those? That said, the method I posted can be adapted to preserve the bookmarks so they can be updated again later on. For example:
Code:
Sub UpdateBookMark(wdDoc As Object, BmkNm As String)
Dim BmkRng As Object
With wdDoc
  If .Bookmarks.Exists(BmkNm) Then
    Set BmkRng = .Bookmarks(BmkNm).Range
    BmkRng.Paste
    .Bookmarks.Add BmkNm, BmkRng
  End If
End With
Set BmkRng = Nothing
End Sub

Thanks for the reply. I will try this. Unfortunately, I have to use the existing Word document because it has a lot of charts which link to other Excel files.
 
Upvote 0
The code I posted will replace the existing bookmarked picture but, under some circumstances, the bookmark range may not be preserved - which would indeed result in the addition of pictures rather than their replacement.

Why are you changing the pictures in an existing document each day instead of creating new documents from a Word template and updating those? That said, the method I posted can be adapted to preserve the bookmarks so they can be updated again later on. For example:
Code:
Sub UpdateBookMark(wdDoc As Object, BmkNm As String)
Dim BmkRng As Object
With wdDoc
  If .Bookmarks.Exists(BmkNm) Then
    Set BmkRng = .Bookmarks(BmkNm).Range
    BmkRng.Paste
    .Bookmarks.Add BmkNm, BmkRng
  End If
End With
Set BmkRng = Nothing
End Sub

Sorry to bother you again but I am at a very beginner stage with VBA.

Here is the part of my code that is supposed to deal with the transfer of the images from Excel to Word so far. I am not quite sure how to incorporate what you wrote into it.

Code:
Dim wd As Object
Dim ObjDoc As Object
Dim FilePath As String
Dim FileName As String
FilePath = "OMITTED_FOR_PRIVACY1"
FileName = "OMITTED_FOR_PRIVACY2.docx"

On Error Resume Next
    Set wd = GetObject(, "Word.Application")

If wd Is Nothing Then
    Set wd = CreateObject("Word.Application")
    Set ObjDoc = wd.Documents.Open(FilePath & "" & FileName)
Else
    On Error GoTo notOpen
    Set ObjDoc = wd.Documents(FileName)
    GoTo OpenAlready
notOpen:
    Set ObjDoc = wd.Documents.Open(FilePath & "" & FileName)
End If
OpenAlready:
On Error GoTo 0

For Each CCY In Worksheets("MarketLive").Range("O1:O9")
    wd.Visible = True
    Sheets("rKO").Pictures(CCY & " C RKO").Copy

    wd.Visible = True
    Sheets("rKO").Pictures(CCY & " P RKO").Copy
    
End Sub


  • If I copy and paste your code, don't I have some duplicated items? Do I need to "Dim wdDoc As Object" when I already have "Dim wd As Object".
  • If I have multiple word documents open, how does your code know which Word Document to locate the bookmark in/paste to? In my previous code, I have specified a file path but I don't know how to reconcile it with your code...
  • I should type "BmkNm = "NAME_OF_MY_BOOKMARK"", right?
  • Does BmkRng.Paste paste over all the existing items in that bookmark? What if I have other charts/text after the bookmark that I don't want to replace?
  • Where do I set the bookmark? I thought I could manually set and name a bookmark and then refer to it in the code. Is it possible to clear the bookmark at the end of the code?
  • As you can see, right now, my code uses a For Each loop to select each image from the Excel file one-by-one. I guess I now need to select all the images at once before pasting...

Really sorry for all the questions and I appreciate your help!
 
Upvote 0
Your original code referred to wdDoc - your latest refers to ObjDoc. Some consistency, please.

To use the code I posted, all you need do is insert a line like:
Call UpdateBookMark(wdDoc, "BookmarkName")
or:
Call UpdateBookMark(ObjDoc, "BookmarkName")
as appropriate, after each line like:
Sheets("rKO").Pictures(CCY & " C RKO").Copy
[*]If I copy and paste your code, don't I have some duplicated items? Do I need to "Dim wdDoc As Object" when I already have "Dim wd As Object".
[*]If I have multiple word documents open, how does your code know which Word Document to locate the bookmark in/paste to? In my previous code, I have specified a file path but I don't know how to reconcile it with your code...
[*]I should type "BmkNm = "NAME_OF_MY_BOOKMARK"", right?
[*]Does BmkRng.Paste paste over all the existing items in that bookmark? What if I have other charts/text after the bookmark that I don't want to replace?
[*]Where do I set the bookmark? I thought I could manually set and name a bookmark and then refer to it in the code. Is it possible to clear the bookmark at the end of the code?
[*]As you can see, right now, my code uses a For Each loop to select each image from the Excel file one-by-one. I guess I now need to select all the images at once before pasting...
[/LIST]
1. No.
2. The wdDoc or ObjDoc reference takes care of that.
3. Not necessarily. As indicated above, you can use the bookmark name directly in the Call statement.
4. Obviously, you'd have a bookmark that only applies to what is to be replaced.
5. Please read my reply in post #2 .
6. Not so. If you want to use the Call statement inside a loop, you merely need a way of identifying the correct bookmark name on each iteration. (e.g. use a counter with a line like:
Call UpdateBookMark(wdDoc, "Pic" & i)
or:
Call UpdateBookMark(ObjDoc, "Pic" & i)
where the bookmarks in the document are named Pic1 to Picn.
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,138
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top