Excel to Word with Template: How do I place images in a specific place?

Mescoman

New Member
Joined
Oct 27, 2021
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hey hey people

I am currently working on an Excel VBA Macro on 365 which creates a new Word document using a template Word document, copying data from an Excel workbook into the new Word document. I have managed to use the "find" function to replace text as I want, as well as select images from Excel and paste them into the Word document. What I havent been able to figure out is how to choose where I paste an image. This is the code I have used so far:
VBA Code:
Sub Image_to_word_MRXL()

    Dim wd As Word.Application
    Dim doc As Word.Document
    
    'Set wd = New Word.Application
    Set wd = CreateObject("Word.Application")
    wd.Visible = True

    'Location of Template
    Dim Word_Template As String
    
    Word_Template = "C:\LOCATION_OF_TEMPLATE_HERE\NAME_OF_TEMPLATE_HERE"
    Word_Template = "C:\Marco\work\Excel stuff\Learning excel\Sending data excel to word debugging\Word_Receive_Image_Template"
    
    'Creates Word Document
    wd.Documents.Add template:=Word_Template, NewTemplate:=False, DocumentType:=0
     
    'We select the image we want
    ActiveSheet.DrawingObjects.Select
    Selection.Copy
    
    'With our selection we can paste the image to the top of the document.
    With wd.Selection
    .PasteSpecial
    End With
    
    'With the selection.find command, we can replace text, even text from the excel document.
    With wd.Selection.Find
    .Text = "***Replace***"
    .Replacement.Text = "Replacement text here"
    '.Replacement.Text = ActiveSheet.Cells(1, 1).Value
    '.Replacement.Text = Range("Variable_name").Value
    .Execute Replace:=2
    End With
    
    MsgBox ("The code has finished running")
    
    
    
End Sub

I got a lot of my code from here:

The "inteli sense" as its called does show this

1637054629373.png

We can see a "Placement" suggestion, but unfortunately as far as I can tell the [Placement] input refers to how the image is wrapped (square, in front of text, behind text). The suggestions are all nice but none of them seem to address what I want.

Any ideas on what I should try? An idea I had was to try and figure out a way to select some text, a bookmark or maybe even another "named" image so that I may be able to use the wd.Selection.Paste command. Or maybe if I could paste the image with a specific name, I could use VBA to move it afterwards (I dont know how to do this)? I would like to keep all my code on the Excel Workbook and not have any macros on the Word document. Many thanks :)
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hey hey people.

I searched far and wide and finally I think I found a good answer to my own question, I thought I should post it here since I would have appreciated it greatly myself. I found the answer on How to Create Word Documents Within Excel VBA | Hacker Noon

The basis of the answer is the .selection.goto command, where we can use it like this:

VBA Code:
With wd.Selection
    .Goto wdGoToBookmark, , , "BookMark_name"
    .PasteSpecial
    End With

For a bit more guidance on how this Goto command works, Id recommend using this website, it explains it pretty well: Selection.GoTo method (Word)

I will warn anyone working on this to be careful editing templates. Its not that its particularly unsafe for your computer as far as I know, but that opening a template will as intended open a copy of the template rather than the template itself: you must save over its own name and make sure its being saved in the correct location (it likes to suggest a new location). This probably wasted a few hours of my time until I realized what was going on.

So youre welcome past me :)
 
Upvote 0
Solution

Forum statistics

Threads
1,224,817
Messages
6,181,149
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