VBA code to fit object size as cell size

kkyuvaraj

New Member
Joined
Apr 25, 2019
Messages
37
Hi,
I have a below VBA code to attach an object in a defined cell in a Worksheet.

In the below code, the attached object is not fit to the size of the cell height & width, hence i need to add code in below code to fit the the attached object to the cell height & width.

And also i need to add the code to call for a message box if no file is selected using below code.(Like MsgBox "No item selected.", vbInformation, "NO SELECTION").If any file selected and uploaded then this message box should not appear.

I am using below macro code in a Button to attach an object.

Public Sub insertFile()

'Select the cell in which you want to place the attachment
Range("D6").Select

'Get file path
fpath = Application.GetOpenFilename("All Files,*.*", Title:="Select file")
If LCase(fpath) = "false" Then Exit Sub

'Insert file
ActiveSheet.OLEObjects.Add _
Filename:=fpath, _
Link:=False, _
DisplayAsIcon:=True, _
IconFileName:="explorer.exe", _
IconIndex:=0, _
IconLabel:=extractFileName(fpath)

End Sub


Public Function extractFileName(filePath)

For i = Len(filePath) To 1 Step -1
If Mid(filePath, i, 1) = "\" Then
extractFileName = Mid(filePath, i + 1, Len(filePath) - i + 1)
Exit Function
End If
Next

End Function
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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