Creating event for Word - vba

Here is a link to, how to create event in Microsoft Word.
https://learn.microsoft.com/en-us/office/vba/word/concepts/objects-properties-methods/using-events-with-the-application-object-word
I created my own class module, and my own code in procedure.
When I try Set X.App = Word.Application
I get
Run-time error '424' Object required
X is declared in dim before App is declared in class module.

In your class module code write this:
Option Explicit
Public WithEvents App As Application
Private Sub App_DocumentChange()
MsgBox "App_DocumentChange - active document has been changed."
End Sub
Private Sub Class_Initialize()
MsgBox "Class Initialize"
End Sub
Name this class module as EventClassModule
In your Module1 code write:
Dim X As New EventClassModule
Sub Register_Event_Handler()
Set X.App = Application
End Sub
Now, if you run sub Register_Event_Handler you should see messagebox "Class Initialize". Note that App_DocumentChange event occurs when you change active document for other, not when you change the content of active document.

Related

Outlook VBA event handler not called

I have the following ThisOutlookSession in Outlook:
Public Sub Application_Startup()
Call GetItemsFolderPath.Initialize
End Sub
And the following GetItemsFolderPath class module:
Public WithEvents myOlExp As Outlook.Explorer
Public Sub Initialize()
Set myOlExp = Application.ActiveExplorer
End Sub
Private Sub myOlExp_SelectionChange()
MsgBox "Hello, world"
End Sub
I'm basically following the docs from https://learn.microsoft.com/en-ca/office/vba/api/Outlook.Explorer.SelectionChange
The code compiles BUT it never shows the MsgBox
Restarting Outlook so Application_Startup is called didn't work
Manually executing the Application_Startup macro didn't help either
Any ideas - what am I doing wrong?
When adding Dim GetItemsFolderPath As New GetItemsFolderPath in global space, the code work as it should, displaying the messagebox when you switch folder in Outlook.
ThisOutlookSession:
Option Explicit
Dim GetItemsFolderPath As New GetItemsFolderPath 'Instantiate the class
'***************************************************
'* Outlook start
'*
Public Sub Application_Startup()
Call GetItemsFolderPath.Initialize
End Sub
Class module GetItemsFolderPath:
Option Explicit
Public WithEvents myOlExp As Outlook.Explorer
Public Sub Initialize()
Set myOlExp = Application.ActiveExplorer
End Sub
Private Sub myOlExp_SelectionChange()
MsgBox "Hello, world"
End Sub
I ran into the following problem:
https://learn.microsoft.com/en-us/outlook/troubleshoot/deployment/macros-in-this-project-disabled-outlook

VBA get instance name inside of class

Sub Main()
Dim test As New Class1
End sub
Class1:
Private Sub Class_Initialize()
msgbox(Name_of_Class_Instance)
End Sub
I want the msgbox to show "Test"
Can that be done in VBA?
As per my understanding, you are trying to get the class instance name which is declared in module.
First of all you need to do is create a class like below.
Public classname As String
'Below method is going to get the value from module.
Public Sub Class_Initialize()
MsgBox classname
End Sub
Second, create a module like below.
Sub getname()
Dim test As Class1
Set test = New Class1
'Here we are passing the class name as 'test' and executing the 'Class_Initialize' method
With test
.classname = "test"
.Class_Initialize
End With
End Sub
Now you will get the instance name of the class.

Calling OnTime method in a Custom Class

I'm trying to use the .OnTime method in a class module, but can't figure out how to call a procedure in the class. All of the .OnTime examples I've seen refer to using the method from a standard code module rather than a custom class. Is there any way of calling a procedure in the class module rather than a standard code module?
#Alex P: Updated to include code. Here is the Class Module:
Option Explicit
Public Sub Test()
MsgBox "Success"
End Sub
Private Sub Class_Initialize()
Application.OnTime EarliestTime:=Now + TimeValue("00:00:03"), _
Procedure:="Test"
End Sub
And the Standard Module:
Option Explicit
Public Sub TestOnTime()
Dim OnTime As CCOnTime
Set OnTime = New CCOnTime
End Sub
I've also tried Procedure:="CClass.Test"
You can do it, but the call-back needs to be bounced back into the object from a Standard Module or a Worksheet Module or Thisworkbook.
Here is an example that pulses a value in a worksheet cell.
The timer is (almost) encapsulated in the cOnTime Class.
A cOnTime Object is instantiated in the host worksheet, whose code module can have a property to set the pulse time as well as the call-back routine.
If you protect the sheet, it will start pulsing and you can stop it by un-protecting the sheet.
If you navigate away from the host sheet, the timer is killed and if you navigate back it re-starts (as long as the sheet is protected).
Class cOnTime
Option Explicit
Const DEFPulseTime = "PulseTime"
Const DEFearliestTime As Long = 5
Const DEFlatestTime As Long = 15
Public WithEvents wb As Workbook
Public ws As Worksheet
Private DoWhen As String
Public mPulseTime As Long
Public mNextTime As Double
Property Let callBackDoWhen(cb As String)
DoWhen = "'" & wb.Name & "'!" & ws.CodeName & "." & cb 'e.g. 'wb Name.xlsm'!Sheet1.kickdog
End Property
Private Function PulseTime() As Long
On Error Resume Next
PulseTime = CallByName(ws, DEFPulseTime, VbGet)
If Err.number <> 0 Then
PulseTime = DEFearliestTime
End If
On Error GoTo 0
End Function
Property Get designMode() As Boolean
designMode = Not ws.ProtectContents
End Property
Public Sub kickDog()
Const myName As String = "kickDog"
Dim psMessage As String
If ws Is ActiveSheet And Not designMode Then
mNextTime = Now + TimeSerial(0, 0, mPulseTime)
On Error Resume Next
Application.OnTime mNextTime, DoWhen
On Error GoTo 0
End If
Exit Sub
End Sub
Public Sub killDog()
If ws Is Nothing Or mNextTime = 0 Then Exit Sub
On Error Resume Next
Application.OnTime mNextTime, DoWhen, , False
On Error GoTo 0
End Sub
Private Sub Class_Initialize()
Dim errorContext As String
On Error GoTo enableAndExit
Set wb = ActiveWorkbook
Set ws = ActiveSheet
On Error GoTo 0
callBackDoWhen = DEFDoWhen
callBackPulseTime = DEFPulseTime
mPulseTime = PulseTime
kickDog
Exit Sub
enableAndExit:
If Err <> 0 Then
If ws Is Nothing Then
errorContext = "ws"
ElseIf wb Is Nothing Then
errorContext = "wb"
End If
End If
End Sub
Private Sub Class_Terminate()
Const myName As String = "Class_Terminate"
On Error Resume Next
killDog
Set ws = Nothing
Set wb = Nothing
Exit Sub
End Sub
Private Sub wb_WindowActivate(ByVal Wn As Window)
wb_Open
End Sub
Private Sub wb_WindowDeactivate(ByVal Wn As Window)
killDog
End Sub
Private Sub wb_BeforeClose(Cancel As Boolean)
killDog
End Sub
Private Sub wb_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI Then killDog
End Sub
In Worksheet Module
Option Explicit
Const cPulseTime As Long = 1
Dim mOnTime As cOnTime
Property Get PulseTime() As Long
PulseTime = cPulseTime
End Property
'****************************************
'Timer call-back for cOnTime
Public Sub kickDog()
' Code to execute on timer event
'******************************************
On Error Resume Next
Me.Cells(1, 1) = Not Me.Cells(1, 1)
On Error GoTo 0
'******************************************
Debug.Print "woof!!"
mOnTime.kickDog
End Sub
Private Sub Worksheet_Activate()
Me.Cells(1,1) = False
Set mOnTime = New cOnTime
End Sub
Private Sub Worksheet_Deactivate()
On Error Resume Next
Set mOnTime = Nothing
End Sub
You are asking for magic - VBA is a comprehensive tool but it is not magic.
The reason is that every Class module is simply a template which can be instantiated any number of times in the application code. Excel could not hope to correctly guess which particular instantiation of the Class module is the correct one on which to invoke the method. You are responsible for making this decision and managing the references to the appropriate Class instance.
Ah you say - But there is no private data/references being used by the method I want called. It is a static method. Well the answer to that is that VBA does not support static methods on Class modules, only on Standard modules. Any method that you wish to declare to the environment as being static is declared as being static by being included in a standard module.
So, place your call-back method in a Standard module, and declare a private member that holds a reference to the particular instance of the Class that you wish to handle the event.
One alternative route which isn't specified by others here but, I think, would theoretically work is:
Register VBA object as an active object with a specified GUID
Launch a PowerShell (or binary) daemon to connect to the object with specified GUID and call a specified method on that object every n seconds.
Pros:
Totally encapsulated
Cons:
Launches external process
Method likely has to be public (unless connection points can be abused)
Potentially crash-prone
I haven't implemented such a solution yet but intend to on stdCOM of stdVBA library if everything works out.

DocumentBeforeClose syntax

I really don't know the syntax of the DocumentBeforeClose event. Following this page, I should create a class module called 'EventClassModule' (see also this article). So I did. Then I copied this piece of code (from the example of the first link) into that (class)module:
Public WithEvents appWord as Word.Application
Private Sub appWord_DocumentBeforeClose _
(ByVal Doc As Document, _
Cancel As Boolean)
Dim intResponse As Integer
intResponse = MsgBox("Do you really " _
& "want to close the document?", _
vbYesNo)
If intResponse = vbNo Then Cancel = True
End Sub
And finally I put this in a normal module, and executed it:
Dim X As New EventClassModule
Sub Register_Event_Handler()
Set X.App = Word.Application
End Sub
What does the 'X' means in this case, and what am I doing wrong? There is no event executed when I close the document now.
X is an instance of the class you created (EventClassModule)
Your problem is that .App is not a property of EventClassModule. Change
Set X.App = Word.Application
to the property you defined in your class
Set X.appWord = Word.Application
I tried the same thing! Actually it works for me. I have this in a class module named EventClassModule
Public WithEvents appWord As Word.Application
Private Sub appWord_DocumentBeforeClose _
(ByVal Doc As Document, _
Cancel As Boolean)
'Here is the code you want to do before it close
MsgBox "WORKING!"
End Sub
And in a module (not a class module) I have this
Dim X As New EventClassModule
Sub AutoExec()
'Call any other sub or function you want
Call Register_Event_Handler
End Sub
Sub Register_Event_Handler()
Set X.appWord = Word.Application
End Sub
AutoExec is called as soon as the document is loaded. So it calls the sub Register_Event_Handler to register the object X (which is a object EventClassModule, the class module created). So X will be annonced that the document is about to close
hope it helps!

Variable Protection Level in Script

I am creating a script programatically. However I cannot access the dataset from the modeule. See "Sample" below. ('ds' is not declared. It may be inaccessible due to its protection level.)
In the Sub main() I can access ds without a problem
In the Sub test() in Cacl_Module I cannot access ds.
Namespace Evaluator
Public Class Evaluator
Public ds As New DataSet
Public ComboBox1 As New ComboBox
Public CheckBox1 As New CheckBox
Public TextBox1 As New TextBox
Sub main()
Debug.Print(ds.Tables("Table1").Rows.Count)
Debug.Print(CheckBox1.Checked)
End Sub
End Class
Public Module Calc_module
Sub test()
Debug.Print(ds.Tables("Table1").Rows.Count)
Debug.Print(CheckBox1.Checked)
End Sub
End Module
End Namespace
That's because ds is a member of the Evaluator class. In the Main method of the Evaluator class, you can access its members. But in the test method of the Calc_Module, you don't have an instance of Evaluator. You should create one, like that:
Public Module Calc_module
Sub test()
Dim MyEvaluator as new Evaluator()
Debug.Print(MyEvaluator.ds.Tables("Table1").Rows.Count)
Debug.Print(MyEvaluator.CheckBox1.Checked)
End Sub
End Module
Or even better (no need to duplicate code)
Public Module Calc_module
Sub test()
Dim MyEvaluator as new Evaluator()
MyEvaluator.main()
End Sub
End Module