【问题标题】:VBA - Checking Folder/File exist in SharePointVBA - 检查 SharePoint 中是否存在文件夹/文件
【发布时间】:2017-11-28 03:29:36
【问题描述】:

我想通过单击图像使用 VBA 将本地文件复制到共享点库。现在好像我无法检查 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

感谢任何帮助和建议。如果需要更多信息,请告诉我。提前致谢。

【问题讨论】:

  • 试试SharePointLib = "\\company.com\sites\MS\10 Mg Review\"
  • @TimWilliams 是的,我现在正在使用SharePointLib = "\\company.com\sites\MS\10 Mg Review\"。现在可以正常工作,但是早上找不到路径,您知道为什么一段时间后它会突然工作吗?我根本没有更改代码。
  • 我偶尔会看到这种情况 - 通常打开 Windows 资源管理器并导航到有问题的路径会解决它。
  • @TimWilliams 也许你是对的。我早上正在使用 chrome 进行测试,但无法获得路径。下午,我用IE测试,它工作正常。使用 IE 测试后,Chrome 也可以正常使用。
  • 通过 UNC 路径访问依赖于 webclient 服务。 Explorer 会处理此问题并自动连接到服务器上的 WebDAV 共享。完成后,系统将能够通过 UNC 进行连接

标签: vba excel sharepoint


【解决方案1】:

确保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

【讨论】:

  • 感谢您提供信息。它帮助我弄清楚我可以在哪里工作。我发现 this 有用的帖子有完整的解决方案并解释了过程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-05
  • 2013-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-20
  • 2011-11-15
相关资源
最近更新 更多