Error 91 object Variable not Set

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Error 91 object Variable not Set

Post by D Willett »

I'm sure I've come across this before in my code, I can't find my notes on it.

I'm getting the object variable 91 error: Would someone just mind checking my code?

Code: Select all

Dim fso As FileSystemObject

    If Me.AcrobatPath.Text = "" Then
    MsgBox "Please Select a File to Delete", vbInformation, "Document Delete"
    Exit Sub
    End If
    
    If MsgBox("This Document Will Be Deleted " & Me.AcrobatPath.Text, _
            vbYesNo + vbInformation) = vbYes Then
            fso.DeleteFile (Me.AcrobatPath.Text)
            MsgBox "Document Deleted", , "Deleted"
            Me.File1.Refresh
    Else
    MsgBox "Document Not Deleted", , "Cancelled"
    End If
Set fso = Nothing
Cheers ...

Dave.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Error 91 object Variable not Set

Post by Rudi »

I think you need to Set you object reference.
Set fso = ... (or something more relevant!)
Last edited by Rudi on 26 Mar 2014, 16:21, edited 1 time in total.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78229
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Error 91 object Variable not Set

Post by HansV »

You can either use

Code: Select all

    Dim fso As New Scripting.FileSystemObject
or

Code: Select all

    Dim fso As Scripting.FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78229
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Error 91 object Variable not Set

Post by HansV »

Rudi,

Code: Select all

Set fso as Object 
won't work - it is syntactically incorrect. You can use Dim ... As ..., and you can use Set ... = ..., but not Set ... As ...
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Error 91 object Variable not Set

Post by Rudi »

HansV wrote:Rudi,

Code: Select all

Set fso as Object 
won't work - it is syntactically incorrect. You can use Dim ... As ..., and you can use Set ... = ..., but not Set ... As ...
Sorry, I saw that just after I posted it and went back to do a basic edit on my reply. Had a weak moment of thought there. Set as?? Silly me!
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Error 91 object Variable not Set

Post by D Willett »

Cheers guys.
I used

Dim fso As New Scripting.FileSystemObject

and added "True" to the end of this statement to allow me to get over error 70, permission denied.

fso.DeleteFile (Me.AcrobatPath.Text), True
Cheers ...

Dave.