How to make consistent embedded object icons' size in vba

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
I use the below code to insert object icons to fit active cells in a column but they are irregular. How to modify the code to fit the icon to the size of cell.

Code

Sub SelectOLE()
Dim objFileDialog As Office.FileDialog

Set objFileDialog = Application.FileDialog(MsoFileDialogType.msoFileDialogFilePicker)

objFileDialog.AllowMultiSelect = False
objFileDialog.ButtonName = "Select File"
objFileDialog.Title = "Select File"
objFileDialog.Show

If (objFileDialog.SelectedItems.Count > 0) Then

Set f = ActiveSheet.OLEObjects.Add _
(Filename:=objFileDialog.SelectedItems(1), _
Link:=False, _
DisplayAsIcon:=True, _
IconLabel:=objFileDialog.SelectedItems(1), _
Top:=ActiveCell.Top, _
Left:=ActiveCell.Left _
)
f.Select

f.Width = ActiveCell.Width
f.Height = ActiveCell.Height

End If

End Sub

Capture.PNG
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try setting the LockAspectRatio to msoFalse before setting the width and height properties...

VBA Code:
With f
    .ShapeRange.LockAspectRatio = msoFalse
    .Width = ActiveCell.Width
    .Height = ActiveCell.Height
End With

Hope this helps!
 
Upvote 0
Always use LockAspectRatio to True in order to respect the homothety, to not distort the picture …​
 
Upvote 0
Try setting the LockAspectRatio to msoFalse before setting the width and height properties...

VBA Code:
With f
    .ShapeRange.LockAspectRatio = msoFalse
    .Width = ActiveCell.Width
    .Height = ActiveCell.Height
End With

Hope this helps!
Thanks Domenic,
It works now !
 
Upvote 0
Always use LockAspectRatio to True in order to respect the homothety, to not distort the picture …​
Thanks Marc,
I don't mind the icon is distort, I just want to fix its size within a cell.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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