VBA powerpoint, run timed task/macro in background across presentation - vba

I am trying to build a presentation with a counter/number in the footer which evolves with time (as a function of some external data set present in an Excel spreadsheet). I developed the VBA code for loading data from the spreadsheet, built a footer in the Slide Master and a macro for updating the counter in it (and even graph some data in the footer), but I am unable to build the mechanism for updating the counter - e.g. every 2 seconds - across the presentation (by that I mean that the update has to happen independently of the active/view slide). I've seen a few threads with similar topics [ref, ref], which were not really answered or active, hence my question: is there a way to build a background timer for launching macros in VBA/Powerpoint?
Thank you and best regards,
Cedric

Excel has an Application.OnTime event to handle this but I can't see one in Powerpoint. Not sure what your setup is but if you have an Excel sheet open as well you could use Application.OnTime from Excel to run the Macro in Powerpoint
Something like
Excel
Application.OnTime Now + TimeValue("00:00:15"), "my_Procedure"
Public Sub my_procedure()
Dim PPObj As Object
Set PPObj = CreateObject("PowerPoint.application")
PPObj.Run "myPPTFile.ppt!MyMacro"
End Sub

Related

Excel VBA timer performance differs depending on which sheet is active?

I have a workbook with many sheets, each sheet has varying number of equations that inter-link with each other. I have a workbook wide VBA timer that is supposed to run every 5 seconds.
When I am active on a sheet that doesn't have a lot of formulas on it, it seems to run exactly once every 5 seconds... However, if I then change the active sheet to a more "busy" sheet, the vba timer just doesn't go off... Or if it does go off it was like minutes later. It is not until I switch to a less busy sheet again and then the timer runs magically like normal without having to reset anything.
I do not have any special VBA code specifically on the busy sheets... and I can't see why the VBA timer code won't run consistently across all sheets? If the timer is to be affected, it should be affected across ALL sheets and not only certain ones.
Here is the VBA timer code:
Sub TimerTick()
On Error GoTo ErrorHandler
If toggletimer = True Then
RunMyCode
runWhen_ES = Now() + TimeValue("00:00:05")
Application.OnTime EarliestTime:=runWhen_ES, Procedure:="TimerTick", Schedule:=True
End If
ErrorHandler:
End Sub
The issue is Excel has a hard time running a macro and letting other processes run. Add this after you specify your wait condition:
DoEvents
Excel will release the resources and threading used for running the macro instead of churning on it the whole time the wait is going on. This should allow your spreadsheet to function normally between wait cycles.

Timeout in Excel VBA

I was wondering if it's possible to create a VBA macro in Excel, which will save the file each X minutes. I've already figured how to initialize the macro after excel startup and I found on the google something like this should pause the macro
Application.Wait(Now + TimeValue("0:10:00"))
but this will also block the user input and he cannot make any changes during that time.
This is not a anti-crash protection , the reason why I need this, is that some users are forgetting to save the document regularly...
Thank you
Francis
The 2 examples of Simoco will work great, but if you want to prevent having to deal with unnecessary saves (especially if you're working on network files, or large files), you can do a check everytime there is a change in the worksheet.
Just use the Worksheet_Change function to do that, here is a possible pattern:
Private Sub Worksheet_Change(ByVal Target As Range)
If (Now > TimeStamp)
ThisWorkbook.SaveAs BlaBlaBlaBlaBla
TimeStamp = Now + TimeValue("0:10:00")
End If
End Sub
TimeStamp needs to be a global variable defined in each workbook.
Btw, make sure that saving your file every X minutes doesn't screw with the undo / redo function of excel. I remember I had unwanted behaviors in the past when using an auto-save function.
Other thought: Google document won't require this type of macro as there is no need to save.

Where Exactly To Store VBA

We receive Excel files daily from our field offices which I have to clean and re-format (about 110 columns and 500 rows-worth) using VBA.
I need to save my VBA as a macro so we can use it to clean up all the workbook we receive by running the macro and saving the edited sheet as a new worksheet by getting the name from UserForm Combobox items.
Where exactly should I store the VBA snippets? I mean when I open the Visual Basic panel, I have these three options:
Running The Code From Microsoft Excel Object :Sheets1(Sheet1)
Running the Code From An Inserted Module
Running the Code From User Form
If I am supposed to use options 1 or 2, how can I call the UserForm for saving the sheet?
I Recomend you to use modules (Option B)
Option C goes with option B, ill explain, you can create a sub in a module in option B, then you can do:
UserForm1.show
In Option B I would writte this code, but before trying this i recomend you to understand a bit more of vba
sub ClearWBs()
'opening workbook
Workbooks.Open Filename:="c:\book1.xls"
'your code
'your code
'below code for saving and closing the workbook
Workbooks("book1.xls").Activate
ActiveWorkbook.Save
ActiveWorkbook.Close
end sub
Use Module:
If your VBA code focusses on data summarization and manipulation I suggest you use a Module.(example is what you have described in your question).
Use Form:
If what you wan't to do requires a GUI(Graphical User Interface) then you'll have to resort to Form where you can design your GUI. (example is if you have many fields that the user needs to fill-up with distinct values in which you provide the choices)
Use Excel Object:
If what you wan't to do has something to do with events happening on Worksheet and/or Workbook, like for example whenever you add sheet the macro runs, or when you open or close the workbook the macro runs then you will have to write the macro in the Excel Object.
This is basically what i have in mind, hope this helps.
If you receive files that do not contain VBA and you need to apply the same code on those files all the time then I propose that you either save that code in your personal workbook.
You can see how to do that here: http://office.microsoft.com/en-ca/excel-help/copy-your-macros-to-a-personal-macro-workbook-HA102174076.aspx
This is nice because you can also tie it to keyboard shortcut or just have it always ready for you to use.
The disadvantage is that it will only be set up per user session per computer. What you can do is have that code all set up in a module and then import it into your personal workbook if you change session or if someone else has to do it.
Once it's done, you will not have to include the module in your files your receive again.

Ending undo transaction in macro in PowerPoint

As described in http://support.microsoft.com/kb/278591 PowerPoint will usually combine all changes that a macro or add-in makes to a single undo step. So if you put the following code into VBA and execute it twice by pressing F5 there
will only be one undo step.
Sub Move()
If ActiveWindow.Selection.Type = ppSelectionShapes Then
ActiveWindow.Selection.ShapeRange.IncrementLeft 10
End If
End Sub
I am looking for a way to change this behavior in a more complex scenario where the user can make multiple changes without directly accessing PowerPoint. Ideally, it should be possible to undo a set of modifications that corresponds to one change from the user's perspective.
What I found out is that ExecuteMso seems to break the undo transaction. So if you execute the following code twice it results in 4 undo steps (first increment, ExecuteMso, second increment, ExecuteMso).
Sub Move()
If ActiveWindow.Selection.Type = ppSelectionShapes Then
ActiveWindow.Selection.ShapeRange.IncrementLeft 10
Application.CommandBars.ExecuteMso "Bold"
End If
End Sub
Anyone knows a real solution for the problem or a better workaround? Although I didn't find it maybe there is a solution for Word or Excel that can be ported to PowerPoint?
If you replace the ExecuteMso line with this one, the entire procedure remains one Undo:
ActiveWindow.Selection.ShapeRange.TextEffect.FontBold = msoTrue
Update
If you are using PowerPoint 2010 or later put this line before each block of code that you want the user to be able to undo a step at a time:
Application.StartNewUndoEntry

VBA: Waiting for Bloomberg BDP calls to finish

I have a script that imports some external data into the worksheet, which in turn affects some =BDP(...) formulas. Optimally, I'd like to do some checks on the BDP results immidiately after copying the data.
The Bloomberg Excel Add-in updates asynchronously - how do I wait for the results and then resume the script? It seems that the results are only imported after the VBA script finishes, no matter how long it runs.
Thanks in advance
Martin
I built something similar using BDH. I had to release control so that the Bloomberg add-in can go and collect the data, then resume my code. I was able to do this using Application.OnTime.
For example, if you have a 'CheckForData' function that affects the =BDP calls, and another function called 'ProcessData' that checks the results, make an aynchronous call to to 'ProcessData' within 'CheckForData', e.g.
Sub CheckForData
' Your code here
Application.OnTime Now + TimeValue("00:00:05"), "ProcessData"
End Sub
Then within 'ProcessData' perform your checks.