Paul Nema
Paul Nema

Reputation: 61

Excel VBA: how to check if a file exists using Administrative share path

I’m trying to create a deployment tool using excel and VBA. The spreadsheet contains an Administrative share path i.e. (\\G5RCJ55\C$\users\public\desktop\file.txt) I have administrative rights. I pull the path using VBA and tried using dir() but that fails.

Any suggestions on how I can:
1. Check if a file exists
2. If not, copy a file to the target machine/directory (The public desktop on the C: Drive).

enter image description here

Sub deployToDesktop(cnt As Integer)
   Dim fullPath As String: fullPath = Cells(cnt, "D").value & "\" & Cells(cnt, "E").value

   MsgBox ("Admin path " & fullPath)

   If Not Dir(fullPath) Then
      MsgBox (fullPath)
   End If
End Sub

Upvotes: 0

Views: 318

Answers (1)

Paul Nema
Paul Nema

Reputation: 61

OK Found an answer. I need to use the FileSystemObject. This understands UNC path files. Here is the updated code:

Sub deployToDesktop(cnt As Integer)
   Dim fso As New FileSystemObject

   Dim fullPath As String: fullPath = Cells(cnt, "D").value & "\"

   If fso.FolderExists(fullPath) Then
      MsgBox ("Exists " & fullPath)
   End If
End Sub

Upvotes: 2

Related Questions