使用Excel VBA获取SharePoint文件夹的内容

通常我使用这段代码来检索VBA中文件夹的内容。 但是在共享点的情况下这是行不通的。 我能怎么做 ?

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 

编辑 (在shahkalpesh好评后):

如果我在Windows资源pipe理器中input地址,则可以访问该共享点。 访问共享点需要authentication,但它是透明的,因为它依赖于Windowslogin。

我发现在SharePoint上处理文件的唯一方法就是将WebDAV文件夹映射到一个驱动器盘符。 这是一个实现的例子。

在VBA中添加对以下ActiveX库的引用:

  • Windows脚本主机对象模型( wshom.ocx ) – 用于WshNetwork
  • Microsoft脚本运行时( scrrun.dll ) – 用于FileSystemObject

创build一个新的类模块,将其称为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 

那么你可以在你的代码中实现它:

 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 

此外:

 myFilePath = replace(myFilePath, "/", "\") myFilePath = replace(myFilePath, "http:", "") 

也取代空间:

 myFilePath = replace(myFilePath, " ", "%20") 

使用UNCpath而不是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 

要获取要使用的UNCpath,请进入文档库中的文件夹,下拉操作菜单,然后select在Windows资源pipe理器中打开。 复制你在那里看到的path并使用它。

恕我直言,最酷的方式是通过WebDAV(没有networking文件夹,因为这是经常不允许的)。 这可以通过ActiveX Data Objects来完成,就像这篇出色的文章中所阐述的那样(代码可以直接在Excel中使用,最近使用这个概念)。

希望这可以帮助!

http://blog.itwarlocks.com/2009/04/28/accessing-webdav-in-microsoft-word-visual-basic/

原始链接已经死了,但至less文本内容仍然可以在archive.org上find: http ://web.archive.org/web/20091008034423/http: //blog.itwarlocks.com/2009/04/28/访问-WebDAV的在微软字-视觉基本

我花了一些时间在这个问题上 – 我试图validation一个文件,然后再打开它。

最后,我想出了一个使用XML和SOAP的解决scheme – 使用EnumerateFolder方法,并用该文件夹的内容拉入XML响应。

我在这里博客了。

驱动器映射到共享点(也是https)

获取共享点内容通过映射驱动器为我工作迭代它作为一个文件系统对象; 诀窍是如何设置映射: 从共享点,作为资源管理器打开 然后复制path(用http *)(见下面)

浏览器中的地址

在浏览器或命令的地图驱动器中使用此path(即net use N: https:://thepathyoujustcopied )注意:HTTPS可以与Windows7 / 8,与XP无关。

这可能适用于你,但我更喜欢不同的方法,因为每个电脑上的驱动器号码是不同的。 这里的技巧是从共享点开始(而不是从访问共享点的VBA脚本作为Web服务器)。

build立一个数据连接到excel表单

  • 在共享点上,浏览到您要监视的视图
  • 导出视图为Excel(2010年:库工具; libarry |导出到Excel) 导出为ex​​cel
  • 当查看这个excel,你会发现一个数据源设置(选项卡:数据,连接,属性,定义)

连接选项卡

你可以在vba中包含这个查询,也可以在你的电子表格中维护数据库链接,用VBA遍历表。 请注意:上面的图片没有显示实际的数据库连接(命令文本),它会告诉你如何访问我的共享点。

我把这个问题弄混了一下,发现了一个非常简单的2行解决scheme,只需要replace'http'和所有正斜杠就可以了:

 myFilePath = replace(myFilePath, "/", "\") myFilePath = replace(myFilePath, "http:", "") 

它可能不适用于所有人,但它对我有用

如果你正在使用一个安全的网站(或希望迎合两者),你可能希望添加以下行:

 myFilePath = replace(myFilePath, "https:", "") 

尝试将Sharepoint库映射到Windows中的驱动器号。 然后在代码中select驱动器和path。