Working with pictures in Excel.

unluckyuser

New Member
Joined
Jan 12, 2025
Messages
26
Office Version
  1. 2019
Platform
  1. Windows
I'm got a picture image located in cell D1, which I've named PicD1. The ultimate goal is that I want the picture to appear in my VBA userform. However, it appears the only way to achieve this is to extract the picture from the Excel file, write it to the hard drive as a jpg or png, and then link the jpg or png file to the control in the userform. That seems pretty cumbersome but I don't see another way. Any comments greatly appreciated.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
You could just copy the picture (Select it and press CTRL+C) and then go to the VBE, select the userform control and Paste (CTRL+V) in the Picture field located in the Properties pane.
 
Upvote 0
You could just copy the picture (Select it and press CTRL+C) and then go to the VBE, select the userform control and Paste (CTRL+V) in the Picture field located in the Properties pane.
very nice, can I do this with VBA when I load the next record? When I iterate through the records, I need the next picture to load. Thanks.
 
Upvote 0
can I do this with VBA when I load the next record? When I iterate through the records, I need the next picture to load. Thanks.
Yes. Do a search for OleCreatePictureIndirect which is an api that creates a VB stdPicture Object from a bitmap handle ... You get the bitmap object from the clipboard after having copied the picture.
 
Last edited:
Upvote 0
This appears to solve it. Now I need to drink a beer..... and I don't drink at all :).



' NOTE: MyChart is the new chart where the image will eventually be copied to
' in order to then be exported as a jpg file
Dim MyChart As Chart


' NOTE: The workbook sheet "Sheet1" will be made the active sheet
Sheet1.Activate

' NOTE: This will past whatever is currently in the clipboard to the active sheet
' So, make sure that your image is what was most recently copied
'
ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range("c1")

' NOTE: This is creating the new Chart
Set MyChart = Charts.Add

' NOTE: This is name the new chart
MyChart.Name = "HELLO"

' NOTE: This is moving the chart to the sheet where the picture is
Set MyChart = MyChart.Location(Where:=xlLocationAsObject, Name:="Sheet1")

' NOTE: This is making the width and height of the MyChart equal to whatever image you copied to the clipboard
MyChart.ChartArea.Width = Sheet1.Shapes(2).Width
MyChart.ChartArea.Height = Sheet1.Shapes(2).Height

' NOTE: This is removing the shape container boarder
MyChart.Parent.Border.LineStyle = 0

' NOTE: This is copying the image that was pasted to the sheet "Sheet1"
Sheet1.Shapes(2).Copy

' NOTE: This is selecting or making active the ChartArea of MyChart
MyChart.ChartArea.Select

' NOTE: This is pasting the image that was just copied via VBA of EXCEL into the MyChart ChartArea
MyChart.Paste

' NOTE: This is exporting the MyChart to a jpg file.
' Filename:="location of folder\name of the file.XXX"
' FilterName:="jpg" read about this at - > Chart.Export method (Excel)
MyChart.Export Filename:="c:\hold\Me.jpg", FilterName:="jpg"

' NOTE: Make cell (Row 1, Column A) the active cell
Sheet1.Cells(1, 1).Activate

' NOTE: Delete the Chart you created earlier
Sheet1.ChartObjects(Sheet1.ChartObjects.Count).Delete

' NOTE: Delete the (hopefully) only 1 shape (image pasted ealier) on the sheet "Sheet1"
Sheet1.Shapes(1).Delete

Me.Image1.Picture = LoadPicture("c:\hold\Me.jpg")



End Sub
 
Upvote 0

Forum statistics

Threads
1,226,453
Messages
6,191,134
Members
453,642
Latest member
jefals

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