none
(outlook vba)How to use public sub in thisoutlooksession? RRS feed

  • Question

  • belows are my ThisOutlookSession code

              

    'In the userform's code or make Public and put in a Module
    Private Declare Function FindWindow Lib "user32" _
      Alias "FindWindowA" (ByVal lpClassName As String, _
      ByVal lpWindowName As String) As Long

    Private Declare Function BringWindowToTop Lib "user32" _
    (ByVal hWnd As Long) As Long


    Dim WithEvents exp As Explorer
    Dim WithEvents mail As MailItem


    Private Sub Application_Startup()
        Set exp = Application.ActiveExplorer
    End Sub

    Private Sub exp_SelectionChange()
        If exp.Selection.Count = 1 Then
          If TypeName(exp.Selection.Item(1)) = "MailItem" Then
            Set mail = exp.Selection.Item(1)
          End If
        End If
    End Sub


    Private Sub mail_PropertyChange(ByVal Name As String)
        '//If Name = "UnRead" And mail.UnRead = False Then
        If mail.UnRead = False Then
            Dim T As Date
            Dim NextTime As Date
            T = Now
            NextTime = T + TimeValue("00:00:01")
            Do While Now < NextTime
                DoEvents
            Loop

            UserForm1.Show vbModeless
        End If



    'In a Sub
    Dim hWnd As Long
    hWnd = FindWindow("ThunderDFrame", UserForm1.Caption)
        If hWnd Then
              'AppActivate UserForm1.Caption
              'UserForm1.TextBox1.SetFocus
              'UserForm1.Show
              BringWindowToTop hWnd
        End If

    mail.GetInspector.Activate

    End Sub

    Public Sub maildisplay()
        mail.Display
    End Sub

    I want to use "mail" object in userform so I declared public sub in thisoutlooksession (maildisplay)

    and in userform I use that sub with this code

    Private Sub UserForm_Click()
        Call maildisplay
    End Sub

    but there are error message : theres no sub like that.

    How can I solve this problem??

    Saturday, February 24, 2018 2:32 AM

All replies

  • Hello Truth-,

    For calling public sub in ThisOutlookSession, you could call it like this.

    Call ThisOutlookSession.maildisplay

    Please note it will also cause error since there is no mail object in UserForm model. You could dim mail as public and call the mail in maildisplay() like below to make it work.

    Public WithEvents exp As Explorer
    Public WithEvents mail As MailItem
    
    
    
    
    
    Public Sub maildisplay()
        ThisOutlookSession.mail.Display
    End Sub

    Besides, you said what you want is to use "mail" object in usefrom, you could also call it directly after declaring it as public.

    Private Sub UserForm_Click()
    MsgBox ThisOutlookSession.mail.Subject
    End Sub

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 26, 2018 2:34 AM