Home > OS >  Insert object in Excel via VBA
Insert object in Excel via VBA

Time:08-12

I'm trying to write a macro in which the user gets an pop up in which he/she can select the file which they want to upload. (mainly images) I've got an .OLEObjects setup, but I keep getting an error.

VBA so far:

Sub add_file()

Dim Fl As Variant
Dim Filename As String

ChDrive "C:"
ChDir "C:\temp"

Fl = Application.GetOpenFilename()
If Fl = False Then Exit Sub

Filename = Mid$(F1, InStrRev(F1, "\")   1, Len(F1))

Sheet1.OLEObjects.Add Filename:=Filename, Link:=True, DisplayAsIcon:=True, IconIndex:=0, IconLabel:=Filename

End Sub 

Error:

Run-time error '1004':
Add method of OLEObjects class failed

What am I missing? Thanks in advance.

CodePudding user response:

Use Option Explicit and

Filename = Mid$(Fl, InStrRev(Fl, "\")   1, Len(Fl))

You may want to read this post although it covers .Net it is also applicable for VBA.

  • Related