Copy Image from Excel to Word bookmark

abdelrahman720

New Member
Joined
May 10, 2022
Messages
6
Office Version
  1. 2016
Platform
  1. Windows
Hi all ,

I'm new her , also I'm new at VBA as well :)

I'm trying to make a excel VBA code copying data (including images) from excel workbook to word template bookmarks

this code should update the word document if opened and open the word document if it's not

so i wrote the following code

VBA Code:
Sub VBACODE()

Dim objWord As Object
Dim ws As Worksheet

    Set ws = ActiveWorkbook.ActiveSheet

    Set objWord = CreateObject("Word.Application")

    objWord.Visible = True

    objWord.Documents.Open "WORD TEMPLATE PATH"

    With objWord.ActiveDocument
        .Bookmarks("Text1").Range.Text = Format(ws.Range("P4").Value, "#,##0.00")
        .Bookmarks("Text2").Range.Text = ws.Range("Q4").Value
                    
         End With

  ws.Range(Array("Picture 1")).CopyPicture
  objWord.ActiveDocument.Bookmarks("Pic1").Range.Paste
 
    Set objWord = Nothing

          
End Sub

it's only copying text data from excel to word bookmarks , but crashes when copy/paste images

also i still need to update the word documents if opened and open it if it's closed

Appreciate your help
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
You can trial this for pasting the image..
Code:
Sub UpdateBookmark(BookmarkToUpdate As String)
Dim ORng As Object

  'make sure bookmark exists
     If ActiveDocument.Bookmarks.Exists(BookmarkToUpdate) = True Then
          Set ORng = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
          ORng.Delete
          ORng.Paste
          ActiveDocument.Bookmarks.Add BookmarkToUpdate, ORng
          Application.CutCopyMode = False
     End If
Set ORng = Nothing
End Sub
So after you copy your picture you would use this line of code...
Code:
Call UpdateBookmark("pic1")
HTH. Dave
 
Upvote 0

Forum statistics

Threads
1,223,882
Messages
6,175,165
Members
452,615
Latest member
bogeys2birdies

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