-
-
[VBA]Dim f as String
f = "I:\BDMSFA\BDMdatafiles\" & x & "activity.xls"
If Dir(f)="" Then
MsgBox f & " does not exist."
Else
MsgBox f & " exists."
End If[/VBA]
-
Could I search the users computer to see if the share is mapped and if it is use that drive in the reference?
Kind of like
for x = A to Z
if true drive=x
reference file as "drive:\BDMSFA\BDMdatafiles\" & x & "activity.xls""
UUUGGGLLYYY I know!
-
If you have WScript installed and enabled, scrrun.dll:
[VBA]Sub ShowNetworkDrives()
Dim d As Object
Dim msg As String
Dim dDriveType As String
For Each d In CreateObject("Scripting.FileSystemObject").Drives
dDriveType = d.DriveType
'3=Newtwork Drive, 6=Network CDROM with CDFS file system
If d.IsReady = True And (dDriveType = 3 Or dDriveType = 6) Then
msg = msg & d.driveletter & vbNewLine
End If
Next
If msg = "" Then msg = "No network drive found."
MsgBox msg
End Sub[/VBA]
-
-
So this should work when referencing files on this share regardless of the letter the user has it mapped to?
[vba]Public Sub NetworkMapDrive()
Dim WshNetwork As Object
Dim oDrives As Object
Dim searchdrive As String
Dim userdrive As String
Dim i As Long
searchdrive = "\\apwfs01\ho_region$"
Set WshNetwork = CreateObject("WScript.Network")
Set oDrives = WshNetwork.EnumNetworkDrives
For i = 0 To oDrives.Count - 1 Step 2
If (oDrives.Item(i + 1) = searchdrive) Then
userdrive = oDrives.Item(i)
End If
Next
MsgBox "You have the drive mapped! to " & oDrives.Item(i) & Chr(13) & userdrive
'ChDir userdrive & "\BDMSFA\BDMdatafiles\backups"
'Workbooks.Open Filename:=userdrive & "\BDMSFA\BDMdatafiles\backups\1activity.xls"
End Sub[/vba]
Is this the correct way to reference these files or is there a better way?
Thanks again all.....
-
Run it to see if it works. If it does, you will want to remove the MsbBox part.
You could just use the UNC paths without a drive letter. I like drive letters though. Of course some functions don't work with UNC paths like DIR().
I typically use DIR() to determine if a file or folder exists. Here is a method to check for a UNC file's existence.
[vba]
Sub Test()
Dim f As String, rc As Boolean
f = "\\matpc10\excelvbaexamples\_vba express\txt\txt.xls"
rc = FileExists(f)
Debug.Print f, rc
If rc Then Workbooks.Open f
f = ThisWorkbook.FullName
Debug.Print f, FileExists(f)
End Sub
Function FileExists(sFilename As String) As Boolean
Dim fso As Object, tf As Boolean
Set fso = CreateObject("Scripting.FileSystemObject")
tf = fso.FileExists(sFilename)
Set fso = Nothing
FileExists = tf
End Function
[/vba]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules