![](/img/trans.png)
[英]How to get the path+filename of an excel workbook stored in my local OneDrive folder? (as opposed to its URL!)
[英]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.