2

I have a VBA file which creates packs to support salespeople present offers to their customers. I have a main template, but the nature of this specific sales process is that the offer could change multiple times before the customer agrees anything.

To account for these re-runs, the macro will create a folder in OneDrive, and then save itself within that folder so that it can be run without having to start afresh.

However, I am getting some errors when it gets to the below code - here is a walkthrough of the steps:

  • SaveFilePath is a string which contains the target folder for whichever user is running it - i.e. [C:\Users\User.Name\Company\Output]
  • It then gets some contract details added to determine the name of the folder to create for that customer - i.e. [C:\Users\User.Name\Company\Output\123 - Customer Name - Gas]
  • I then use MDir to create a folder with the above path - this works
  • It then saves itself within the folder it has created, with a name ending in "Master.xlsb" - this is where I am getting the issues

The process throws out this error:

Run-time error 1004: Method 'SaveAs' of object '_Workbook' failed

However, when I check the folder, it has successfully saved itself within the folder - i.e. it has worked, but it still throws out an error.

I have played around with some of the parameters in the SaveAs line, but still get this error. Likewise with turning alerts off - still get the same issue.

SaveFilePath = SaveFilePath & TenderID & " - " & wsControl.Range("CustomerName") & " - " & Utility & " " & MPAN & "\"
If Dir(SaveFilePath, vbDirectory) = vbNullString Then VBA.FileSystem.MkDir (SaveFilePath)


'    Application.DisplayAlerts = False
        wb1.SaveAs FileName:=SaveFilePath & FileName & " Master.xlsb", ConflictResolution:=xlLocalSessionChanges  'add thisback in when live
'    Application.DisplayAlerts = True

Any help with this would be appreciated - I am banging my head against a wall with this one.

Thanks :)

2
  • Why do you prefix the file name with a space? " Master.xlsb"
    – StureS
    Mar 12, 2021 at 10:26
  • The file name ends up as a combination between the customer name, the product and the word "Master" - like this "Test Customer - Gas Master.xlsb" The first part of this filename comes from the variable "FileName" in the code above - forgot to specify that, sorry! Mar 12, 2021 at 10:37

1 Answer 1

0

I'm getting the same error with a slightly different scenario, so thought the info might help lead us both somewhere.

I am saving my file into an existing OneDrive folder from VBA and am getting the same error.

I noticed the filename my process creates was not C:... but was https:// (even though I was running from C:). I'm assuming this is due to it being a OneDrive-synched folder. I adjusted my process to account for this, but the error still occurs.

When I run the process in a non-OneDrive folder...

  1. I don't get the issue
  2. When the file already exists, I get a "do you want to replace" message, not an error

I'm speculating that this is some sort of OneDrive Synch delay/cleanup issue since it goes away over time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.