VBA: Before Save Event - Suspend Running Until User Confirms Save As - vba

I only want my code to run if the workbook is going to be saved. At present, if Save As is selected, the code will run as soon as the user attempts to browse for a file/location. The problem is they can still cancel the save.
Alternatively, I want to be able to run some other code if the user decides to cancel the save. (I don't think the Cancel Boolean in the Before Save event can be used because once the user selects browse, it is assumed the file is being saved)
Maybe the Before Save event cannot be used in this instance but I want to avoid disabling/replacing the Save As functionality if possible.

I was able to handle this using the After Save event - I didn't realise that this could be used if the file wasn't actually saved.

Related

Change method of save in Word

I would like to know if it is possible to change the way of saving a Word document. What I want to do is have a button to save the word document which I have done successfully but if the Save button on the Ribbon or the Save\Save As option is selected a message is displayed instructing the user to save using the button on the document. How can this be done please?
What you need to do is 'catch' the Save event by adding an event handler before save happens.
Look at this post where they ask for something similar:
How to run a macro in Word before save?
It depends on you environment (Add-in, VBA, other) what exactly the eventhandler looks like but if you Google for DocumentBeforeSave you should be able to find enough examples.

Handling save event in Outlook

Good morning, I need my VBA code to run right before the TaskItem is saved, is there any way I can handle the event of saving ? I cannot find anything I could use in documentation. I am using MS-Office 2010.
Edit: I have tried
Private Sub TaskItem_Quit()
The macro disappears from macro list, but the code still does not run.
Try the Write event.
https://msdn.microsoft.com/en-us/library/office/ff868664.aspx
"Occurs when an instance of the parent object is saved, either explicitly (for example, using the Save or SaveAs methods) or implicitly (for example, in response to a prompt when closing the item's inspector)."

Eclipse plugin-Control closing of editor

What I am cunrrently doing is while closing the editor in doSave method I check for a condition If it is satisfied I allow super.doSave() be called else I display a dialog box displaying the error message.
Now I want to instead ask user again if he want to save it again and if he says yes he could save the wrong file or say no to edit it but in either case editor should not close.
However currently after the error it closes.
If your editor part implements ISaveablePart2 Eclipse will call the method:
public int promptToSaveOnClose();
to prompt for saving when closing. You can respond with ISaveablePart2.CANCEL to cancel the close, ISaveablePart2.YES to continue the save (by calling doSave), ISaveablePart2.NO to skip the save, or ISaveablePart2.DEFAULT to use the normal behavior.

excel background save via 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.

vb.net add text to form without interaction

I have a winform project which lists all the files in a specified folder. It allows the user to select a new destination for each file, and when the user has chosen the destinations for all files that he would like to be moved, it moves the files, one by one.
My next step is, I need to display a confirm form when the files are being moved, and add each file's name and destination to the confirm form as it is being moved.
My question is:
How can I add more text to the confirm form's controls after I already loaded it (using confirm.showdialog() from my other form, without any user interaction? I imagine that I need to do it from the original form, because it needs to display each one when it starts to move that file, but I'm open to any suggestions:)
TIA
Both above answers are good.
If I understand correctly, your main form will allow one to select multiple files, then select their destination and launch the move process. If that's what you need, I would simply do the following:
Create a new form that would report the process to the user, without requiring any interaction, but just to inform the user what file is being moved;
Create an instance of a BackgroundWorker object, and call the file-move method from the BackgroundWorker.DoWork() method (within your main form);
Flag your BackgroundWorker to report progress, then call the BackgroundWorker.ReportProgress() event handler from within your move-file method;
Use the previously created list of file names to get its name and report it to your file-move dialog form while the file is being changed. A simple DataBinding over a Label should do the trick while you'll move your CurrencyManager to the next item within the list, or you could simpler use the list indexer to get the filename at a particular index;
When the user launches the move process, get your filenames and and count them, then set your ProgressBar Maximum value to the number of files you have.
The BackgroundWorker.ReportProgress() method takes an integer value as its argument, then, with your ProgressChanged() event handler, you will be able to display the name of the file being copied to your window by getting the filename at the index location, index given by your ReportProgress() method.
This will allow you to use the performance of a supplemental thread, without "freezing" your main thread from which your form has been created, then you will be able to simultaneously perform your file move, and display the filename to the progress-form, while illustrating to your user visually what the progress is about through your ProgressBar control, and displaying the filename as required.
Does this help?
Wouldn't it be simpler to make another form instead of using preset dialogs?
that way you can just have a textbox that you populate and refresh
You could do put all of the code to show the moving of the files inside the confirm dialog window.
But to give a more complete solution could you tell me how you are gathering the file moving information.
A Good solution would be to just pass in the list of the files to be moved and then perform the moving function in the dialog.