vba for inserting picture into textbox

robertkrz

Board Regular
Joined
Apr 25, 2004
Messages
52
Hi all,
I know how to insert picture in to worksheet by VBA but can not figure out how to insert a picture into a textbox (preferably with keeping ratio).
Have you seen such anywhere ? :pray:
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
By TextBox you mean the TextBox from the 'Drawing' toolbar ? if so, you do it something like this:

<font face=Courier New>
<SPAN style="color:#00007F">Sub</SPAN> ShapePicture()
    <SPAN style="color:#00007F">Dim</SPAN> Sh <SPAN style="color:#00007F">As</SPAN> Shape
    
    <SPAN style="color:#00007F">Set</SPAN> Sh = Sheets("Sheet1").Shapes(1)
    Sh.Fill.UserPicture "D:\My Documents\My Pictures\DSC00090.JPG"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>

If you want to resize the shape to adjust the ratio of the picture, that requires a bit more work:

<font face=Courier New>
<SPAN style="color:#00007F">Sub</SPAN> ShapePicture2()
    <SPAN style="color:#00007F">Dim</SPAN> Sh <SPAN style="color:#00007F">As</SPAN> Shape, Pic <SPAN style="color:#00007F">As</SPAN> IPictureDisp
    
    <SPAN style="color:#00007F">Set</SPAN> Pic = LoadPicture("D:\My Documents\My Pictures\DSC00090.JPG")
    <SPAN style="color:#00007F">Set</SPAN> Sh = Sheets("Sheet1").Shapes(1)
    
    <SPAN style="color:#007F00">'Resize the picture, keeping the Width constant</SPAN>
    Sh.Height = Pic.Height / Pic.Width * Sh.Width
    
    <SPAN style="color:#007F00">'Clear Pic from memory</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> Pic = LoadPicture("")
    <SPAN style="color:#00007F">Set</SPAN> Pic = <SPAN style="color:#00007F">Nothing</SPAN>
    
    <SPAN style="color:#007F00">'Load the picture</SPAN>
    Sh.Fill.UserPicture "D:\My Documents\My Pictures\DSC00090.JPG"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

</FONT>
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,141
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