There is some great information HERE about making PowerPoint respond to events, however the caveat is unfortunately:
An Event handler cannot be set automatically. To set an event handler when PowerPoint starts up you still need to rely on the Auto_Open macro of an add-in to instantiate the event handler.
I am not sure if that is a suitable solution for your needs. Working with PPT Add-Ins is kind of a pain in the butt.
The code for the Add-In (untested, but copied mostly from existing Add-In that I have used) should be something like this, in an ordinary module, include the Auto_Open
routine, your EveryDayAccidents
routine (NOTE I modified it with some error-trapping), and two more (TrapEvents
and ReleaseTrap
) required by the event handler:
Option Explicit
'#################
'Creates a new class object from cEventClass module
Public cPPTObject As New cEventClass
Public TrapFlag As Boolean
'Public TrapFlag As Boolean
Sub Auto_Open()
'Call on the TrapEvents to instantiate the event handler
MsgBox "Auto_Open"
TrapEvents
End Sub
Sub TrapEvents()
If TrapFlag = True Then
MsgBox "Relax, my friend, the EventHandler is already active.", vbInformation + vbOKOnly, "PowerPoint Event Handler Example"
Exit Sub
End If
'## Instantiate our class object event handler
Set cPPTObject.PPTEvent = Application
TrapFlag = True
End Sub
Sub ReleaseTrap()
If TrapFlag = True Then
Set cPPTObject.PPTEvent = Nothing
Set cPPTObject = Nothing
TrapFlag = False
End If
End Sub
Sub EveryDayAccidents()
Dim injdate As String
Dim lastdate As String
Dim injfree As Integer
Dim BnrMsg As String
'This Macro defines the latest injury date
injdate = InputBox("Please enter last injury date in this format: dd/mm/yyyy")
lastdate = injdate
On Error GoTo InvalidDate
injfree = DateDiff("d", CDate(injdate), Now)
On Error GoTo 0
BnrMsg = injfree
ActivePresentation.Slides(3).Shapes("Accidents").TextFrame.TextRange = BnrMsg
Exit Sub
InvalidDate:
If MsgBox("You have entered an invalid date, try again?", vbOKCancel, "Invalid Date!") = vbOK Then
Err.Clear
GoTo Retry
End If
End Sub
Then, also create a class module named cEventClass, and in that module put the following code:
Option Explicit
Public WithEvents PPTEvent As Application
Private Sub PPTEvent_PresentationOpen(ByVal Pres As Presentation)
'## Only run it on a particular filename:
'## Modify this line to reflect the presentation you need to run this on.
If Pres.Name = "MyPresentation.pptx" Then
'Call your procedure:
EveryDayAccidents
End If
End Sub
You will need to save as PPAM file type, and install the add-in. After the add-in is installed, PPTEvent_PresentationOpen
will run every time the user opens a PPT file, and will call the procedure EveryDayAccidents
if the filename is correct.
The Add-in file then becomes read-only, and you will not be able to debug errors in it without making a registry hack (google it). You will never be able to "Save As" from the PPAM file so I recommend always keeping a copy of PPTM version that you can use to debug, if needed. Any user expected to open this file will need to have the add-in installed for it to work as expected. so, like I said, working with Add-Ins is kind of a pain in the butt in PowerPoint and developing/debugging them is a fairly advanced exercise.
Good luck!
As for the remainder of your questions:
Will the text box then update when the date changes naturally
No. Do you mean "Would it prompt the user for input at regularly scheduled intervals?" If so, you could probably use windows task scheduler, or maybe Application.OnTime
to run the routine at specific intervals.
will something need to be running in the background to update the text box?
Something will need to be running in the background, either task scheduler or the presentation running with an Application.OnTime
assignment.