Run Time error 91 in Dir function

Eric Penfold

Active Member
Joined
Nov 19, 2021
Messages
417
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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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,213,510
Messages
6,114,040
Members
448,543
Latest member
MartinLarkin

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