【问题标题】:DirExist for OneDrive synchronized Sharepoint directoriesDirExist 用于 OneDrive 同步的 Sharepoint 目录
【发布时间】:2021-08-09 15:45:19
【问题描述】:

现在,即使您没有通过例如链接到公司网络,您也可以通过 OneDrive 处理同步的共享点内容。 VPN,但通过您的公司帐户登录到 Onedrive。奇怪的是,在这种断开连接的情况下,DIR 函数为肯定存在的目录返回了一个空值。我看到了尝试创建映射驱动器的解决方案,但它可以做得更简单。 我会自己回答这个问题。

【问题讨论】:

    标签: vba directory filesystemobject


    【解决方案1】:

    罗伯特,这正是你的代码,但更密集。

    With 语句负责处理对象引用并确保在到达End With 块时将其销毁。

    Public Function DirExists(ByVal path As String) As Boolean
        On Error Resume Next
        
        With CreateObject("Scripting.FileSystemObject")
            DirExists = Not .GetFolder(path) Is Nothing
        End With
        
        On Error GoTo 0
    End Function
    

    这只不过是编写特定函数的不同方法。你的例子绝对没有错。

    【讨论】:

    • 我喜欢!我想知道对象内存会发生什么。是立即清除对象内存还是等待垃圾收集器?
    • VBA 中没有垃圾收集器。一旦对象超出范围,内存就会被回收。
    【解决方案2】:

    这是我的解决方案:

    Function MyDirExists(ByVal myPath As String) As Boolean
    
        'Dir() doesn't work on directories in synchronized sharepoint 
        'that appear in your OneDrive folders
        'Let's use the FileSystem instead. 
        'Use Late binding as not everyone has the library FileSystemObject included
    
        Dim objFSO As Object    'Late binding
        Dim objfolder As Object 'Late binding
    
        Set objFSO = CreateObject("Scripting.FileSystemObject")
    
        On Error Resume Next
        'if directory does not exist GetFolder raises an error
        'and we happily use that fact
        
            Set objfolder = objFSO.GetFolder(myPath)
            
            If Not objfolder Is Nothing Then
                MyDirExists = True  'Default return value is False
            End If
            
        On Error GoTo 0
        
        'Clean up objects
        Set objFSO = Nothing
        Set objfolder = Nothing
        
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      • 2017-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多