[英]check folder path if on onedrive or local
company moved folders to onedrive however some people still using local drives, so I need help to check the folder is exist on one drive or not.公司将文件夹移至 onedrive,但是有些人仍在使用本地驱动器,因此我需要帮助来检查文件夹是否存在于一个驱动器上。 below code for using for onedrive.
下面用于onedrive的代码。 I cannot create if condition on this.
我无法在此基础上创建 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
you could set objFolder
before the loop你可以在循环之前设置
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
Try the next code, please.请尝试下一个代码。 It will create hyperlinks for both cases, if both of them exist:
如果两种情况都存在,它将为这两种情况创建超链接:
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
Please, take care to use your real paths.请注意使用您的真实路径。 I do not think that the OneDrive one is correct in the way you show it.
我认为 OneDrive 的显示方式不正确。 Is it a path on the intranet?
是内网的路径吗?
It will create hyperlinks from OneDrive in A:A column, and ones for local path in C:C column.它将在 A:A 列中从 OneDrive 创建超链接,并在 C:C 列中创建本地路径的超链接。
Of course, if you need only to create hyperlinks for the OneDrive folder (only in case it exists), you just delete the last part treating the local path.当然,如果您只需要为 OneDrive 文件夹创建超链接(仅在它存在的情况下),您只需删除处理本地路径的最后一部分。 Or use
Else
instead of End If ... If
, in case you need the hyperlink for the local path, only in case that the first one does not exist.或者使用
Else
而不是End If ... If
,以防您需要本地路径的超链接,仅在第一个不存在的情况下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.