PDA

View Full Version : Variable scope



OldRelic115
08-03-2017, 08:34 PM
I'm a beginner, attempting a rather simple little project in Outlook 2016, but I seem to be confused about variable scope. In the ThisOutlookSession module I have a Public variable declaration before the first procedure. (Public Abort as Boolean)

Here is a little snippet from my Application.ItemSend procedure:

Abort = False
frmChoose.Show
MsgBox Abort

In the module for that form an event procedure changes Abort = True then Unloads the Form.

Abort = True
MsgBox "in form module: " & Abort
Unload Me

When it comes back to the ItemSend procedure the MsgBox indicates that Abort is still false.

I must be overlooking something fundamental here but can't figure it out. Any suggestion is appreciated.

Bob Phillips
08-04-2017, 03:59 AM
Can you post the whole code, what you are doing seems okay, but the bigger picture would help.

OldRelic115
08-04-2017, 08:37 AM
Can you post the whole code, what you are doing seems okay, but the bigger picture would help.

So far, this is all the farther I've gotten...

This code is from the ThisOutlookSession module-

Public strSendAccount
Public Abort As Boolean

Sub Application_ItemSend _
(ByVal Item As Object, Cancel As Boolean)
Dim strMsg As String
Dim res As Long

Abort = False
frmChoose.Show
MsgBox Abort
If Abort = True Then
Cancel = True
Exit Sub
End If


If Item.Subject = "" Then

Cancel = True
strMsg = "Please fill in the subject before sending."
MsgBox strMsg, _
vbExclamation + vbSystemModal, "Missing Subject"
Item.Display
End If
End Sub

This is from the form module-

Sub btnCancel_Click()

Abort = True
MsgBox "in form module: " & Abort
Unload Me

End Sub

Private Sub btnSend_Click()

If Me.ListBox1 = "" Then
Abort = True
End If
strSendAccount = Me.ListBox1
Unload Me

End Sub

Private Sub UserForm_Activate()

Dim oAccount As Outlook.Account

For Each oAccount In Application.Session.Accounts
ListBox1.AddItem oAccount
Next

End Sub

OldRelic115
08-05-2017, 04:57 AM
Well...
I've learned a little bit since the above post. It is only the module in ThisOutlookSession that doesn't share Public declared variables. Other modules, including the form module all see Public variables.
I am using Outlook 2016 on Windows 10. I don't know if that is a factor but there is no doubt that this is the behavior on my system