Path not found when using a URL

WaterGypsy

Well-known Member
Joined
Jan 15, 2010
Messages
700
Hi everyone

I am trying to access files on our sharepoint system so that I can update a dashboard showing when they were last run.

I can open a file held on sharepoint if I specify the full path and name, but if I try to access the folder only, so that I can list the files stored in that folder I get "Path not found".

I have tried objFSO.GetFolder and even simply ChDir (which works when I'm recording macro but not when I run that macro afterwards).

I don't even know where to look any more :-((

Thanks in advance for any assistance

Kevin

Forgot to say that the sharepoint address starts http:..... (obviously?)
 
Last edited:
Walter, I read somewhere else that your assumption is indeed correct. As of yet I have not learned how to do this but I believe there is a way.
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hi Mike,

Six years on, did you ever find a way of listing the file names within a SharePoint document library? I seem to have reach the same impasse you and Walter reached back in 2010. Any ideas?
 
Upvote 0
Hi, Mike!

I don't even remember this post to be honest. However I have done it. Let me clean up a sample code and I'll post it. So to make sure I understand the question, we want to list files found on a SharePoint site, right?
 
Last edited:
Upvote 0
Absolutely. Thanks. Not surprised you can remember something from six years ago, I can barely remember last week. ;-)

Hi, Mike!

I don't even remember this post to be honest. However I have done it. Let me clean up a sample code and I'll post it. So to make sure I understand the question, we want to list files found on a SharePoint site, right?
 
Upvote 0
Here is the code I use. Let me know of any questions you have.
Code:
Public ifile
Option Base 1
Public afiles()
 

Sub GetFileList()
Dim IsOpen As Integer
Dim TheFile As String
Dim Ffp As String
Dim CDir As String
Let CDir = CurDir
Dim FileDir As String
Dim Ans As Variant
Dim DataArray(150, 4) As Variant
Dim TabToRefresh
Let TabToRefresh = ActiveSheet.Name
Beep
Let Ans = MsgBox(Prompt:="Are you sure you wish to repopulate this file?", Title:="FLIR Systems, Inc.", Buttons:=vbOKCancel)
If Ans <> 1 Then Exit Sub
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Cursor = xlWait
With Application
    .Calculation = xlManual
    .MaxChange = 0.001
End With
ifile = 0
'clear out the last population of data
For X = 2 To 100
    
    For Y = 1 To 4
        DataArray(X, Y) = Cells(X, Y).Value
        Cells(X, Y).Value = Empty
    Next
Next
'the value of "thepath" should be a SharePoint site in this format:
'\\globalstd@SSL\DavWWWRoot\team\icxcorp\acctg\GL Closing\Detection\201609 Sep\Account Recons\

Let FileDir = Range("thepath")
 

ListFilesInDirectory FileDir, 0
 

'this places the file names in column B of the current sheet
For IsOpen = 1 To ifile
    Cells(1 + IsOpen, 2).Value = afiles(IsOpen)
Next
Call DoHyper
 
 

Sheets(TabToRefresh).Select
    Range("B2:E" & ifile + 1).Select
    ActiveWorkbook.Worksheets(TabToRefresh).Sort.SortFields.Clear
    ActiveWorkbook.Worksheets(TabToRefresh).Sort.SortFields.Add Key:=Range("B2:B" & ifile + 1) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets(TabToRefresh).Sort
        .SetRange Range("B1:E" & ifile + 1)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Cursor = xlDefault
With Application
    .Calculation = xlAutomatic
    .MaxChange = 0.001
End With
End Sub
Sub DoHyper()
Dim X As Long
Dim TheFileOnly As String
Let X = 2
Do While True
        If Cells(X, 2).Value = Empty Then Exit Do
        Let Filename = Cells(X, 2).Value
        'Cells(X, 2).FormulaR1C1 = "=HYPERLINK(" & Filename & ")"
        Cells(X, 2).Select
        Let TheFileOnly = FileFromPath(Filename)
        ActiveCell.Hyperlinks.Add ActiveCell, Filename, TextToDisplay:=TheFileOnly
        X = X + 1
Loop
End Sub

Private Sub ListFilesInDirectory(Directory As String, EraseIt As Integer)
'This is called by the list all files function above.
  Dim X As Integer, Y As Integer
  Dim StartRow As Integer
  Dim aDirs() As String, iDir As Integer, stFile As String
Dim SubName As String
Dim Goback As Integer
Dim Counter As Integer
Let SubName = "ListInDir"
On Error GoTo handleCancelListInDir
  ' use Dir function to find files and directories in Directory
  ' look for directories and build a separate array of them
  ' note that Dir returns files as well as directories when vbDirectory
  ' specified
  
If EraseIt = 1 Then
    Application.GoTo Reference:="PathToRename"
    X = ActiveCell.Row + 1
    Y = ActiveCell.Column + 1
    Cells(X, Y).Select
    
    X = ActiveCell.Row
End If
  
  
  iDir = 0
  stFile = Directory & Dir(Directory & "*.*", vbDirectory)
  Do While stFile <> Directory
    If Right(stFile, 2) = "\." Or Right(stFile, 3) = "\.." Then
      ' do nothing - GetAttr doesn't like these directories
    ElseIf GetAttr(stFile) = vbDirectory Then
      ' add to local array of directories
      iDir = iDir + 1
      ReDim Preserve aDirs(iDir)
      aDirs(iDir) = stFile
    Else
      ifile = ifile + 1
      ReDim Preserve afiles(ifile)
      afiles(ifile) = stFile
    End If
    stFile = Directory & Dir()
 Loop
  If iDir > 0 Then
    For iDir = 1 To UBound(aDirs)
      ListFilesInDirectory aDirs(iDir) & Application.PathSeparator, 0
    Next iDir
  End If
If EraseIt = 1 Then
    StartRow = X
        For Y = 1 To ifile
            Cells(X, 1).Value = afiles(Y)
            X = X + 1
        Next
        Cells(StartRow, 1).Select
End If
Exit Sub

handleCancelListInDir:
Let Counter = Counter + 1
If Counter > 50 Then
    If Err = 18 Then
        Let Goback = MsgBox(Prompt:="You interrupted the program by hitting the Escape key.  The system will return to the point where you caused this intervention.  Thank you.  (Note, if you wish to stop the program click Cancel instead of OK)", Title:="FLIR Systems User Intervention by " & UsrID, Buttons:=vbYesNoCancel + vbCritical)
    Else
        Let Goback = MsgBox(Prompt:="BE SURE TO REFRESH THE B:\ DRIVE!!!!  In Sub " & SubName & ", there is an Error (" & Err.Number & ") of " & Err.Description & ".  The system will return to the point where this error was caused.  Thank you.", Title:="FLIR User System Error", Buttons:=vbOKCancel + vbCritical)
    End If
    DetailsCntr = DetailsCntr + 1
    Goback = 1
    If Goback = 1 Then   'Selected OK
        Resume
    ElseIf Goback = 2 Then 'Selected Cancel
        Exit Sub
    ElseIf Goback = 6 Then 'Selected Yes
        Resume
    ElseIf Goback = 7 Then 'Selected NO
        Resume Next
    End If
Else
    Resume
End If
End Sub
Public Function FileFromPath(ByVal strFullPath As String, Optional bExtensionOff As Boolean = False) As String
'this function parses file names from a full path and file and extension text.
  Dim FPL As Long        'len of full path
  Dim PLS As Long        'position of last slash
  Dim pd As Long        'position of dot before exension
  Dim strFile As String
  On Error GoTo ERROROUT
  FPL = Len(strFullPath)
  PLS = InStrRev(strFullPath, "\", , vbBinaryCompare)
  strFile = Right$(strFullPath, FPL - PLS)
  If bExtensionOff = False Then
    FileFromPath = strFile
  Else
    pd = InStr(1, strFile, ".", vbBinaryCompare)
    FileFromPath = Left$(strFile, pd - 1)
  End If
  Exit Function
ERROROUT:
  On Error GoTo 0
  FileFromPath = ""
End Function
 
Upvote 0
Hi.

Thanks for the code but unfortunately it hangs on the handleCancelListInDir error handler. When I comment this out I get the following error message 'Run-time error '52': Bad file name or number' at the following line in the ListFilesInDirectory routine:

stFile = Directory & Dir(Directory & "*.*", vbDirectory)

Just to clarify my needs. I want to store a number of Excel workbooks in a document library and then using VBA I originally wanted to allow the user to select these files through a normal Application.FileDialog window. I can do this no problem if the files are stored on a normal file server but I can't select more than one if they are stored on SharePoint.

My alternative approach was to use VBA to get a list of all the files in the document library and filter these down to the relevant ones for the user to select.

At the moment I keep getting stuck at the stage of identifying the files names in the document library.

Your code looks promising but can you think why it is falling over?

Many thanks,

Mike
 
Upvote 0
Sorry guys ... I am a bit late catching up with this thread (actually thought I had replied yesterday) ....

I finally got this working to build my list of files in a SharePoint folder

It is checking for files starting with "Cnnnn" and ending " - TOC.xlsx" and then putting the filename in column A

Code:
iDL = iDL1
sTOCFolder = "//sharepoint.companyinternal.com/sites/XYZ/Shared Documents/Project Documentation/Active Projects"
Set folder = fs.GetFolder(sTOCFolder)
For Each f In folder.Files
    
    If Right(f.Name, 11) = " - TOC.xlsx" _
    And Left(f.Name, 1) = "C" _
    And IsNumeric(Mid(f.Name, 2, 4)) Then
        iDL = iDL + 1
        Cells(iDL, 1).Value = f.Name
    Else
        sLOG = sLOG & Time & "Unexpected file found : " & f.Name & Chr(10)
    End If
Next f
If iDL <= iDL1 Then
    MsgBox "No valid TOC files found"
    Exit Sub
End If

It doesn't appear as sophisticated as Mike's but it did the job for me

kevin
 
Upvote 0

Forum statistics

Threads
1,225,151
Messages
6,183,197
Members
453,151
Latest member
Lizamaison

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