PDA

View Full Version : Pass public variable to Sub



vbmalik12345
03-31-2019, 11:20 AM
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

gmayor
04-01-2019, 12:39 AM
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.

vbmalik12345
04-01-2019, 10:30 AM
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...

Paul_Hossler
04-01-2019, 12:17 PM
I think all you need to do is insert a third Standard Module with just the one line:


Public strSubject As String

vbmalik12345
04-01-2019, 12:30 PM
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....

Paul_Hossler
04-01-2019, 02:50 PM
I was thinking something like this.

23976

gmayor
04-01-2019, 08:43 PM
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 Subwithout 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 Subgoes in the Userform code. The userform will then show when you click 'Send' on a message.

Paul_Hossler
04-02-2019, 05:13 AM
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

vbmalik12345
04-02-2019, 09:18 AM
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