afe*_*wcc 20 excel sharepoint vba
通常我使用这段代码来检索VBA中文件夹的内容.但这不适用于sharepoint的情况.我能怎么做 ?
Dim folder As folder
Dim f As File
Dim fs As New FileSystemObject
Set folder = fs.GetFolder("//sharepoint.address/path/to/folder")
For Each f In folder.Files
'Do something
Next f
Run Code Online (Sandbox Code Playgroud)
编辑(经过shahkalpesh的好评):
如果我在Windows资源管理器中输入地址,我可以访问sharepoint.访问sharepoint需要进行身份验证,但它是透明的,因为它依赖于Windows登录.
Chr*_*yes 14
我发现在拥有服务器权限时使用SharePoint上的文件的唯一方法是将WebDAV文件夹映射到驱动器号.这是实现的一个例子.
在VBA中添加对以下ActiveX库的引用:
wshom.ocx) - 用于WshNetworkscrrun.dll) - 用于FileSystemObject创建一个新的类模块,调用它DriveMapper并添加以下代码:
Option Explicit
Private oMappedDrive As Scripting.Drive
Private oFSO As New Scripting.FileSystemObject
Private oNetwork As New WshNetwork
Private Sub Class_Terminate()
UnmapDrive
End Sub
Public Function MapDrive(NetworkPath As String) As Scripting.Folder
Dim DriveLetter As String, i As Integer
UnmapDrive
For i = Asc("Z") To Asc("A") Step -1
DriveLetter = Chr(i)
If Not oFSO.DriveExists(DriveLetter) Then
oNetwork.MapNetworkDrive DriveLetter & ":", NetworkPath
Set oMappedDrive = oFSO.GetDrive(DriveLetter)
Set MapDrive = oMappedDrive.RootFolder
Exit For
End If
Next i
End Function
Private Sub UnmapDrive()
If Not oMappedDrive Is Nothing Then
If oMappedDrive.IsReady Then
oNetwork.RemoveNetworkDrive oMappedDrive.DriveLetter & ":"
End If
Set oMappedDrive = Nothing
End If
End Sub
Run Code Online (Sandbox Code Playgroud)
然后你可以在你的代码中实现它:
Sub test()
Dim dm As New DriveMapper
Dim sharepointFolder As Scripting.Folder
Set sharepointFolder = dm.MapDrive("http://your/sharepoint/path")
Debug.Print sharepointFolder.Path
End Sub
Run Code Online (Sandbox Code Playgroud)
Chr*_*tta 13
使用UNC路径而不是HTTP.此代码有效:
Public Sub ListFiles()
Dim folder As folder
Dim f As File
Dim fs As New FileSystemObject
Dim RowCtr As Integer
RowCtr = 1
Set folder = fs.GetFolder("\\SharePointServer\Path\MorePath\DocumentLibrary\Folder")
For Each f In folder.Files
Cells(RowCtr, 1).Value = f.Name
RowCtr = RowCtr + 1
Next f
End Sub
Run Code Online (Sandbox Code Playgroud)
要获取要使用的UNC路径,请进入文档库中的文件夹,下拉"操作"菜单,然后选择"在Windows资源管理器中打开".复制您在那里看到的路径并使用它.
小智 10
此外:
myFilePath = replace(myFilePath, "/", "\")
myFilePath = replace(myFilePath, "http:", "")
Run Code Online (Sandbox Code Playgroud)
也取代空间:
myFilePath = replace(myFilePath, " ", "%20")
Run Code Online (Sandbox Code Playgroud)