• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Open Latest File from SharePoint|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Open Latest File from SharePoint|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosOpen Latest File from SharePoint
sp_PrintTopic sp_TopicIcon
Open Latest File from SharePoint
Avatar
Matthias
Member
Members
Level 0
Forum Posts: 48
Member Since:
December 13, 2017
sp_UserOfflineSmall Offline
1
May 18, 2021 - 11:55 am
sp_Permalink sp_Print

Hi, I use the below code, to open the latest file from a folder or subfolder. Works great, easy to adapt to different folder situations and file name components.

But the MyFile step throws an error if I want to use it for a SharePoint folder. [Bad file name or number]

Google gave me two possible reasons in regard to the path: "/" and "https:"
I can change one or both of them, but it does not change the error on the MyFile step. So I commented it out here.

Where is my mistake? Is there any other adaptation necessary? Or is there an alternative approach available? Thanks!

'find & open latest XYZ File
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date

MyPath = "https://company.sharepoint.com/blah/abcd/Team Documents/whatever/details/2021/"
'MyPath = Replace(MyPath, "/", "\")        'adaptation attempt for SharePoint folder
'MyPath = Replace(MyPath, "https:", "")  'adaptation attempt for SharePoint folder
MyFile = Dir(MyPath & "XYZ *.xlsm")

If Len(MyFile) = 0 Then
MsgBox "No XYZ files were found...", vbExclamation
Exit Sub
End If

Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop

Workbooks.Open MyPath & LatestFile

sp_AnswersTopicSeeAnswer See Answer
Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1810
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
2
May 23, 2021 - 3:12 pm
sp_Permalink sp_Print sp_EditHistory

Hi Matthias,

Dir works on local file system. An online sharepoint folder is not local, it's online (obviously).

From web, you have to download the file.

Easiest way is to go online to that folder, and sync it to local computer, you should find a Sync command in the menu for this. Then you can use dir to look into the location on your computer where the sync'd folder is. (best option)

To download from web, search the web for Function URLDownloadToFile, you will find what you need.

Or, you can map the SP web folder with your local computer, see image attached.

1-6.jpgImage Enlarger

sp_PlupAttachments Attachments
  • sp_PlupImage 1-6.jpg (112 KB)
sp_AnswersTopicAnswer
Answers Post
Avatar
Matthias
Member
Members
Level 0
Forum Posts: 48
Member Since:
December 13, 2017
sp_UserOfflineSmall Offline
3
May 24, 2021 - 7:17 am
sp_Permalink sp_Print

Hi Catalin,

thanks for the answer, that makes as always a lot of sense!

What would be the benefit of Sync (going through OneDrive) against mapping?

Thanks again,
Matthias

Avatar
Anders Sehlstedt
Eskilstuna, Sweden

VIP
Members


Trusted Members
Level 3
Forum Posts: 871
Member Since:
December 7, 2016
sp_UserOfflineSmall Offline
4
May 24, 2021 - 7:49 pm
sp_Permalink sp_Print

Hello,

With synced folder you still have the data available even when not online, with mapped folder you need to be online to get the data.

Br,
Anders

Avatar
Matthias
Member
Members
Level 0
Forum Posts: 48
Member Since:
December 13, 2017
sp_UserOfflineSmall Offline
5
May 25, 2021 - 1:01 am
sp_Permalink sp_Print

Hello Anders,

Thanks for the answer. Generally that is an advantage. I did not perceive it as advantage as I normally work online and if I "Open Latest File from SharePoint" I need to be online to be sure that I get the latest file.
[=> I'd have to spent quite some disk space on my local machine for data which is most likely not up to date.]

Thanks,
Matthias

Avatar
Matthias
Member
Members
Level 0
Forum Posts: 48
Member Since:
December 13, 2017
sp_UserOfflineSmall Offline
6
May 25, 2021 - 1:02 am
sp_Permalink sp_Print sp_EditHistory

Hello Catalin,

mapping the sharepoint showed me that adding @SSL was the missing step:

MyPath = Replace(MyPath, "https:", "")
MyPath = Replace(MyPath, "/", "\")
MyPath = Replace(MyPath, Split(MyPath, "\")(2), Split(MyPath, "\")(2) & "@SSL")

With this it just works. Thanks for the hint!

Regards,
Matthias

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Brian Pham
Guest(s) 9
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 871
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
Jessica Stewart: 205
A.Maurizio: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
stuart burge
Bruce Tang Nian
Scot C
Othman AL MUTAIRI
Misael Gutierrez Sr.
Attif Ihsan
Kieran Fee
Murat Hasanoglu
Brett Dryland
Saeed Aldousari
Forum Stats:
Groups: 3
Forums: 24
Topics: 6223
Posts: 27295

 

Member Stats:
Guest Posters: 49
Members: 31920
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.