SOLVED

Excel VBA and OneDrive SaveCopyAs

%3CLINGO-SUB%20id%3D%22lingo-sub-3124294%22%20slang%3D%22en-US%22%3EExcel%20VBA%20and%20OneDrive%20SaveCopyAs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3124294%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20running%20office%20365%20home%20on%20windows%2010%20both%20latest%20versions.%20I%20have%20a%20project%20that%20requires%20the%20workbook%20(%22Diddly.xlsm%22)%20data%20to%20be%20archived%20every%206%20months%20to%20a%20code%20generated%20file%20name%20leaving%20the%20original%20file%20clear%20of%20data.%20The%20project%20was%20developed%20using%20workstation%20(Acer%20Laptop)%20based%20files%20and%20worked%20as%20expected.%20I%20needed%20to%20allow%20others%20share%20access%20so%20started%20to%20use%20OneDrive.%20Opening%20the%20workbook%20from%20OneDrive%20generated%20an%20error%20at%20the%20line%20ActiveWorkbook.SaveCopyAs%20lcfile.%20Looking%20at%20the%20file%20path%20obtained%20not%20surprising.%3C%2FP%3E%3CP%3EMy%20question%20is%20how%20can%20I%20code%20to%20obtain%20a%20valid%20file%20save%20path%20when%20using%20the%20online%20version%20of%20the%20workbook%20OR%20is%20there%20another%20way%20to%20what%20I%20want%3F%3C%2FP%3E%3CP%3EThe%20test%20code%20is%3A%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23DF0000%22%3ESub%20test()%3C%2FFONT%3E%3CBR%20%2F%3EDim%20lcfilesavepath%2C%20lcfile%20As%20String%3CBR%20%2F%3EProtection%20(False)%3CBR%20%2F%3EApplication.DisplayAlerts%20%3D%20False%3CBR%20%2F%3Elcfilesavepath%20%3D%20Application.ThisWorkbook.Path%20%26amp%3B%20%22%5CArchive%5C%22%3CBR%20%2F%3E%3CFONT%20color%3D%22%23008000%22%3E'Opening%20the%20diddly%20file%20from%20LapTopUser%20with%20OneDrive%20closed%20gives%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3Elcfilesavepath%3DC%3A%5CUsers%5COwner%5COneDrive%5CDiddly_Share%5CArchive%5C%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23008000%22%3E'lcfilesavepath%20%3D%20%22%22%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23008000%22%3E'If%20lcfilesavepath%20is%20not%20used%20the%20file%20is%20saved%20to%20C%3A%5CUsers%5COwner%5COneDrive%5CDocuments%3C%2FFONT%3E%3CBR%20%2F%3Elcfile%20%3D%20lcfilesavepath%20%26amp%3B%20Range(%22Title%22).Value%20%26amp%3B%20%22.xlsm%22%3CBR%20%2F%3E%3CFONT%20color%3D%22%23008000%22%3E'If%20the%20diddly%20file%20is%20opened%20from%20OneDrive%20the%20Filesavepath%20%3D%20%3CA%20href%3D%22https%3A%2F%2Fd.docs.live.net%2F61298770340f%2FDiddly_Share%7C%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fd.docs.live.net%2F61298770340f%2FDiddly_Share%7C%3C%2FA%3EArchive%3C%2FFONT%3E%3CBR%20%2F%3E'%3CFONT%20color%3D%22%23008000%22%3Eand%20the%20next%20line%20fails%20using%20lcfilesavepath%20https%3A%5C%5C...%20or%20null%3C%2FFONT%3E%3CBR%20%2F%3EActiveWorkbook.SaveCopyAs%20lcfile%3CBR%20%2F%3Elvans%20%3D%20MsgBox(%22The%20workbook%20has%20been%20saved%20to%20file%20%22%20%26amp%3B%20lcfile)%3CBR%20%2F%3EApplication.DisplayAlerts%20%3D%20True%3CBR%20%2F%3EProtection%20(True)%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23DF0000%22%3EEnd%20Sub%3C%2FFONT%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3124294%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3129937%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20VBA%20and%20OneDrive%20SaveCopyAs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3129937%22%20slang%3D%22en-US%22%3EIt%20looks%20like%20the%20SaveCopyAs%20does%20not%20allow%20you%20to%20save%20to%20a%20onedrive%20location.%20The%20only%20way%20to%20do%20this%20is%20by%20using%20the%20SaveAs%20method%20I'm%20afraid.%20So%20you'll%20have%20to%3A%3CBR%20%2F%3E-%20Remember%20the%20current%20name%3CBR%20%2F%3E-%20Save%20as%20to%20the%20new%20location%3CBR%20%2F%3E-%20Remove%20data%3CBR%20%2F%3E-%20Save%20as%20to%20the%20remembered%20name%3C%2FLINGO-BODY%3E
Occasional Contributor

I am running office 365 home on windows 10 both latest versions. I have a project that requires the workbook ("Diddly.xlsm") data to be archived every 6 months to a code generated file name leaving the original file clear of data. The project was developed using workstation (Acer Laptop) based files and worked as expected. I needed to allow others share access so started to use OneDrive. Opening the workbook from OneDrive generated an error at the line ActiveWorkbook.SaveCopyAs lcfile. Looking at the file path obtained not surprising.

My question is how can I code to obtain a valid file save path when using the online version of the workbook OR is there another way to what I want?

The test code is:

Sub test()
Dim lcfilesavepath, lcfile As String
Protection (False)
Application.DisplayAlerts = False
lcfilesavepath = Application.ThisWorkbook.Path & "\Archive\"
'Opening the diddly file from LapTopUser with OneDrive closed gives
lcfilesavepath=C:\Users\Owner\OneDrive\Diddly_Share\Archive\
'lcfilesavepath = ""
'If lcfilesavepath is not used the file is saved to C:\Users\Owner\OneDrive\Documents
lcfile = lcfilesavepath & Range("Title").Value & ".xlsm"
'If the diddly file is opened from OneDrive the Filesavepath = https://d.docs.live.net/61298770340f/Diddly_Share|Archive
'and the next line fails using lcfilesavepath https:\\... or null
ActiveWorkbook.SaveCopyAs lcfile
lvans = MsgBox("The workbook has been saved to file " & lcfile)
Application.DisplayAlerts = True
Protection (True)

End Sub

11 Replies
It looks like the SaveCopyAs does not allow you to save to a onedrive location. The only way to do this is by using the SaveAs method I'm afraid. So you'll have to:
- Remember the current name
- Save as to the new location
- Remove data
- Save as to the remembered name
Thank you for your response. Due to my inadequate question it does not address my issue. I have no problem when codng for SaveCopyAs when working with files for which I have a storage address such as C:\users\owner\Onedrive\..... But when I try to code for files I am sharing from another's OneDrive source I can't get a useable location to save any edits to the shared OneDrive. The code Application.ThisWorkbook.Path returns a URL https://d.docs.live.net/61298770340f/Diddly_Share. which cannot be interpreted by subsequenr coding.
I suspect you missed the point. SaveCopyAs does not work for OneDrive folders. you have to use SaveAs instead of SaveCopyAs.

Hi@Jan Karel Pieterse 

I've attached Sub test() and the error displayed on running " Application.ActiveWorkbook.SaveAs (lcFile)". I'm guessing that I have something wrong with the syntax? Can you advise?

@coltartjmcsa3re my msg 9/2/22 I attached the wrong .gif file. Correct file attached. I'm not dong too well with this problem. Thanks for your patience.

@Jan Karel Pieterse 

re my msg 9/2/22 I attached the wrong .gif file. Correct file attached. I'm not dong too well with this problem. You probaby noticed I managed to send a msg to myself. Thanks for your patience.

In your code the filename is never assigned, lcFile is an empty string.

@Jan Karel Pieterse 

I can't apologise enough. This will be my third try at sending you the correct info. The code works when opening the Excel on my laptop when it has access to the resident file locations. When I open Excel from the online view of OneDrive the code fails with the message shown in the attached .gif. I don't see a way round this and I may have to be satisfied by making the archive file "manually" using the Save As option resident in Excel. As I sometimes reply on others to operate the workbook I would have preferred coding. Perhaps I need to find another route to a solution.

best response confirmed by coltartjmcsa3 (Occasional Contributor)
Solution
I don't know what the current value of lcFile is when the error occurs?

If you pass just a file name (no path) to the SaveAs method, Excel assumes it needs to be saved in the active directory. Judging by the error message you showed that seems to be c:\Windows. Users are not allowed to save files there. You must provide the SaveAs method with both the path and a filename.

@Jan Karel Pieterse 

Yes I understand all that. The problem is that the sharer would have to provide the path (we could agree one in advance) and then move the file into OneDrive "manually" (can't see a way to provide a path to OneDrive). As I said earlier I need to find another way round this.

I appreciate your help and have gained some more knowledge on the way.

This is my first time of posting on this site so don't know how to close this out.

I was assuming the file already is on a OneDrive folder. So ActiveWorkbook.Path would give you that.