Paste into the standard module
Sub ProcedureAAA() Dim oModul As Object
'Simulation of interference in the VBA project
Call AddModule
Call AddProcedure
'version 1
'calling other procedures by OnTime
'(run the procedure in a different thread than the current one)
Application.OnTime Now, "ProcedureBBB"
'version 2
'run the procedure in the same thread
'Application.Run "ProcedureBBB"
MsgBox "End of ProcedureAAA"
End Sub
Sub AddModule()
Const vbext_ct_StdModule As Long = 1
ThisWorkbook.VBProject.VBComponents.Add vbext_ct_StdModule
End Sub
Sub AddProcedure()
Dim VBCodeMod As Object 'CodeModule
Dim LineNum As Long
With ThisWorkbook.VBProject
Set VBCodeMod = .VBComponents(.VBComponents.Count).CodeModule
End With
With VBCodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, _
"Sub ProcedureBBB()" & Chr(13) & _
" Msgbox ""Hello world!"", vbInformation, ""ProcedureBBB""" _
& Chr(13) & "End Sub"
End With
End Sub
Watch when you get the "Hello World" message for the version from OnTime, and when from Run.
I am not convinced that I am going the right way, but without looking at the whole solution I am unable to indicate why the procedure will be started twice.
Artik