繁体   English   中英

在 OneDrive 中新建文件夹

[英]Create new Folder in OneDrive

多年来一直在使用下面的代码。 它创建新文件夹,并将其命名为下一个工作日的日期+在此文件夹中添加另一个名为“VO”的文件夹。 代码有两个“fPath”行。 暂停的那个是原来的那个。 有了这个我可以移动我的文件,代码仍然会根据 ThisWorkbook 的位置创建新文件夹。

但是,对于 OneDrive,原始“fPath”行以“运行时错误 52:文件名或编号错误”结尾,标记行.CreateFolder (EndDir1) 为什么此代码在 OneDrive 中不起作用? 当我将“fPath”行更改为完整地址时,它工作得很好。

Sub NewFolderNextWorkDay()

Dim FSO As Object
Dim fsoObj As Object

Dim NeArbDg As Double
NeArbDg = Application.WorkDay(Date, 1)

Dim Dato As String
Dim fPath As String
Dim EndDir1, EndDir2 As String
Dato = Format(NeArbDg, "yyyy-mm-dd")

'fPath = ThisWorkbook.Path & "\..\"    '(old code, worked fine until OneDrive came along)
fPath = "C:\Users\MyId\OneDrive - MyJob\Mine dokumenter\PROD\TEST\2022\"   '(new code, works ok with OneDrive)

EndDir1 = (fPath & Dato & "\")
EndDir2 = (fPath & Dato & "\VO")

Set fsoObj = CreateObject("Scripting.FileSystemObject")
    
    With fsoObj
    
        If Not .FolderExists(EndDir1) Then
        .CreateFolder (EndDir1)
        End If
        
        If Not .FolderExists(EndDir2) Then
        .CreateFolder (EndDir2)
        End If
        
    End With

End Sub

这个来自链接帖子( https://stackoverflow.com/a/67582367/478884 )的 function 似乎对我有用。 strCID没有内容时,我确实需要进行更改以解决问题。 请参阅标记为#### 的行

Function GetLocalFile(wb As Workbook) As String
    ' Set default return
    GetLocalFile = wb.FullName

    Const HKEY_CURRENT_USER = &H80000001

    Dim strValue As String

    Dim objReg As Object: Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
    Dim strRegPath As String: strRegPath = "Software\SyncEngines\Providers\OneDrive\"
    Dim arrSubKeys() As Variant
    objReg.EnumKey HKEY_CURRENT_USER, strRegPath, arrSubKeys

    Dim varKey As Variant
    For Each varKey In arrSubKeys
        ' check if this key has a value named "UrlNamespace", and save the value to strValue
        objReg.getStringValue HKEY_CURRENT_USER, strRegPath & varKey, "UrlNamespace", strValue

        ' If the namespace is in FullName, then we know we have a URL and need to get the path on disk
        If InStr(wb.FullName, strValue) > 0 Then
            Dim strTemp As String
            Dim strCID As String
            Dim strMountpoint As String
        
            ' Get the mount point for OneDrive
            objReg.getStringValue HKEY_CURRENT_USER, strRegPath & varKey, "MountPoint", strMountpoint
        
            ' Get the CID
            objReg.getStringValue HKEY_CURRENT_USER, strRegPath & varKey, "CID", strCID
        
            ' strip off the namespace and CID
            If Len(strCID) > 0 Then strValue = strValue & "/" & strCID     '#####
            strTemp = Right(wb.FullName, Len(wb.FullName) - Len(strValue)) '#####
        
            ' replace all forward slashes with backslashes
            GetLocalFile = strMountpoint & "\" & Replace(strTemp, "/", "\")
            Exit Function
        End If
    Next
End Function

尝试这个:

切换 https://my.... 到 C:\users...


Sub Sample()
    GetLocalFile = Split(ThisWorkbook.Path, "/Documents")(2)
    GetLocalFile = Replace(GetLocalFile, "/", "\")
    MyPath = Environ("onedrive") & "\documents" & GetLocalFile
    MkDir (MyPath & "\New")
End Sub

暂无
暂无

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

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