1
votes

I'm using VBA in Excel to loop through files on a sharepoint site and open all Excel files.

The code crashes Excel the first time I run it, however, if I then reopen it it works fine.

Are there any known issues around this?

Thanks.

Edit: Here is the code:

Sub Refresh()

    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False

        Dim fso As FileSystemObject
        Dim fldr As Folder
        Dim f As File
        Dim wb As Workbook

        Set fso = New FileSystemObject
        Set fldr = fso.GetFolder(SharePointSite)

        For Each f In fldr.Files

            Set wb = Workbooks.Open(SharePointURL & f.Name)

        Next f

        Set wb = Nothing
        Set fldr = Nothing
        Set fso = Nothing

        .DisplayAlerts = True
        .ScreenUpdating = True
    End With

End Sub
1
1. Does it crash with an error? If so - what kind of error? 2. Could you post your code please? Have you put any defence coding?Juliusz
Thanks for the reply. No error comes up, Excel just crashes. I've put the code in the original post. Cheers.user1423997
oh, could you let us know versions of Excel and SharePoint? I believe there were some issues with compability between Excel 2003 and latest version of SharePoint.Juliusz
Have you tried mapping sharepoint to a drive letter and using that reference instead of the URL?Alistair Weir
Just tried this and it solved the issue. Thanks!user1423997

1 Answers

2
votes

Instead of mapping the document library to a drive letter try using the WebDAV address to access the library in your code. This way if the macro is distributed no one will be dependent upon having the "Z:" drive being mapped to a specific location

Set your FilePath variable equal to a string like this (use @SSL for HTTPS sites):

\\sharepoint.site.com@SSL\DavWWWRoot\site1\usersite\Book2\Shared%20Documents

If you are going to access the text file directly then set it up like this:

\\sharepoint.site.com@SSL\DavWWWRoot\site1\usersite\Book2\Shared%20Documents \Test_Text1.txt

Take a look at this blog post for a full explanation on retrieving the WebDAV path.