Insert Picture as Icon with Custom Name

Sumeluar

Active Member
Joined
Jun 21, 2006
Messages
274
Office Version
  1. 365
  2. 2016
  3. 2010
Platform
  1. Windows
  2. MacOS
  3. Mobile
Good day - I do not recall where I got the below macro from, can I get some assistance to have it modified to do the following:

- Allow to choose .gif; *.jpg; *.bmp; *.tif), *.gif; *.jpg; *.bmp; *.tif" (already doing so)
- Allow to customize a name to be visible at the bottom of the image
- Change the image to show it as an icon instead as if it was an embeded object

Sub InsertPicture()
Dim sPicture As String, pic As Picture
sPicture = Application.GetOpenFilename _
("Pictures (*.gif; *.jpg; *.bmp; *.tif), *.gif; *.jpg; *.bmp; *.tif", _
, "Select Picture to Import")
If sPicture = "False" Then Exit Sub
Set pic = ActiveSheet.Pictures.Insert(sPicture)
With pic
.ShapeRange.LockAspectRatio = True
.Height = ActiveCell.Height
.Top = ActiveCell.Top
.Placement = xlMoveAndSize
End With
Set pic = Nothing
End Sub


Any assistance would be greatly appreciated.

Regards, Sumeluar
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
You can use the Add method of the OleObjects object, for example...

VBA Code:
Sub InsertPicture()

    Dim sPicture As String
    Dim oOleObj As OLEObject
    
    sPicture = Application.GetOpenFilename( _
        FileFilter:="Pictures (*.gif; *.jpg; *.bmp; *.tif), *.gif; *.jpg; *.bmp; *.tif", _
        Title:="Select Picture to Import")
    
    If sPicture = "False" Then Exit Sub
    
    Set oOleObj = ActiveSheet.OLEObjects.Add( _
        Filename:=sPicture, _
        Link:=False, _
        DisplayAsIcon:=True, _
        IconFileName:="C:\Program Files (x86)\Internet Explorer\iexplore.exe", _
        IconIndex:=13, _
        IconLabel:="YourCustomizedNameHere")
        
    oOleObj.Height = ActiveCell.Height
        
End Sub

Change the IconFileName, IconIndex, and IconLabel accordingly. Have a look at the following link for additional information...


Hope this helps!
 
Upvote 0
You can use the Add method of the OleObjects object, for example...

VBA Code:
Sub InsertPicture()

    Dim sPicture As String
    Dim oOleObj As OLEObject
   
    sPicture = Application.GetOpenFilename( _
        FileFilter:="Pictures (*.gif; *.jpg; *.bmp; *.tif), *.gif; *.jpg; *.bmp; *.tif", _
        Title:="Select Picture to Import")
   
    If sPicture = "False" Then Exit Sub
   
    Set oOleObj = ActiveSheet.OLEObjects.Add( _
        Filename:=sPicture, _
        Link:=False, _
        DisplayAsIcon:=True, _
        IconFileName:="C:\Program Files (x86)\Internet Explorer\iexplore.exe", _
        IconIndex:=13, _
        IconLabel:="YourCustomizedNameHere")
       
    oOleObj.Height = ActiveCell.Height
       
End Sub

Change the IconFileName, IconIndex, and IconLabel accordingly. Have a look at the following link for additional information...


Hope this helps!
Domenic - Thank you for the help, I tried your approach but I could not get it to do what I am after as I was unable to input the custom name and when I saved the file the icon went to default, I made some modifications and now is doing exactly what I want until I "Save" it, at that point it loses the icon and the custom name.

This is where I have it at:

Sub Domenics_Modified()

Dim sPicture As String
Dim oOleObj As OLEObject
Dim embeddedFileName As String
sPicture = Application.GetOpenFilename( _
FileFilter:="Pictures (*.gif; *.jpg; *.bmp; *.tif), *.gif; *.jpg; *.bmp; *.tif", _
Title:="Select Picture to Import")
If sPicture = "False" Then Exit Sub
Do
embeddedFileName = VBA.InputBox("Enter custom picture name.", "Picture Name", "Custom Name")
If StrPtr(embeddedFileName) = 0 Then Exit Sub
Loop Until Len(embeddedFileName) > 0
Set oOleObj = ActiveSheet.OLEObjects.Add( _
Filename:=sPicture, _
Link:=False, _
DisplayAsIcon:=True, _
IconFileName:="E:\Icons\Picture.ico", _
IconIndex:=0, _
IconLabel:=embeddedFileName)
oOleObj.Top = ActiveCell.Top
oOleObj.Left = ActiveCell.Left
With oOleObj
.ShapeRange.Fill.Transparency = 1
.Border.LineStyle = xlNone
End With
End Sub

Regards, Sumeluar
 
Upvote 0
Yes, I can confirm the same behaviour. Unfortunately, I don't know why this happens, I'll have to do some research.

If I find a solution, I'll post back. In the meantime, maybe someone else here on the Board will have a suggestion.

Cheers!
 
Upvote 0
Okay, from what I can tell, it's likely due to an incorrect IconFileName. Here are a couple of suggestions...

1) Record a macro while you manually insert your object onto the worksheet, and see which file the generated code points to.

2) Have a look at the solution offered by @RoryA in the following link...


Hope this helps!
 
Upvote 0
If all else fails, you can simply set your IconLabel without the icon...

VBA Code:
Set oOleObj = ActiveSheet.OLEObjects.Add( _
        Filename:=sPicture, _
        Link:=False, _
        DisplayAsIcon:=True, _
        IconFileName:="", _
        IconIndex:=-1, _
        IconLabel:="YourCustomizedNameHere")
 
Upvote 0
If all else fails, you can simply set your IconLabel without the icon...

VBA Code:
Set oOleObj = ActiveSheet.OLEObjects.Add( _
        Filename:=sPicture, _
        Link:=False, _
        DisplayAsIcon:=True, _
        IconFileName:="", _
        IconIndex:=-1, _
        IconLabel:="YourCustomizedNameHere")
Domenic - Thanks for the help but still no can do, when I save the workbook the picture does not preserve the new name.

Regards, Sumeluar.
 
Upvote 0
Domenic - Thanks for the help but still no can do, when I save the workbook the picture does not preserve the new name.

Regards, Sumeluar.
I did test it before posting the solution, and I just tested it again just to be sure, and it seems to work. When I save the workbook, it does save the new name.

Did you try the other suggestions in Post #5 ?
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,111
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