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:
I got a lot of my code from here:
The "inteli sense" as its called does show this
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
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
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