1004 runtime error when saving to OneDrive

BarvoDelancy

New Member
Joined
Aug 28, 2019
Messages
2
Hi everyone, new to the boards and stuck on resolving an error. Using O365 Excel. I first did anything with VBA five days ago, but I'm competent otherwise.

What I'm trying to do, and what happens when I do it

This is a fairly simple code that runs as a part of a larger process. The code copies a worksheet and then Saves As to a new filename.

I want to save to a onedrive location which is mapped on my local PC. The procedure runs, creates the sheet, saves it, and I get the following error:

Run-time error '1004':

Method 'SaveAs' of object '_Workbook' failed

If I save to a local location, there's no error. So the issue appears to be saving to a network location. Note that the file is successfully created and saved, even though the file says 'filename.xlsx - Save Failed'.

Here is the code I'm using:

Code:
Sub Export()
    
    Dim fname As String
    Dim fpath As String
    Dim newbook As Workbook
    Dim newfile As String
    
    newfile = "[COLOR=#b22222]C:\pathname\filename.xlsx[/COLOR]"
    
    fpath = "[COLOR=#b22222]C:\pathname[/COLOR]"
    fname = "[COLOR=#b22222]Filename[/COLOR]" & ".xlsx"
    
    Set newbook = Workbooks.Add
    
    ThisWorkbook.Sheets("[COLOR=#b22222]SheetName[/COLOR]").Copy Before:=newbook.Sheets(1)
    
    newbook.SaveAs filename:=newfile

End Sub

Other Info

This is a piece of a larger process wherein the workbook creates forms, user fills in forms, and the forms populate some tables. (this works fine)

One of these tables is supposed to synchronize with Outlook to create events for each table row. My intended solution is to use Flow from O365 to do this. The complication is that Flow does not see .xlsm files as excel files, thus creating a copy of the worksheet with the table I need whenever we do an update. Flow then runs on a schedule to synch the calendar according to the copied file, which is why I want the file in a Onedrive location.

If there's a reasonable way to create Outlook events from excel using VBA I'm also open to that.

Thanks!
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Specifying file format type didn't do it, but I did resolve the issue!

It turns out that when saving to a locally-mapped share I need to save to the actual network location. So when the file path was https://sharepointsite/subfolders the file saves successfully, and even updates new versions as I go.

That said, thanks for the excel to outlook examples I'll be checking those out.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top