Run Time error 91 in Dir function

Eric Penfold

Active Member
Joined
Nov 19, 2021
Messages
431
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Please can somebody help me to understand why the Dir function says Runtime Error 91

VBA Code:
Private Sub OpenFolder_Click()

    Dim SourcePath As String
    Dim SubPath As String
    Dim strFolder As String
    Dim MyPath As String
    Dim PDFFName As String
    Dim CmbData

        CmbData = Split(Me.OpenDrawing.Value, "-")
        CmbData(0) = Replace(CmbData(0), "-", "")
        
        MyPath = "C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe"
        SourcePath = "\\dc01\Company\R&D\Drawing Nos"
        SubPath = CStr(Val(Int(CmbData(0) / 50) * 50 + 1) & "-" & Int(CmbData(0) / 50 + 1) * 50)
        
        PDFFName = OpenDrawing.Value
        strFolder = SourcePath & "\" & SubPath & "\" & Int(CmbData(0))
        
    ActiveWorkbook.FollowHyperlink Address:=strFolder, NewWindow:=True

End Sub
Private Sub Fill_DrNumbers_Click()

        Dim myfso As FileSystemObject, myfolder As Object, myFile As Object, Dir As Object
        Dim SourcePath As String
        Dim SubPath As String
        Dim PdfFolder As Folder
        Dim PdfFile As String
        Dim MyPath As String
        Dim PDFFName As String
        Dim CmbData
    


        CmbData = Split(Me.OpenDrawing.Value, "-")
        CmbData(0) = Replace(CmbData(0), "-", "")
        
        MyPath = "C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe"
        SourcePath = "\\dc01\Company\R&D\Drawing Nos"
        SubPath = CStr(Val(Int(CmbData(0) / 50) * 50 + 1) & "-" & Int(CmbData(0) / 50 + 1) * 50)
        
                
        Me.PdfDrawingList.Clear
        
        Set myfso = New Scripting.FileSystemObject
        Set myfolder = myfso.GetFolder(SourcePath & "\" & SubPath & "\" & Int(CmbData(0)))
        Set myFile = Dir(myfolder & ".Pdf")
        
        If Right(myfolder, 1) <> "\" Then
        myfolder = myfolder & "\"
        End If

        
        Do While Len(myFile) > 0
        If Right(myFile, 3) = "pdf" Then
        Me.PdfDrawingList.AddItem myFile.Name
        End If
        
        myFile = Dir

        Loop

        End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Dir returns a string, not an object. You need to declare myfile as string and remove the Set from the error line.
 
Upvote 0
Thanks that`s sorted that
But the code below is showing invalid qualifier on myfile.

VBA Code:
        Me.PdfDrawingList.AddItem myFile.Name
 
Upvote 0
Just use myfile, not myfile.name
 
Upvote 0
Solution

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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