0

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.

Richa
  • 73
  • 7
  • 2
    `Fl` and `F1` are not the same – Storax Aug 10 '22 at 14:21
  • 1
    I suspect you just want `Filename:=Fl`. In recording a macro, the `Filename` parameter took the full file path. That said, you really really really need to use `Option Explicit`. – BigBen Aug 10 '22 at 14:26

1 Answers1

2

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.

Storax
  • 11,158
  • 3
  • 16
  • 33