VBA Macro ActiveWorkbook.SaveCopyAs Run-time error '1004'

damrkstr

New Member
Joined
May 19, 2020
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hi All, I have been trying to search in the existing threads for a solution to my question and cannot seem to find one. I am trying to create this generic SaveCopyAs macro to save a copy of my current file with the current date in a "Superseded" folder in the current directory where my file is saved, and I keep getting a run-time error saying that the file could not be found and "Is it possible it was moved, renamed or deleted?". The "Superseded" folder already exists. Any help you could provide in getting this code to work would be much appreciated! thank you!

Sub Supersede_Copy()
ActiveWorkbook.SaveCopyAs Filename:=ActiveWorkbook.Path & "\Superseded\" & Format(Now, "yyyy-mm-dd") & " " & ActiveWorkbook.Name
End Sub
 
I am not really sure what to tell you. When I test your code (after the slash correction), it works perfectly fine for me. So I cannot reproduce the behavior you are talking about.
Can you do me one last favor? Can you post the full path and file name that is being brought back by the message box?
I think that there may be a limit to the maxiumum file name path and length, so I would be interested to see how many characters it is.

Thank you Joe4 for your patience, I appreciate it. I think it's a server issue with my work and the file path and username part of the path. What is odd though and I do not understand is that the code below works perfectly, but it does a SaveAs. As soon I change the ActiveWorkbook.SaveAs to ActiveWorkbook.SaveCopyAs it doesn't work. If I type the exact path and filename in quotes in vba it does work. I cannot provide you with the exact path given it is work related directories but I can tell you that it is 164 characters, or 180 with spaces. I'm not sure that's the issue though since it works when I put the full path name explicitly. I was just hoping to leave it dynamic. I think what is happening is that ActiveWorkbook.path is bringing in a different path on my company server, without my username attached, and the actual path needed is the one with the username. Nor sure if there is a fix for this. My path is structured as follows, where the items in CAPS are placeholders for the actuals. "C:\Users\USERNAME\SERVERNAMEl\DOCUMENTS FOLDER\SECOND FOLDER\THIRD FOLDER\Superseded\2020-05-19 FILE NAME.xlsb"
What the ActiveWorkbook.path is bringing in is structured as follows : "https://company.sharepoint.com/FOLDER ONE/FOLDER TWO/FOLDER THREE/Supersede/"

VBA Code:
Sub Supersede_Copy()
    Dim fname As Variant
    fname = ActiveWorkbook.path & "\Superseded\" & Format(Now, "yyyy-mm-dd") & " " & ActiveWorkbook.Name
    MsgBox fname
    ActiveWorkbook.SaveAs Filename:=fname
End Sub

Thank you for all your effort and help. It is greatly appreciated!
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
You are welcome.
I think you are probably right, it may something with your servers, as I could not reproduce that error. I though maybe the "superseded" path might push it over the character limit, but based on your response, that doesn't appear to be the case.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,212
Members
453,023
Latest member
alabaz

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