PDA

View Full Version : [SOLVED:] Inserting image to table from userform



kestrel1306
08-25-2019, 04:07 PM
Hey team,

So I'm working on a document that is created from a userform.

In creating the letter head portion, I currently do this...

GRAPHIMAGE_LOGO = "C:\Users\OEM\Documents\Form_Set\letterhead.png"
.Cell(1, 1).Range.InlineShapes.AddPicture FileName:=GRAPHIMAGE_LOGO, LinkToFile:=False, SaveWithDocument:=True

What I would really like is to have a hidden userform with picture controls in it that hold my images, so I can then grab said image from the userform and fling it into the cell in the document table.

GRAPHIMAGE_LOGO = UserForm999.letterhead.Picture
.Cell(1, 1). [insert amazing code here please]

Thanks in advance.


Regards
Kes

gmayor
08-25-2019, 08:30 PM
Let us assume that you have a userform with four picture controls displaying your images (you can have more or fewer as required) named Image1 to Image4 then the code for the userform would be


Option Explicit


Private Sub Image1_Click()
CopyToCell Image1
Hide
End Sub


Private Sub Image2_Click()
CopyToCell Image2
Hide
End Sub


Private Sub Image3_Click()
CopyToCell Image3
Hide
End Sub


Private Sub Image4_Click()
CopyToCell Image4
Hide
End Sub


Sub CopyToCell(oCtrl As Control)
Const TemporaryFolder = 2
Dim FSO As Object
Dim strImage As String
Dim oRng As Range
Set oRng = ActiveDocument.Tables(1).Cell(1, 1).Range
oRng.End = oRng.End - 1
oRng.Text = ""
Set FSO = CreateObject("scripting.filesystemobject")
strImage = FSO.GetSpecialFolder(TemporaryFolder).Path & "\" & FSO.gettempname
SavePicture oCtrl.Picture, strImage
oRng.InlineShapes.AddPicture strImage
FSO.deletefile strImage
lbl_Exit:
Set oRng = Nothing
Set FSO = Nothing
Exit Sub
End Sub

You can call the userform with a simple macro e.g.

Option Explicit


Sub AutoOpen()
ShowForm
End Sub


Sub ShowForm()
UserForm1.Show
Unload UserForm1
End Sub

Click one of the images to insert it in cell 1 - see attached.

kestrel1306
08-26-2019, 04:00 PM
Thank you for the quick reply, I'll get to work on integrating it.

Thanks
Kes

kestrel1306
08-27-2019, 05:22 AM
Worked perfectly, shame I can't upvote you!