繁体   English   中英

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

[英]VBA - Checking Folder/File exist in SharePoint

我想通过单击图像使用VBA将本地文件复制到sharepoint库。 现在看来,我无法在SharePoint上检查“文件夹和文件”。

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

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

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

更新

如果我使用\\试了几次后,IE浏览器,将打开网络文件路径。 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

任何帮助和建议,表示赞赏。 让我知道是否需要更多信息。 提前致谢。

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

随着WebClient服务的运行,您的文件夹/文件测试将按预期进行。

编辑:另外,如果将共享点URL映射到驱动器号,则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

暂无
暂无

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

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