I have a worksheet that enables users to attach files in it.
I coded in VBA so that every time a user clicks the button, Excel will attach a document by inserting an object into the worksheet.
My problem is that I want the object to be appeared as a document icon, but I want the icon to be an image in my worksheet.
I know I can use the IconFileName attribute in ActiveSheet.OLEObjects.Add, but the image is in the worksheet which I don't have a separate image file link, and I want to keep it one document so that I don't need to send two files to other people.
Below is the coding.
Sub AddFile()
'~~> Sample icons for relevant files
Const pdfIco = "C:\windows\Installer\{AC76BA86-7AD7-1033-7B44-AA1000000001}\PDFFile_8.ico"
Const wrdIco = "C:\PROGRA~2\MICROS~3\Office14\WINWORD.EXE"
Const xlsIco = "C:\windows\Installer\{90140000-0011-0000-0000-0000000FF1CE}\xlicons.exe"
Dim Xl, Wb, Ws, ol, FileToBeInserted, lbl
'~~> Let user select a file
FileToBeInserted = Application.GetOpenFilename( _
fileFilter:="Word Documents (*.doc;*.docx), *.doc;*.docx,PDF Files (*.pdf), *.pdf,Excel files (*.xls;*.xlsx;*.xlsm), *.xls;*.xlsx;*.xlsm", _
Title:="Select a file or files", _
MultiSelect:=False)
'~~> Check if the user selected a file
If FileToBeInserted <> False Then
'~~> Get the file name for the label
lbl = GetFileName(FileToBeInserted)
'Default icon
icoIndex = 0
'~~> I am testing for just 3 extensions. Amend as applicable
Select Case GetFileExtn(FileToBeInserted)
Case "PDF"
icoIndex = 0
icoFile = pdfIco
Case "DOCX", "DOC"
icoFile = wrdIco
Case "XLS", "XLSX", "XLSM"
icoFile = xlsIco
End Select
'~~> Create the object
ActiveSheet.OLEObjects.Add(, FileToBeInserted, Link:=False, DisplayAsIcon:=True, IconFileName:=icoFile, IconIndex:=icoIndex, IconLabel:="").Select
' Delete line and fill
With Selection.ShapeRange
.Line.Visible = msoFalse
.Fill.Visible = msoFalse
End With
End If
End Sub
Here is the file. The icon is in F3,F4.
attach.xlsm
Big thanks to all experts!
Bookmarks