2

我在 SO、SU 和SP.SE中搜索了解决方案,但找不到我需要的东西。我正在寻找可能是脚本其他一些非编码方法/工具的解决方案。

我正在尝试编写一个脚本(供其他人使用)或某种其他形式的自动化来自动将各种报告上传到 SharePoint 网站。我已经设法使以下(VBScript)代码工作,但仅适用于基于文本的文件——在这种情况下为 .CSV,尽管这也适用于 .TXT 等。

Option Explicit

Dim sCurPath
sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
UploadAllToSP sCurPath

Sub UploadAllToSP(sFolder)
    Dim fso, folder, fil
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder(sFolder)
    For Each fil In folder.Files
        If fso.GetExtensionName(fil) = "csv" Then
            UploadFileToSP fil
        End If
    Next
End Sub

Sub UploadFileToSP(ofile)
    Dim xmlhttp
    Dim sharepointUrl
    Dim sharepointFileName
    Dim tsIn
    Dim sBody

    Set tsIn = ofile.openAsTextstream
    sBody = tsIn.readAll
    tsIn.close
    sharepointUrl = "http://SHAREPOINT URL HERE"

    sharepointFileName = sharepointUrl & ofile.name
    set xmlHttp = createobject("MSXML2.XMLHTTP.4.0")
    xmlhttp.open "PUT", sharepointFileName, false
    xmlhttp.send sBody
    If xmlhttp.status < 200 Or xmlhttp.status > 201 Then
        wscript.echo "There was a problem uploading " & ofile.name & "!"
    End If
End Sub

这仅适用于文本文件,因为它将文本数据通过管道传输到 SP 站点上的文件中。但是,如果我想传输任何类型的二进制文件(.XLS、.PDF),这会导致上传垃圾。

我试图看一下Shell.Application==> .Namespace(),但这似乎不适用于 URL,而只能用于物理驱动器。这是我尝试过的其他一些内容(修剪以显示相关部分):

Set oApp = CreateObject("Shell.Application")

If oApp.NameSpace(sharepointUrl) <> Null then ' Always Null!
    ' Copy here
    ' Some lines omitted
    oApp.NameSpace(sharepointUrl).CopyHere ofile.Name ' This also fails when not surrounded by the Null check
Else
    MsgBox "SharePoint directory not found!"
End If

我还尝试了使用 xcopy 的批处理文件,但无法连接到http://任何一个。我查看了这种方法,它可能对我有用,但我不想处理 mapping/ NET USE,因为我们公司有多个网络共享,其映射取决于谁登录。

由于这些都不是我需要的方式:有没有一种方法可以自动化这种功能?

我有使用 VBA/VBscript 的经验,所以像上面这样的脚本,或者内置在 MS Office 应用程序中的东西(Outlook 是最好的,但我可能可以适应我所得到的任何东西)会更可取。话虽如此,我对任何允许我这样做的方法持开放态度,在 Windows 或 Office 中本地运行。但是,我无权访问 Visual Studio,因此无法使用任何 .NET 功能。

4

2 回答 2

5

感谢Sean Cheshire指出我没有看到的明显答案。发布相关代码,因为我不相信这在 SO 上还存在。

Sub UploadFilesToSP(sFolder)

Dim sharepointUrl
Dim sharepointFileName
Dim LlFileLength
Dim Lvarbin()
Dim LobjXML
Dim LvarBinData
Dim PstrFullfileName
Dim PstrTargetURL
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim fldr
Dim f

    'This has not been successfully tested using an "https" connection.
    sharepointUrl = "http://SHAREPOINT URL HERE"
    Set LobjXML = CreateObject("Microsoft.XMLHTTP")
    Set fldr = fso.GetFolder(sFolder)

    For Each f In fldr.Files
        sharepointFileName = sharepointUrl & f.Name

        PstrFullfileName = sFolder & f.Name
        LlFileLength = FileLen(PstrFullfileName) - 1

        ' Read the file into a byte array.
        ReDim Lvarbin(LlFileLength)
        Open PstrFullfileName For Binary As #1
        Get #1, , Lvarbin
        Close #1

        ' Convert to variant to PUT.
        LvarBinData = Lvarbin
        PstrTargetURL = sharepointFileName 

        ' Put the data to the server, false means synchronous.
        LobjXML.Open "PUT", PstrTargetURL, False

        ' Send the file in.
        LobjXML.Send LvarBinData
    Next f

Set LobjXML = Nothing
Set fso = Nothing

End Sub

这是 VBA 代码,格式化为主要与 VBScript 一起使用,尽管我无法正确传输此块。作为 VBA,这可以通过分配数据类型等来改进。

' Read the file into a byte array.
ReDim Lvarbin(LlFileLength)
Open PstrFullfileName For Binary As #1
Get #1, , Lvarbin
Close #1
于 2012-08-20T15:37:14.363 回答
2

这是一个非常古老的帖子,但非常有用,因此感谢大家的贡献。这是我的早期绑定版本。我发现由于 VBA 假设没有声明的变量类型,以前的帖子不起作用。

Private Sub cmdUploadToApplicationsAndApprovals_Click()

    Dim strSharePointUrl As String
    Dim strSharePointFileName As String
    Dim lngFileLength As Long
    Dim bytBinary() As Byte
    Dim objXML As XMLHTTP
    Dim varBinData As Variant
    Dim strFullfileName As String
    Dim strTargetURL As String
    Dim fso As FileSystemObject
    Set fso = New FileSystemObject
    Dim folder As folder
    Dim file As file
    Dim strFolder As String
    
    strFolder = CurrentProject.Path & "\Upload\"
        
    'This has not been successfully tested using an "https" connection.
    strSharePointUrl = "http://sps.mysite.ca/subsite/DocLib/"
    Set objXML = New XMLHTTP 'CreateObject("Microsoft.XMLHTTP")
    Set folder = fso.GetFolder(strFolder)

    For Each file In folder.Files
        strSharePointFileName = strSharePointUrl & file.Name

        strFullfileName = strFolder & file.Name
        lngFileLength = FileLen(strFullfileName) - 1

        'Read the file into a byte array.
        ReDim bytBinary(lngFileLength)
        Open strFullfileName For Binary As #1
        Get #1, , bytBinary
        Close #1

        'Convert to variant to PUT.
        varBinData = bytBinary
        strTargetURL = strSharePointFileName

        'Put the data to the server, false means synchronous.
        objXML.Open "PUT", strTargetURL, False

        'Send the file in.
        objXML.Send varBinData
        
        'Now Update the metadata
        
        
    Next file

    'Clean up
    Set objXML = Nothing
    Set fso = Nothing
    MsgBox "Done"
End Sub

仅供参考,上述代码需要 2 个参考。1. Microsoft XML,v6.0 2. Microsoft 脚本运行时

希望这有助于改进已经很出色的答案!

于 2015-03-29T23:01:57.817 回答