Consulting

Results 1 to 4 of 4

Thread: Variable scope

  1. #1

    Thumbs up Variable scope

    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.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Can you post the whole code, what you are doing seems okay, but the bigger picture would help.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Quote Originally Posted by xld View Post
    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

  4. #4
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •