Check if a File exist in shared Folder without opening the file

drom

Well-known Member
Joined
Mar 20, 2005
Messages
540
Office Version
  1. 2021
  2. 2019
  3. 2016
  4. 2013
  5. 2011
  6. 2010
  7. 2007
Hi and thanks in advance!!

I have this Macro:
VBA Code:
Sub Macro1()
  On Error Resume Next
Dim wCell As String:        wCell = ActiveCell
  ThisWorkbook.FollowHyperlink (wCell)
  If Err <> 0 Then
    Debug.Print Err, Err.Description
  End If
End Sub

Is any way of knowing if I am gonna get err <> 0 without using:
ThisWorkbook.FollowHyperlink (wCell)
Sometimes the Link is gonna be in a Shared folder and the File Address is gonna be something like:
Invoices%20VISADAS%202024/Week%2021/OLIVAR%20EL%20frfrss-%2024A001894%20ABC.pdf

So I Would like to know if I can Check if I am gonna get the Err Message without opening the file

So If the File exists (No ERR) Good but but:

Otherwise Get the Message



Sometimes the File are gonna be on a PC like:
  • D:\ExcelFiless\aaaaaaaaaaaaaaaaPendingJOBS\20ABC.PDF
  • X:\File___0001abc.PDF
  • E:\Filess\AAA___0001abc.PDF


PS:
I fthe file is in a PC, I know how to act (D: X: E: etc...)
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
I use this as a Function just because I like the way it reads as part of an if statement:
VBA Code:
Function file_exists(filename As String) As Boolean
    file_exists = Dir(filename) <> ""
    End Function

Add that somewhere, then your code might look like this:

VBA Code:
Sub Macro1()
Dim wCell As String:        wCell = ActiveCell
If file_exists(wcell) Then
  ThisWorkbook.FollowHyperlink (wCell)
  End If
End Sub

Or you can avoid that function and just do the test directly:

VBA Code:
Sub Macro1()
Dim wCell As String:        wCell = ActiveCell
If Dir(wcell) <> "" Then
  ThisWorkbook.FollowHyperlink (wCell)
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,481
Messages
6,160,080
Members
451,616
Latest member
swgrinder

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