I am calling an Excel macro from an Outlook rule script.
The process is: Get mail, run an Outlook rule which runs an Outlook script, open Excel from that script, run the Excel macro, close Excel.
How can I validate in the Outlook rule script that the Excel macro is done, to save and close the application?
Sub AskMeAlerts()
Dim appExcel As Excel.Application
Dim wkb As Excel.Workbook
Set appExcel = CreateObject("Excel.Application")
appExcel.Workbooks.Open ("C:\Ask me question workflow.xlsm")
appExcel.Visible = True
appExcel.Run "'Ask me question workflow.xlsm'!AskMeFlow"
appExcel.DisplayAlerts = False
appExcel.ActiveWorkbook.Save
appExcel.Quit Set appExcel = Nothing
Set wkb = Nothing
End Sub
VBA Wait is a built-in function to pause the code from executing for a specified time. It is very similar to what we do in the Sleep command. To pause a code, we use the Application. Wait method.
If you need Excel to run some VBA at a specific time, or repeatedly at set intervals, you can use the Application. OnTime method. A basic call to Ontime requires that you supply a time when you want the code to run, and the name of the macro you want to run.
You could either
The code below uses a marker in A1 of the first sheet to catch the code being run (in the Excel portion). I have also tided your code (it was a mix of early and later binding)
outlook code
Sub AskMeAlerts()
Dim appExcel As Excel.Application
Set appExcel = New Excel.Application
With appExcel
.DisplayAlerts = False
.Workbooks.Open ("C:\TEMP\Ask me question workflow.xlsm")
.Run "'Ask me question workflow.xlsm'!AskMeFlow"
If .activeworkbook.sheets(1).[a1].Value = "Complete" Then
MsgBox "Code has run"
.activeworkbook.sheets(1).[a1].Value = vbNullString
.activeworkbook.Save
.DisplayAlerts = True
.activeworkbook.Close
appExcel.Quit
Set appExcel = Nothing
End If
End With
End Sub
excel code
Sub AskMeFloW()
'do stuff
ThisWorkbook.Sheets(1).[a1] = "Complete"
End Sub
A really simple way, is by implementing a lock.
This code is a quick and dirty solution, by checking the existence of a file, in a predefined place.
in C:\Ask me question workflow.xlsm
add this sub:
Sub WrapAskMeFlow()
Dim tmpFile As String
tmpFile = "C:\AskMeFlow.tmp"
Open tmpFile for Output as #1
Close #1
AskMeFlow
Kill tmpFile
End Sub
In your outlook macro add:
Sub AskMeAlerts()
Dim appExcel As Excel.Application
Dim wkb As Excel.Workbook
Set appExcel = CreateObject("Excel.Application")
appExcel.Workbooks.Open ("C:\Ask me question workflow.xlsm")
appExcel.Visible = True
appExcel.Run "'Ask me question workflow.xlsm'!WrapAskMeFlow"
appExcel.DisplayAlerts = False
While Dir("C:\AskMeFlow.tmp")="":DoEvents:Wend
While Dir("C:\AskMeFlow.tmp")<>"":DoEvents:Wend
appExcel.ActiveWorkbook.Save
appExcel.Quit Set appExcel = Nothing
Set wkb = Nothing
End Sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With