繁体   English   中英

检查文件夹路径是在 onedrive 还是本地

[英]check folder path if on onedrive or local

公司将文件夹移至 onedrive,但是有些人仍在使用本地驱动器,因此我需要帮助来检查文件夹是否存在于一个驱动器上。 下面用于onedrive的代码。 我无法在此基础上创建 if 条件。

            Set objFSO = CreateObject("Scripting.FileSystemObject")
            Set objFolder = objFSO.GetFolder(Environ("UserProfile") & "\OneDrive - company name\Pictures\Camera Roll")
            i = 1
            For Each objFile In objFolder.Files
                Range(Cells(i + 1, 1), Cells(i + 1, 1)).Select
                ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:= _
                                           objFile.Path, _
                                           TextToDisplay:=objFile.Name

你可以在循环之前设置objFolder

Set objFSO = CreateObject("Scripting.FileSystemObject")

With objFSO
    If .folderexists(Environ("UserProfile") & "\OneDrive - company name\Pictures\Camera Roll") Then
        Set objFolder = objFSO.GetFolder(Environ("UserProfile") & "\OneDrive - company name\Pictures\Camera Roll")
    Else
        Set objFolder = objFSO.GetFolder(Environ("UserProfile") & "\picture\camera roll")
    End If
End With

请尝试下一个代码。 如果两种情况都存在,它将为这两种情况创建超链接:

Sub CheckOneDriveVersusLocalToHyperlink()
 Dim objFSO As Object, objFolder As Object, objFile As Object, sh As Worksheet, i As Long
 Const OneDrPath As String = "\OneDrive - company name\Pictures\Camera Roll"
 Const LocalPath As String = "c:\users\userprofile\picture\camera roll"

 Set sh = ActiveSheet

 Set objFSO = CreateObject("Scripting.FileSystemObject")
 If objFSO.FolderExists(Environ("UserProfile") & OneDrPath) Then
    Set objFolder = objFSO.GetFolder(Environ("UserProfile") & OneDrPath)
    i = 1
    For Each objFile In objFolder.Files
        ActiveSheet.Hyperlinks.Add sh.cells(i + 1, 1), Address:= _
                                objFile.Path, TextToDisplay:=objFile.Name
        i = i + 1
    Next
 End If
 If objFSO.FolderExists(LocalPath) Then
    Set objFolder = objFSO.GetFolder(LocalPath)
    i = 1
    For Each objFile In objFolder.Files
        ActiveSheet.Hyperlinks.Add sh.cells(i + 1, 3), Address:= _
                                objFile.Path, TextToDisplay:=objFile.Name
        i = i + 1
    Next
 End If
End Sub

请注意使用您的真实路径。 我认为 OneDrive 的显示方式不正确。 是内网的路径吗?

它将在 A:A 列中从 OneDrive 创建超链接,并在 C:C 列中创建本地路径的超链接。

当然,如果您只需要为 OneDrive 文件夹创建超链接(仅在它存在的情况下),您只需删除处理本地路径的最后一部分。 或者使用Else而不是End If ... If ,以防您需要本地路径的超链接,仅在第一个不存在的情况下。

暂无
暂无

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

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