VBA - 检查SharePoint中是否存在文件夹/文件

时间:2017-11-28 03:29:37

标签: excel vba excel-vba sharepoint

我想通过单击图像使用VBA将本地文件复制到sharepoint库。现在好像我无法检查文件夹& SharePoint上的文件。

每次运行代码时(通过单击excel中的图像),它都返回无法在SharePoint中找到该文件。并在返回MsgBox Sorry there's no such Folder......

时停止

我尝试映射驱动器,它工作得很好,但不是一个选项,因为最终用户需要自己映射驱动器。 所以现在我想使用链接连接到SharePoint。

如果我将SharePointLink复制到IE& Chrome使用\,工作正常。但如果我使用/,IE无法找到该链接。

更新

如果我在几次尝试后使用\,IE将打开NetWork中的文件路径。 Chrome会在Chrome页面上显示文件路径。为什么会这样??????

身份验证使用的是Windows身份验证,因此不是问题。

这是我的代码

Sub imgClicked()

Dim SharePointLib As String
Dim MyPath As String
Dim folderPath As String
Dim objNet As Object
Dim FSO As Object
Dim copyPath As String
Dim copyFilePath As String

folderPath = Application.ThisWorkbook.path
MyPath = Application.ThisWorkbook.FullName

SharePointLib = "//company.com/sites/MS/10%20Mg%20Review/"
' create new folder to store the file
copyPath = folderPath + "\copyPath\" 

If Not FolderExists(copyPath) Then
    FolderCreate (copyPath)
ElseIf Not FolderExists(SharePointLib) Then
    MsgBox "Sorry there's no such folder. Folder Path: " & vbNewLine & vbNewLine & SharePointLib & ""
    Exit Sub
End If

fileName = "hello.xlsm"
'Copy current excel file and save at the new folder created
ThisWorkbook.SaveCopyAs copyPath & fileName
MsgBox "Save Copy As: " + copyPath & filseName & vbNewLine & vbNewLine & "The file will be uploaded to this address: " + SharePointLib & fileName

' Check whether the file exist in the directory
' If exist error message
' else copy the file from copyPath then paste at the SharePoint directory
If Not Dir(SharePointLib & fileName, vbDirectory) = nbNullString Then
    MsgBox "Sorry file already exist!"
Else
    Call FileCopy(copyPath & fileName, SharePointLib & fileName)
    MsgBox "File has being successfuly created in SharePoint!"
End If

Set FSO = CreateObject("scripting.filesystemobject")
If Right(copyPath, 1) = "\" Then
    copyPath = Left(copyPath, Len(copyPath) - 1)
End If
If FSO.FolderExists(copyPath) = False Then
    MsgBox copyPath & " doesn't exist"
    Exit Sub
End If
FSO.DeleteFolder copyPath
MsgBox "Folder has being deleted successfully!"

End Sub

检查文件夹是否存在的功能

Function FolderExists(ByVal path As String) As Boolean

FolderExists = False
Dim FSO As New FileSystemObject

If FSO.FolderExists(path) Then FolderExists = True

End Function

创建文件夹的功能

Function FolderCreate(ByVal path As String) As Boolean

FolderCreate = True
Dim FSO As New FileSystemObject

try:
If FSO.FolderExists(path) Then
    Exit Function
Else
    On Error GoTo catch
    FSO.CreateFolder path
    Debug.Print "FolderCreate: " & vbTab & path
    Exit Function
End If

catch:
MsgBox "A folder could not be created for the following path: " & path & ". Check the path name and try again."
FolderCreate = False
Exit Function

End Function

感谢任何帮助和建议。如果需要更多信息,请与我们联系。提前谢谢。

1 个答案:

答案 0 :(得分:0)

确保WebClient服务正在运行。您可以通过代码启动WebClient服务,也可以将启动类型设置为自动。

运行WebClient服务后,您的文件夹/文件测试将按预期工作。

编辑:此外,如果您将sharepoint网址映射到驱动器号,Windows将启动WebClient服务。

Sub mapPath(str_drive as string, str_path as string)
  If Not Len(str_drive) = 1 Then Exit Sub
  Dim wso As Object
  Set wso = CreateObject("WScript.Network")
  wso.MapNetworkDrive str_drive & ":", str_path, False
End Sub