+ Reply to Thread
Results 1 to 17 of 17

If file Exists on Sharepoint ... how to in VBA?

  1. #1
    Registered User
    Join Date
    09-09-2021
    Location
    London, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    56

    If file Exists on Sharepoint ... how to in VBA?

    Hello,
    My question is how to determine if a file exists in sharepoint. If it exists then exit the sub if it does not exist then save the file.
    I have a button on the spreadsheet. If you press it, it will save the workbook if the file does not exist and exit the sub if the file already exists.

    The code below does not work at all. I get "Run-time error '52': bad filename or number"

    In cell AL3 on the AMMobile sheet is "CA-VTS-MobileClear20220524"

    Please Login or Register  to view this content.
    Last edited by GrayWolf; 05-25-2022 at 12:19 PM.

  2. #2
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,377

    Re: If file Exists on Sharepoint ... how to?

    You can't use Dir for sharepointfolders.
    Try to sync your sharepoint folder to your explorer. Then you can.

    Another option is Power Query

  3. #3
    Registered User
    Join Date
    09-09-2021
    Location
    London, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    56

    Re: If file Exists on Sharepoint ... how to in VBA?

    Hello @JEC. and thank you for the suggestion. Sharepoint is currently synched on my machine, but the users who will be using this workbook will not have it synched so I am looking for a solution through VBA to just check for the file before it continues with the SUB.

    Gray.

  4. #4
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,377

    Re: If file Exists on Sharepoint ... how to in VBA?

    The users should sync it too. After they did, it's getting easy.

  5. #5
    Forum Contributor
    Join Date
    02-25-2022
    Location
    Dallas, Texas
    MS-Off Ver
    Office 365
    Posts
    174

    Re: If file Exists on Sharepoint ... how to in VBA?

    I had to get a lot of data from Sharepoint into Excel. I ended up using C# and CSOM (Client Side Object Model). I created desktop command line program that would get data and put in a database. Excel would read db. You could easily create a simple C# command line program, call it from VBA and test if the file is in Sharepoint. Google/Bing C# CSOM.

  6. #6
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2402-17328.20068
    Posts
    1,360

    Re: If file Exists on Sharepoint ... how to in VBA?

    Maybe you could just open it and handle the errors?
    Please Login or Register  to view this content.
    Last edited by CheeseSandwich; 05-25-2022 at 12:44 PM. Reason: Edited the extra .xlsm from code
    If things don't change they stay the same

  7. #7
    Registered User
    Join Date
    09-09-2021
    Location
    London, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    56

    Re: If file Exists on Sharepoint ... how to in VBA?

    Hi Cheese,

    Thank you for your response.
    This method intrigues me.
    I have been trying for over a week trying to get this to work. Considering my code creates a new file every day it is used I thought that checking if the file already exists would be a very simple line of code. Apparently not.

    I tried your test code a few different times and regardless if the filename exists in my sharepoint or not, I get "MsgBox "File not found".
    I appreciate your response, thank you. I'll keep googling.

    Gray

  8. #8
    Registered User
    Join Date
    09-09-2021
    Location
    London, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    56

    Re: If file Exists on Sharepoint ... how to in VBA?

    Quote Originally Posted by mogul View Post
    I had to get a lot of data from Sharepoint into Excel. I ended up using C# and CSOM (Client Side Object Model). I created desktop command line program that would get data and put in a database. Excel would read db. You could easily create a simple C# command line program, call it from VBA and test if the file is in Sharepoint. Google/Bing C# CSOM.
    Hi Mogul,
    Thank you for the response.
    I was really looking for a simpler solution. I literally just need to see if the a file name exists on sharepoint if it does quit the sub, if not save the file.
    There are a number of issues with this solution for me.
    1. I don't know C# at all
    2. The IT department is loathe to give me or anyone admin rights on the PC's here so this will be quite difficult to create. We can't even open the command prompt. Haha. heck at one point they locked out the "task manager".

    Anyway, I appreciate the out of the box thinking on this.
    Gray

  9. #9
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2402-17328.20068
    Posts
    1,360

    Re: If file Exists on Sharepoint ... how to in VBA?

    If you run the below:
    Please Login or Register  to view this content.
    Can you open the file by pasting the text produced in the immediate widow into a web browser?

    Are you currently logged into SharePoint?

  10. #10
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2402-17328.20068
    Posts
    1,360

    Re: If file Exists on Sharepoint ... how to in VBA?

    Just noticed you have two lots of '.xlsm'

    One on 'bmvarFilename'
    and another on the end of:
    "https://lehub.sharepoint.com/sites/CA-VTS-Ontario-Schools-London/Shared Documents/CA-VTS-MobileClear/CA-VTS-2021-2022-MobileClear/" & bmvarFilename & ".xlsm"

    This will make the file name end with '.xlsm.xlsm'

    Maybe correct that and retry the code.

  11. #11
    Registered User
    Join Date
    09-09-2021
    Location
    London, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    56

    Re: If file Exists on Sharepoint ... how to in VBA?

    Hi CheeseSandwich,

    I am logged into sharepoint at the moment.
    Debug.Print was one of the first things I tried in my tests. I did a debug print and pasted it into a browser. If the file existed it does not open it, just downloaded it right away. If it did not exist I got a " This Site Can't be reached" error.
    Gray

  12. #12
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2402-17328.20068
    Posts
    1,360

    Re: If file Exists on Sharepoint ... how to in VBA?

    How about the issue of having two '.xlsm' in the file name?

    Having this:
    Please Login or Register  to view this content.
    And then this:
    Please Login or Register  to view this content.
    Will result in:
    FileName.xlsm.xlsm
    Last edited by CheeseSandwich; 05-25-2022 at 09:19 AM.

  13. #13
    Registered User
    Join Date
    09-09-2021
    Location
    London, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    56

    Re: If file Exists on Sharepoint ... how to in VBA?

    Quote Originally Posted by CheeseSandwich View Post
    Just noticed you have two lots of '.xlsm'

    One on 'bmvarFilename'
    and another on the end of:
    "https://lehub.sharepoint.com/sites/CA-VTS-Ontario-Schools-London/Shared Documents/CA-VTS-MobileClear/CA-VTS-2021-2022-MobileClear/" & bmvarFilename & ".xlsm"

    This will make the file name end with '.xlsm.xlsm'

    Maybe correct that and retry the code.
    OK this got it .. thank you .. not sure why I did not see that in the first place.

    Now I am thinking, is there something else I can try instead of " ... Workbooks.Open"?
    Systems are very slow here and that .open .close took almost 90 seconds ... ha ha ha . this place is hilarious.

    If you can't think of anything else. no worries. I'll resolve this. I have a pop up that tells then to 'WAIT UNTIL THIS IS FINISHED', anyway, so not a huge deal.
    I've Rep'd you.
    Thank you so much.
    Gray

  14. #14
    Registered User
    Join Date
    09-09-2021
    Location
    London, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    56

    Re: If file Exists on Sharepoint ... how to in VBA?

    Are you able to comment this to explain what exactly is goiung on .. i've not had any experience with On Error at all.

    Please Login or Register  to view this content.

  15. #15
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2402-17328.20068
    Posts
    1,360

    Re: If file Exists on Sharepoint ... how to in VBA?

    Please Login or Register  to view this content.
    Hope this helps
    Last edited by CheeseSandwich; 05-25-2022 at 10:50 AM.

  16. #16
    Registered User
    Join Date
    09-09-2021
    Location
    London, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    56

    Re: If file Exists on Sharepoint ... how to in VBA?

    Amazing. thank you very much.

    Gray

  17. #17
    Registered User
    Join Date
    02-11-2020
    Location
    La Verne, CA
    MS-Off Ver
    Office 365
    Posts
    1

    Re: If file Exists on Sharepoint ... how to in VBA?

    Thank you for this. I've been racking my brain for a week now trying to figure this out.
    I need to loop through files on sharepoint and update a pivot only if the file existed (and no, syncing with explorer was not an option).

    It's not the cleanest, but here's where I'm at

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA To Check File Exists In Sharepoint
    By OzTrekker in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 10-11-2023, 09:37 PM
  2. Check if a file exists in onedrive/sharepoint
    By S@S in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-15-2022, 08:15 AM
  3. HOW CHECK FILE.NAME HAS EXISTS OR NOT IN FOLDER,if not exists then copy
    By daboho in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-13-2018, 03:50 AM
  4. VBA to select SharePoint Content Type when saving Excel file to SharePoint
    By Luffk73 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-24-2017, 05:22 PM
  5. Replies: 0
    Last Post: 10-08-2013, 10:54 AM
  6. VBA code to Check if a Folder exists in SharePoint
    By shavazo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-01-2013, 07:46 AM
  7. Check if file exists on SharePoint https environment
    By Purpose in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-16-2013, 04:17 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1