Delay in closing Powerpoint while doing close via VBA Macro - vba

I have two macros in in a Powerpoint file in Microsoft Office Professional Plus 2016 version.
1. Does saveas work
2. Closes Powerpoint
I run these two macros one by one using PowerPoint /m command line option. It's working fine. However, few times 2 macro close call is intermittently taking more time. When I move mouse on PowerPoint window after "close" macro is called, it closes instantly. I am pretty sure first macro saved the file and finished execution successfully. The close call macro code is below. I don't want to save any work. I don't have any other Powerpoint process running. Please advice how can I speed up this? I wonder what's holding Powerpoint. Thank you.
Sub closeit()
With Application.ActivePresentation
.Saved = True
.Close
End With
Application.Quit
End Sub

The delay in macro 2's execution is most likely caused by macro 1 finishing the save. It's not possible to run two macros at once, so 2 can't run until 1 is done.
What's the logic behind two separate macros? It seems unneccessarily complicated, you could just add this code to the end of #1.

Related

Closing any open userform

After a good search and some (over)thinking I came to the conclusion that I have no answer on what seems to be a simple question.
I have an excel document with many (20+) userforms in it. If you press a button (that is not in the userform, but just on the excel sheet) to start over again it should close any userform that's open at that moment.
I tried with unload me but of course I got an error when there wasn't any open userform.
Then I tried to add on error resume next thinking it would skip the line if there was no userform and therefore not giving an error but just continue what I want it to do. (opening a new userform).
It did indeed not give me the error anymore but it doesn't close any open userform as well (when there is one open).
So here I am, hoping someone here can help me as I don't know what to do. I could list up all of the userforms I suppose but it should be possible to go faster and automatically I suppose?
Some more info: It is never possible to have more than one userform open at the same time. // The button I want to create closes all the userforms if there are any and leads the user back to the main menu.
Thanks in advance!
KawaRu
Try calling the following when you want to unload all forms
Sub UnloadAllForms(Optional dummyVariable As Byte)
'Unloads all open user forms
Dim i As Long
For i = VBA.UserForms.Count - 1 To 0 Step -1
Unload VBA.UserForms(i)
Next
End Sub
This is hopefully the worst code that I have written in the last 5 years, but it will close any breathing form in Excel that you may have (and will kill any variables etc) :
Public Sub CloseAll()
End
End Sub
Use with caution!
From the MSDN End Statement:
The End statement stops code execution abruptly, without invoking the Unload, QueryUnload, or Terminate event, or any other Visual Basic code.
Code you have placed in the Unload, QueryUnload, and Terminate events offorms and class modules is not executed.
Objects created from class modules are destroyed, files opened using the Open statement are closed, and memory used by your program is freed.
Object references held by other programs are invalidated.
The End statement provides a way to force your program to halt. For normal termination of a Visual Basic program, you should unload all forms.
Your program closes as soon as there are no other programs holding references to objects created from your public class modules and no code executing.

Microsoft Excel 2013 ScreenUpdating fails

After running a macro for a prolonged period of time the ScreenUpdating in Excel totally stops working even when the macro finishes. I never set it to false but it behaves as if it is set to false and the only way to make Excel update the screen again is to close it out and reopen it. The VBA screen works however. I have tried to run DoEvents and ScreenUpdating=True but the Excel window does not update visually (it is not frozen). Is there anyway of clearing the ScreenUpdating buffer? I have tried manually setting it to false and true to no avail.
If you want to duplicate this error use this code
Application.OnTime (Now + TimeValue("0:00:05")), "macroName", (Now + TimeValue("0:00:10")), True
then I have the code copy and paste some values and I control when the loop stops with a countdown value in cell A1 for example.
I got this issue in my Excel 2013. For me, it was caused when I loaded a userform. It was very, very strange, however. After a lot of trial and error I found that what caused it was using a Sheets("MySheet").Activate command. I moved this command from running after I had shown and closed the form, to immeiately before I show the form (i.e., immediately when user clicks the 'show form' button.) This fixed it. Try fiddling with your sheets.activate commands.

Excel VBA display dialog locked in front of screen during macro

I've looked for this everywhere in vain.
I have a macro in one Excel file (SplitTickets.xlsm) that loops through 3 separate Excel files.
When SplitTickets is opened a dialog appears with a button to launch the macro.
Since I don't want users to mess with the excel with accidental keys or clicks while the macro is running, I would like to keep the dialog on top of all the files being opened, modified and closed, and update the label displayed when the process is over.
I tried setting it up as modal but since the macro needs to select sheets, cells and ranges, the dialog being modal blocks the rest of the macro being executed.
And turning modal off makes it disappear beneath the flurry of the macro's manipulations.
How can I keep this dialog on top without turning it modal?
You can use the below code instead. I use it on regular basis when I don't want the user to interfere with the code run.
Sub Testing()
Application.Screenupdating = False
Your Code
'This is very important, or it will remain disabled.
Application.Screenupdating = true
End sub
This way the user is not aware of what is happening in the background.
Sandesh :)
Add the following line to the start of your macro:
Application.ScreenUpdating = False
This will turn off the "flurry of macro" activity you can see, so the users aren't tempted to interfere. It will also make your macro run faster, because it no longer has to redraw the screen between each step. Be sure to set it back to True at the end of the macro.
You may also want to use this option as well:
Application.Cursor = xlWait
This will turn the mouse cursor to an hourglass (or whatever "wait" cursor is set in their system) so the user knows the macro is running. To disable the hourglass, use this:
Application.Cursor = xlDefault
Beyond this, the macro should automatically prevent the user from interfering with the sheets while it is running. However, it will keep a queue of the keystrokes made while the macro is running and enter them after the macro is done.
Could you add a Formname.Show at the end of you macro to make it reappear?

Excel OnTime Events Scheduler

I need to run my excel VBA daily at a specific time. I google the ontime method, however I dont get a comprehensive understanding from it. I would bring up this to clean up my confusion of ontime method.
Does the containing excel workbook have to be open for ontime method before it runs. If yes, Is there a way to open the excel workbook automatically at a specific time.I know it mite be done by Timer or a Windows task Scheduler.
Could someone work me through this. Is my current code properly constructed for automated task scheduler?
My current code looks like this:
Sub StartTimer()
Application.OnTime EarliestTime:=TimeValue("11:15:00"), Procedure:="rune", _
Schedule:=True
End Sub
Sub rune()
SourceOneUpdate
SourceTwoUpdate
SourceThreeUpdate
GenerateReport
End Sub
Private Sub workbook_open()
StartTimer
End Sub
This is based on the idea from this post: http://www.cpearson.com/excel/OnTime.aspx which mite be helpful. Even this workbook is open, its not running automatically. Could someone help me on this to see why this is not working properly.
Thanks in advance.
just to expand on d-stroyer's comment.
You first need to make sure your macro setting is enabled at all times because this will ensure that everytime your workbook opens, the macro runs without any notification or confirmation.
To do this,
Excel Options > Trust Center > Trust Center Settings (Button) > Macro Settings > Enable All macros > OK
Now that your macro is enabled, you need to ensure the macro will run as soon as the workbook opens. So, go to your VB editor and open the ThisWorkbook module > Create a workbook_open() event and copy and paste your "OnTime code" into workbook_open event > Save & Close
Now, everytime you open the workbook, the workbook should run the desired code at 8:47.
Go to Task Scheduler, on the right click on "Create Basic Task...". Give the task a name and click next. Select a trigger (Daily in the case you mentioned here) and click next. Set the time and recurrence period and click next. In the Action, select "Start a program" and click next. In the Program/script text box browse for your Excel file and click next (leave the other text boxes empty). Click on Finish. I just tried this on my PC now, and it works.
From Windows Help Forum
PS: Make sure you set the scheduler to open the excel file BEFORE 8:47 (so maybe 8:46).
Cheers,
kpark
EDIT: try running this to see if your OnTime is working..
Sub RunOnTime()
Application.OnTime Now + TimeSerial(0, 0, 10), "theSub"
End Sub

Excel 2007 VBA Workbook closing then reopening

In 2003 the workbook would just close. But now the same code is re-opening the workbook. There are some lines afterwards and then the sub ends. When the sub ends, the workbook_open event gets fired for the closed workbook, even though there is no code that opens any workbooks. The debugger is almost useless, it is not reproducing the bug if I execute everything step by step, in fact everything works fine when I use the debugger.
I use
ActiveWorkbook.Close False
For closing the workbook (from an add-in).
Help much appreciated.
I added a slight amount of code for Excel 2007 because of the annoying ribbon and Microsoft getting rid of custom menus. To somewhat compensate for this, I wanted to have the add-ins tab in the ribbon always visible for convenience, but I was using Application.OnTime and sendkeys to do it (because Microsoft didn't bother including an API with the stupid ribbon...). Well OnTime seems to have been the root of my troubles.
Figured out how to get both. Workbook_beforeclose cancels the scheduled ontime event by calling:
Application.OnTime EarliestTime:=Now(), Procedure:="Name", Schedule:=False
but otherwise the ontime event gets executed.
If the event does not get cancelled before beforeclose is called, the workbook will close then re-open to run the scheduled ontime event (because the workbook is opened so briefly that ontime doesn't get a chance to run before I need to close it).