Consulting

Results 1 to 9 of 9

Thread: Pass public variable to Sub

  1. #1

    Pass public variable to Sub

    Public strSubject As String
    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    strSubject = Item.Subject
    MsgBox (strSubject)
    UserForm1.Show
    End Sub

    Private Sub ButtonOK_Click()
    MsgBox strSubject
    End Sub

    This is a test program. I want to pass the value in var strSubject to the form when i click a button.

    When i click the button it showing empty.. Could someone help how to pass a public variable to sub

  2. #2
    Your first macro goes in the ThisOutlookSession module
    The second goes in the Userform code
    The variable declaration must go in an ordinary module so it will be seen by both macros.
    Graham Mayor - MS MVP (Word)
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Quote Originally Posted by gmayor View Post
    Your first macro goes in the ThisOutlookSession module
    The second goes in the Userform code
    The variable declaration must go in an ordinary module so it will be seen by both macros.
    Thanks gmayor.... i want to capture the subject of email using the first macro... and pass it to userform code... so how can i define the variable in ordinary module.. can u provide a sample... if i create a ordinary module, will the script get triggered when a mail is sent?

    please help...

  4. #4
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    6,390
    Location
    I think all you need to do is insert a third Standard Module with just the one line:

    Public strSubject As String
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    Quote Originally Posted by Paul_Hossler View Post
    I think all you need to do is insert a third Standard Module with just the one line:

    Public strSubject As String
    Hi Paul,

    Thanks.. Im very new to this... could you please give bit more details on " insert a third Standard Module ".. whats the standard module means...

    i have added the Public in the begining of the code... any reason this not working....

  6. #6
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    6,390
    Location
    I was thinking something like this.

    Capture.JPG
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    As Paul says, you need to create a new module as he has shown and you put the line (and nothing else required) in that new module.
    The macro
    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
        strSubject = Item.Subject
        MsgBox (strSubject)
        UserForm1.Show
    End Sub
    without that line goes in the ThisOutlookSession module, shown at the top of the folder list in Paul's illustration.
    The macro
    Private Sub ButtonOK_Click()
        MsgBox strSubject
    End Sub
    goes in the Userform code. The userform will then show when you click 'Send' on a message.
    Graham Mayor - MS MVP (Word)
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  8. #8
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    6,390
    Location
    One additional comment / suggestion / personal style:

    I like to keep userform (UserForm1) and application (ThisOutlookSession) code limited to only those macro(s) that are truly part of the object, and just call any non-Object macros that I have in a standard module

    So if ButtonOK_Click() needed to update a database for example (instead of Msgbox), instead of inserting X number of lines in the event handler, I'd just call a stand alone sub in a standard module

    You can mark this thread SOLVED -- #3 in my sig
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  9. #9
    Quote Originally Posted by Paul_Hossler View Post
    One additional comment / suggestion / personal style:

    I like to keep userform (UserForm1) and application (ThisOutlookSession) code limited to only those macro(s) that are truly part of the object, and just call any non-Object macros that I have in a standard module

    So if ButtonOK_Click() needed to update a database for example (instead of Msgbox), instead of inserting X number of lines in the event handler, I'd just call a stand alone sub in a standard module

    You can mark this thread SOLVED -- #3 in my sig

    It worked... Thanks

Posting Permissions

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