Vba to test file path error

Gealer

New Member
Joined
Sep 19, 2018
Messages
19
I have this code which I need to test the file path, as if your are working offline then the file path will not exist. It will only work if you are connected to the server.

Private Sub Workbook_Open()

Dim sFolderPath As String

Dim sCurrentWorkbookName As String



sFolderPath = "\\ SHARED\Public Folder\Switch Off"

If Right(sFolderPath, 1) <> "\" Then

sFolderPath = sFolderPath & "\"

End If

Application.DisplayAlerts = False

If Dir(sFolderPath, vbDirectory) <> vbNullString Then

sCurrentWorkbookName = ThisWorkbook.Name

Open "\\SHARED\Public Folder\ Switch Off\Log.txt" For Append As #1

Print #1, Format(Now, "dd/mm/yyyy hh:mm:ss"); ","; Environ("username"); ","; sCurrentWorkbookName; ","

Close #1

Else

Exit Sub

End If

Application.ScreenUpdating = True

End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
You can test whether your code can see the file path as follows:-

VBA Code:
On Error Resume Next
Debug.Print Dir(sFolderPath, vbDirectory) ' just do something/anything with the file path
If Err.Number = 0 Then
    MsgBox "Found!" ' whatever you need to do if the file path exists
Else
    MsgBox "Not found!" ' whatever you need to do if the file path doesn't exist
End If
On Error GoTo 0
 
Upvote 0
Hi,
How about this.
Code:
Dim fs as object
fs = CreateObject("Scripting.FileSystemObject")
If fs.FolderExists(sFolderPath)=False than exit sub
 
Upvote 0

Forum statistics

Threads
1,214,897
Messages
6,122,141
Members
449,066
Latest member
Andyg666

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