excel background save via vba - vba

The Workbook.Save line in my macro is holding everything up, and while it's important that there's a save step at the end of the macro, I don't mind if it just starts saving and then hands control back to the user.
Is there such a thing as Workbook.Save BackGround or Workbook.Save vbModeLess?

Is there such a thing as Workbook.Save BackGround or Workbook.Save vbModeLess?
Definitively, no. The full list of methods available to the workbook object:
http://msdn.microsoft.com/en-us/library/office/ff847316(v=office.14).aspx
The .Save method does not have any optional arguments:
http://msdn.microsoft.com/en-us/library/office/ff197585(v=office.14).aspx
It seems you are perceiving a "problem" with your code which is not actually a problem, but normal and expected functionality, as I explained in the comments above:
When a user manually saves the file, the application is not interactive. The user can't do anything except wait for the save to finish.
The same occurs when you invoke the .Save method of the workbook object, or the Application.CommandBars.ExecuteMso "FileSave", etc.
This is necessary because (obviously) changes made while saving would not be saved, but the workbook's .Saved property would display True.
This property is used in determining whether to show the "Close this workbook with unsaved changes?" dialog when the user closes the file. If the property is True, then the user can close without any prompt. But of course if you let them make changes this will inevitably lead to unwanted data loss as the user may then close the file with saved state True and unsaved changes to the workbook which have not been reflected in the Saved property.
(Note: there are probably more technical reasons, too, but this is just the common-sense explanation)
If the length of time it takes to save the file is burdensome, you have at least a few options I can think of, first you would want to consider notifying the user that the file is going to be saved and this may take upwards of 45 seconds. This way, they do not think the program is unresponsive or otherwise hanging. You can do this with a MsgBox or a UserForm pretty easily.
Alternatively, you could use either of the above methods to prompt the user, i.e., "Do you want to save the file?"
Etc.

When excel saves a file it created a temporary file with a name like A82732KS.tmp and the quickly delete the original file and rename the temp file (possibly in an atomic operation). To do this excel has to release control of the file to avoid a sharing violation so it necessarily disables any changes in memory in order to guarantee that was is written on file and what is loaded in memory is identical.

Related

MS Excel how to undo multiple actions using VBA

I am wondering if I can undo all the actions in the undo queue,
I know of the Application.Undo function but this only undoes the last action and clears the queue, I tried putting this in a loop and it just kept undoing then redoing.
Extra info:
Users open a form in read only,
Accidentally change fields then use a form that makes changes then saves it (in vba) and the accidental changes are saved too,
I want to undo all the accidental changes before the user runs the form so only the necessary changes are saved.
Can I undo multiple actions at once?
You can close your workbook without saving. This is the only method I am aware of. To avoid data loss run your macros on the copy of your data.
Basically, you can't undo anything after VBA runs...

Check for "save changes to 'filename' prompt w/ VBA?

First off, this is not about the saveasfilename dialog box.
Is there a way to have a flag set that checks to see if the "want to save your changes to 'filename.xsls'? dialog box appears during a VBA sub ?
Basically I have a macro that copies some data into another file, displays a MsgBox then closes the file and Excel SHOULD prompt the user to confirm that the file is saved. However, I have a legacy program that locks that file sometimes, causing the prompt to not appear, and it looks like the file was saved but actually wasn't. To 'fix' it I have to close all instances of Excel and the program and start over. It doesn't happen often, but you can miss it if you aren't paying attention or someone less experienced in the process doesn't know to check to make SURE they are prompted to save.
What I'd like to know is if there's a way to have some sort of check / flag value, 1/0, true/false, etc to make sure that dialog box appears. If it doesn't then warn the user that they need to restart Excel and the other program. Basically I'm trying to catch an error that should never happen, so this might be unsolvable.
This is the dialog box I'm referring to:
Sometimes it doesn't appear because the file is locked and the VBA sub just continues on.
It would probably better to solve the other problem, but since you don't have posted it, we don't know it.
Anyway, everything you want is described here: https://support.microsoft.com/en-us/kb/213428
You can bascally do this:
Sub Auto_Close()
If ThisWorkbook.Saved = False Then
'ThisWorkbook.Save this would autosave
Application.GetSaveAsFilename 'this displays the save as dialog
End If
End Sub
I ended up doing this as a work around and it's not truly a solution to my question although I suspect there's got to be a way to do it through window classes checking or something. Thank you to everyone who took their time to chime in and offer their brainpower :)
I declared two variables for the file size before and after the sub runs. If the file sizes are equal, then a msgbox appears and says the file may have not been written correctly, or you are overwriting the existing file.
Dim ExportFileByte As Long
Dim ExportBytePostFile As Long
ExportFileByte = FileLen(ExportSourcePath)
' (sub runs here and exports a copy of some data to an .XLS file)
ExportBytePostFile = FileLen(ExportSourcePath)
If ExportFileByte = ExportBytePostFile Then MsgBox ("Error: File may have not have saved, or file is being overwritten without changes (OK)")

VBA Password Protect Excel Workbook without Save

It is possible to password protect an excel workbook for opening without the use of SaveAs method (without any method to save the file)?
Currently in our application, we use the SaveAs method to save and set the password after a workbook is generated and filled with data. The generation of this workbook takes a while as we use a lot of data and the resulting file is quite big (120MB).
We were searching for some ways to accelerate the workbook generation and we found that the SaveAs method takes around 3 minutes to complete because a lot of formulas needs to be calculated. After a small talk with the users, we realize that everyone make some small changes to the UI of the workbook anyway so then they need to save again and wait for another 3 minutes.
We have decided to suppress the SaveAs method and will be the users responsibility to save the workbook after they finish with their changes. And here is the problem, without the SaveAs I couldn't find a way to protect the file with a password.
I have tried the Workbook.Protect but it seems it only protects the structure and the windows from changes but not the file from opening:
ActiveWorkbook.Protect Password:=cPwd, Structure:=True, Windows:=False
I also found the Workbook.ProtectSharing method but I did not tried because the documentation says that it saves the file.
So, is there any way to set a workbook password without saving the file that will be applied when the user save it manually after his changes?
You can use the Workbook.Password property, which is read/write. The password will be applied the next time the file is saved.

Only I am able to save a macro-enabled workbook?

All, I have created a workbook that has some macros in it to import data. The idea is that the file is a master file, and every time you import data with it, it is supposed to place that data on the end of the existing data, and then you save it and move on.
The problem is, I am the only user that can save the workbook. Now, two of the sheets in my workbook I have protected, so that they cannot be edited. I have done this so that nothing can accidentally be removed (buttons, instructions, notes, etc). My users have agreed that this protection is a good thing.
But what I think is happening is our network is making anyone who didn't author this file open it as Read Only, and then they cannot save to it. I first thought maybe if added a save macro (and command button) that it would fix it. No dice.
Next, I had the workbook unprotect, and then re-protect itself when the user clicks the save button. Nope, still opening as Read Only.
I then put code in the Workbook_Open() Sub that changed it from Read Only to Read Write. This caused a box to popup when opening the sheet that said the file was in use by "Another User," and it was locked for editing.
The last thing I tried was adding the other users as Authors to the workbook. And it STILL opens as a Read Only file.
I think this has to do with the network settings here in our office (well, corporate-wide, but anyway). These are policies that cannot be changed. Can anyone help me find a work-around that allows my sheets to be locked for editing, but allows my users to save to my workbook?
You can see here that I have added three other users as Authors of the file (This is the information page of the file as opened by a user.):
So it turns out that the issue was not related at all to the workbook...
When attempting to Save As in the folder where the workbook was located, we found out that the user I was using for the tests simply did not have write permissions to the folder in question. I had write permissions to it based on a previous assignment.
So much frustration over something so simple.

Keep getting Excel and Windows Prompts while working with Interop Services

All of this is being done in VB.NET using the Excel 14.0 Interop Services
I am at my wits end. I keep getting prompts from windows and excel during the middle of a batch run.
The program i have takes in a workbook with batch records, then runs simulations on each batch record, then writes the results back out to the excel file.
The steps:
Open workbook
check to see if workbook is already in use by another program.
if it is in use. we try to close the workbook. then we wait for a set amount of time before trying again.
if the workbook is not in use we continue.
Get the contents
Mark the records as being processed
save and close the file.
process the records.
do the same process above to open the workbook.
save the results to the workbook.
close the workbook.
loop these processes until all the records have been simulated.
Ok the problems that can occur:
Workbook is already in use or two programs are trying to interact with the save workbook at the same time.
Ok now for the problem that i am having.
When the workbook is being interacted with by two programs at the same time. a prompt will show saying the file is currently in use.
another problem that happens that i can't explain is excel will show a prompt saying that the file is now ready to be modified with the options read-write, notify, cancel.
I need to find a way to handle these prompts programmatically.
If any one can point me in the right direction I would be very greatful.
You can prevent the prompts from appearing by setting:
Dim xlApp As Excel.Application = New Excel.Application
xlApp.DisplayEvents = False
But I've not found a way to actually "catch" the prompts and do something useful. I've noticed that often if Interop cannot get hold of a file then it will throw an exception. The exception rarely contains any way to distinguish what the actual error is, but you can sometimes work it out based on what could happen at that point.