gmaxey
12-15-2013, 12:21 PM
I'm stumped by the behavior of a module level variable. It works fine is the template, but when I create new documents from the template, the variable is for some reason falling out of scope.
Simple to duplicate. Create a new template. In the ThisDocument module of the template, paste the following code:
Option Explicit
Private m_oCCTest As ContentControl
Private Sub Document_ContentControlOnEnter(ByVal oCC_Entered As ContentControl)
'Set the module level CC variable to the CC entered.
Set m_oCCTest = oCC_Entered
If Not m_oCCTest Is Nothing Then
MsgBox "A CC was entered and the variable m_oCCTest is set"
End If
Application.OnTime Now + TimeSerial(0, 0, 0.1), "CCRunTest"
lbl_Exit:
Exit Sub
End Sub
Public Sub CCRunTest()
On Error Resume Next
MsgBox "Running test. The Test CC text is: " & m_oCCTest.Range.Text
If Err.Number <> 0 Then
MsgBox "m_oCCTest is out of scope. Why?"
End If
End Sub
Add a few content controls, observe the message boxes while changing the text. No problems.
Now save and close the template and create a new document from it.
For some reason the module level variable is being set, but it is now out of scope in the CCRunTest procedure?
Can anyone explain why? Thanks
Simple to duplicate. Create a new template. In the ThisDocument module of the template, paste the following code:
Option Explicit
Private m_oCCTest As ContentControl
Private Sub Document_ContentControlOnEnter(ByVal oCC_Entered As ContentControl)
'Set the module level CC variable to the CC entered.
Set m_oCCTest = oCC_Entered
If Not m_oCCTest Is Nothing Then
MsgBox "A CC was entered and the variable m_oCCTest is set"
End If
Application.OnTime Now + TimeSerial(0, 0, 0.1), "CCRunTest"
lbl_Exit:
Exit Sub
End Sub
Public Sub CCRunTest()
On Error Resume Next
MsgBox "Running test. The Test CC text is: " & m_oCCTest.Range.Text
If Err.Number <> 0 Then
MsgBox "m_oCCTest is out of scope. Why?"
End If
End Sub
Add a few content controls, observe the message boxes while changing the text. No problems.
Now save and close the template and create a new document from it.
For some reason the module level variable is being set, but it is now out of scope in the CCRunTest procedure?
Can anyone explain why? Thanks