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. below code for using for onedrive. I cannot create if condition on this.
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
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. 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.
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. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.