Opening the first (and only the first) file present in a folder with VBA

juancarlos1

New Member
Joined
Sep 22, 2021
Messages
8
Office Version
  1. 365
Hello!

There is the need to open the first and only the first Excel file present in a specific folder.
I am having some trouble using the Dir function with a relative reference.
This code is working when the full path is there placed, but the reference must be relative:

VBA Code:
Dim FileName As String
FileName = VBA.FileSystem.Dir(ThisWorkbook.Path & "\Export files to process\*.xlsx")
If FileName = VBA.Constants.vbNullString Then
MsgBox "No file found in the folder"
Else
Workbooks.Open ThisWorkbook.Path & "\Export files to process\" & FileName
End If

Bad file name or number is the error returned.
Can anyone advise here?

Regards!
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Relative to which folder? Describe exactly how the folder containing the required file is relative to the 'start' folder.

As written, ThisWorkbook.Path & "\Export files to process\, the "Export files to process" folder is a subfolder of the ThisWorkbook.Path folder.
 
Upvote 0
Relative to which folder? Describe exactly how the folder containing the required file is relative to the 'start' folder.

As written, ThisWorkbook.Path & "\Export files to process\, the "Export files to process" folder is a subfolder of the ThisWorkbook.Path folder.
Hi @John_w , The Macro-enabled tool is stored in a folder on my desktop. Inside this folder, aside from this file, there is another folder, where the export (.xlsx) files to be worked on are stored.
The desktop is connected to SharePoint, and this is the problem. Meanwhile, I have found that my code works if the tool is saved on the computer's disk.

ThisWorkbook.Path if the tool is in Sharepoint: https://mydrive......
ThisWorkbook.Path if the tool is in the computer's disk: C:\Users\......

Do you happen to know if there is a way of making the code work in Sharepoint folders?
Regards!
 
Upvote 0
Open the sharepointfolder with the Windows explorer, right click and select properties of folder. You will see the correct path there.
Should be something like this: \\plgabw-portal.itsysbw.cir@SSL\DavWWWRoot\persart\PR_intern.
 

Attachments

  • 1637249250814.png
    1637249250814.png
    19 KB · Views: 11
Upvote 0
Open the sharepointfolder with the Windows explorer, right click and select properties of folder. You will see the correct path there.
Should be something like this: \\plgabw-portal.itsysbw.cir@SSL\DavWWWRoot\persart\PR_intern.
Thank you for the tip! :)
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,317
Members
449,081
Latest member
tanurai

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