繁体   English   中英

从 Sharepoint 读取文件

[英]Read File From Sharepoint

我正在编写一个脚本,我希望将 HTML 文档写入来自共享点的字符串。

Dim Content As String
Dim strShare As String: strShare = "\\link\to\share.html"
Dim iFile As Integer: iFile = FreeFile

Open strShare For Input As #iFile
    Content = Input(LOF(iFile), iFile)
Close #iFile

但是,我发现每次在启动时第一次运行脚本时都会出现“路径/文件访问错误”。 一旦我第一次在 IE 中访问“\link\to\share.html”,路径就开始在 VBA 脚本中解析。

我唯一的想法是 IE 正在执行 VBA 无法执行的某种“DNS 缓存”。 目前我的解决方法是捕获错误并强制 URL 在脚本第一次运行时在 IE 中打开。 之后,该共享下的所有其他 HTML 文件都可以正常加载。

作为测试,我尝试在我理解的 http:// 格式(正斜杠)和 WebDAV 格式(\\ 格式)之间切换,并且只有反斜杠分隔的路径有效。 我还尝试将共享解析为 IP 并尝试这样做,但从未奏效。

我最后的想法是尝试将共享映射到驱动器盘符名称,然后使用 G:\link\to\mapped\share.html 专门访问共享。 但我不认为这是一个优雅的解决方案,并且想知道它是否会以任何方式收到相同的错误。

关于 WebDAV、Windows 文件处理和 VBA 文件输入,是否有明显我不了解的地方? 解析该共享域时发生了一些奇怪的事情,我似乎无法调试它。

看看是否有帮助以及下面我使用的示例。

不过有两件事:我只在 Sharepoint 上使用 Excel 文件,而且我已经在那里登录了。

Dim oFSO As Object 
'Dim oFolder As Object 'if needed
'Dim oFile As Object 'if needed

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder("\\sharepoint.site.com@SSL\DavWWWRoot\sites\")

'For Each oFolder In oFolder.SubFolders 'loops through folders
'    For Each oFile In oFolder.Files 'loops through files
'        'do stuff
'    Next oFile
'Next oFolder

我对你想做什么有点困惑。 您想从 SP 签出文件并将文件签回 SP 吗?

Sub testing()
    Dim docCheckOut As String
    'docCheckOut = "//office.bt.com/sites/Training/Design Admin/Training Plan/adamsmacro.xlsm"
    docCheckOut = "http://your_path_here/ExcelList.xlsb"
    Call UseCheckOut(docCheckOut)
End Sub

Sub UseCheckOut(docCheckOut As String)
     ' Determine if workbook can be checked out.
    If Workbooks.CanCheckOut(docCheckOut) = True Then
        Workbooks.CheckOut docCheckOut
    Else
        MsgBox "Unable to check out this document at this time."
    End If
End Sub

或者...您想列出 SP 文件夹中的文件吗?

Sub ListFiles()
    Dim folder As Variant
    Dim f As File
    Dim fs As New FileSystemObject
    Dim RowCtr As Integer
    Dim FPath As String
    Dim wb As Workbook

    RowCtr = 1

    FPath = "http://excel-pc:43231/Shared Documents"
    For Each f In FPath
    'Set folder = fs.GetFolder("C:\Users\Excel\Desktop\Ryan_Folder")
    'For Each f In folder.Files
       Cells(RowCtr, 1).Value = f.Name
       RowCtr = RowCtr + 1
    Next f
End Sub

Sub test()
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder("C:\Users\Excel\Desktop\Ryan_Folder")
    'Set colSubfolders = objFolder.SubFolders
    'For Each objSubfolder In colSubfolders

       Cells(RowCtr, 1).Value = f.Name
       RowCtr = RowCtr + 1

    'Next
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM