Results 1 to 16 of 16

Thread: Outlook 2003: How to programmitically add code to ThisOutlookSession

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Outlook 2003: How to programmitically add code to ThisOutlookSession

    Hi,
    I know it is possible to add procedures and lines to a module through VBA, however is it possible to add procedures and lines to ThisOutlookSession?

    Here is my code:

    VB Code:
    1. Public Sub cmdOK_click()
    2. Dim i As Integer
    3. Dim VBCodeMod As CodeModule
    4. Dim LineNum As Long
    5. Dim button_num As String
    6. Dim button_num_int As Integer
    7.  
    8. [COLOR=Red][B]Set VBCodeMod = VBE.ActiveVBProject.VBComponents("ThisOutlookSession").CodeModule[/B][/COLOR]
    9.    
    10.     For i = old_num_DBs To new_num_DBs
    11.         With VBCodeMod
    12.             LineNum = .ProcCountLines("Application_MAPILogonComplete", vbext_pk_Proc)
    13.             button_num = Right$(Left$(.Lines(.ProcBodyLine("Application_MAPILogonComplete", vbext_pk_Proc), 1), 18), 1)
    14.             button_num_int = CInt(button_num)
    15.             .InsertLines 1, "Public WithEvents oMnuTrackerSub" & button_num_int & " As Office.CommandBarButton"
    16.             .InsertLines .ProcBodyLine("Application_MAPILogonComplete", vbext_pk_Proc), "Dim oCBmnuTracker" & button_num_int + 1 & " As Office.CommandBarButton"
    17.             .InsertLines button_num_int + 10, "Set oCBmnuTracker" & button_num_int & " = oCBmnuAddToTrackerMain.Controls.Add(msoControlButton, 1, , , False)"
    18.             .InsertLines LineNumb, "With oCBmnuTracker" & button_num_int _
    19.                 & vbCr & "   .BeginGroup = False" _
    20.                 & vbCr & "   .Caption = " & lstDBadd.ListItems(i) _
    21.                 & vbCr & "   .Enabled = True" _
    22.                 & vbCr & "   .Style = msoControlCustom" _
    23.                 & vbCr & "   .Visible = True" _
    24.                 & vbCr & "End With" _
    25.                 & vbCr & "Set oMnuTrackerSub" & button_num_int & " = oCBmnuTracker" & button_num_int
    26.             button_added = True
    27.             'name_button_added = lstDBadd.ListItems(i)
    28.         End With
    29.     Next i
    30.  
    31. Set VBCodeMod = Nothing
    32. End Sub




    The red line is where my error occurs. Does anyone know how to reference ThisOutlookSession properly?
    Last edited by RiceRocket; Apr 25th, 2006 at 02:51 PM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Outlook 2003: How to programmitically add code to ThisOutlookSession

    I dont believe its possible as there is no option to trust programmatic access to the VBA IDE or VB Project. If it was possible it would be a large security hole too.

    It is possible in Excel as I have done that as shown in my code bank thread located here - http://www.vbforums.com/showthread.php?t=313861
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Re: Outlook 2003: How to programmitically add code to ThisOutlookSession

    Would it be possible if I called a function at the end of Application_MAPILogonComplete that added more commandbar buttons? The function would obviously be located in a std module. If this were the case, how would I reference the module?

    For example:
    VB Code:
    1. Private Sub Application_MAPILogonComplete()
    2.  
    3.                Application_MAPILogonComplete Code
    4.                Application_MAPILogonComplete Code
    5.                Application_MAPILogonComplete Code
    6.  
    7.                Call funcitonThatAddsMoreButtons
    8.  
    9. End Sub

    VB Code:
    1. Private Function functionThatAddsMoreButtons()
    2.  
    3.                Code to add buttons here
    4.  
    5. End Function

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Re: Outlook 2003: How to programmitically add code to ThisOutlookSession

    Say I have a module named "Module1", would I reference it like this?:

    VB Code:
    1. Dim VBCodeMod as CodeModule
    2.  
    3. Set VBCodeMod = Application.VBE.ActiveVBProject.VBComponents.Item("Module1")
    4.  
    5. With VBCodeMod
    6.   Blah blah blah
    7. End With

    Edit: Nevermind, this doesn't work, neither does adding ".CodeModule" on the end of my Set statement

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Re: Outlook 2003: How to programmitically add code to ThisOutlookSession

    So i guess my question remains, how to do I reference a CodeModule? In your excel example RobDog, you referenced the code module like this:

    VB Code:
    1. Set VBCodeMod = VBE.ActiveVBProject.VBE.CodePanes.Item("Module2").CodeModule

    I keep getting this error: "Object variable or With block variable not set"

    Do I need to set more references? I have VBCodeMod Dim'd as a CodeModule... what's goin on?

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Outlook 2003: How to programmitically add code to ThisOutlookSession

    Correct but Outlook doesnt have a VBE class even if you add a reference.

    to access or call a function in a module, just make the function Friend or Public.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Re: Outlook 2003: How to programmitically add code to ThisOutlookSession

    I understand how to call a function, but in order for that function to have any meaning, i need to be able to add code to that function programmatically (not through the VBE). Is there any way to do this?

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Outlook 2003: How to programmitically add code to ThisOutlookSession

    Not that I know of. The security model of Outlook is allot tighter then any of the other office apps.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Re: Outlook 2003: How to programmitically add code to ThisOutlookSession

    OK, well then let me test your skillzzzz...

    I have a menu in Outlook that I want to be dynamic to the user (ie I want the user to be able to add commandbar buttons to this menu). In order to do this, I am attempting to add code to the Application_MAPILogonComplete event of ThisOutlookSession that actually adds the commandbar buttons.

    My solution to this problem was to reference the "ThisOutlookSession" CodeModule and then .InsertLines into that module that would add commandbar buttons. - Doesn't work because security won't let you access that CodeModule

    My next solution was to reference a std module, "Module2" CodeModule, and then .InsertLines into that module that would add commandbar buttons. - Doesn't work for the same reason

    Is there anything I can do? How can i dynamically add buttons to the menu without actually referencing a CodeModule or adding code to a CodeModule?

    I can give you more details on how the user is able to add/delete commandbar buttons...

    Thanks in advance

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Outlook 2003: How to programmitically add code to ThisOutlookSession

    1. yes the MAPILoginCompolete is the best procedure for it.

    2. yes, you can do this without having to write the code dynamically.

    Think of commandbarbuttons like vb menu items. Do you know how you can dynamically add menu items at runtime in VB6? Its basically the same solution.

    You need to create a control arrray or code block that loads the items dynamically. Have one procedure to link the click event and use the .Tag property of every menu item added. They all link to the same event procedure and you can use the ,.Tag property to identify them from each other and do a select case to branch out to other code or ??? depending on the item and what you need it to do.

    Not impossible but not beginner cod either.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Re: Outlook 2003: How to programmitically add code to ThisOutlookSession

    I like the way you think, RobDog

    I will need a day or so to punch out some code as I have 900 other bugs with my program that I'm trying to fix. This definitely sounds like a challenge, but I'll certainly give it a shot and let you know how it turns out!

    Thanks again for the help!

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Outlook 2003: How to programmitically add code to ThisOutlookSession

    No prob.

    I think there is more of a challenge of trying to get the menu click to do the custom action or is there a predefined action that it will perform?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Re: Outlook 2003: How to programmitically add code to ThisOutlookSession

    Hmm I think these are custom actions. Basically the only variable that changes when the user clicks one of these buttons is the location of the database they want to access, which I have stored as a public string connectionString. So when the user clicks say CustomButton1, then the CustomButton1_click event will set connectionString to the location of database1. Depending on which button was clicked determines which database is affected. Here is my code for the button that is already on my menu:

    VB Code:
    1. Private Sub oMnuTrackerSub1_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    2. Dim oApp        As Outlook.Application
    3. Dim myExplorers As Outlook.Explorer
    4. Dim myExpSel    As Outlook.Selection
    5. Dim oEmail      As Outlook.MailItem
    6.  
    7. 'Set oApp = GetObject(, "Outlook.Application")
    8. Set myExplorers = Application.ActiveExplorer
    9. Set myExpSel = myExplorers.Selection
    10. Set oEmail = myExpSel.Item(1)
    11.  
    12. ' This is where i have hardcoded the path of a database.
    13. ' If the user adds a button, they will add a path as well,
    14. ' therefore the connectionString for each button's click event
    15. ' should hardcode the path for the database they want to access.
    16. connectionString = "C:\Documents and Settings\edmastro.AMR\My Documents\Issues database.mdb"
    17.    
    18.     Call FindOutlookEmail(oEmail.EntryID, oEmail.Subject) 'Determine if its already in tracker, set recordneedstobedeleted flag
    19.     Call arInfo.AR_Menu_Click 'Add current email (calls Outlook_Emails_2_Access)
    20. arInfo_Close:
    21.     If recordNeedsDeleting Then Call DeleteRecord   'Delete selected records in SimilarARs form
    22.    
    23.     Unload arInfo
    24.     Unload SimilarARs
    25.     Set oEmail = Nothing
    26.     Set myExplorers = Nothing
    27.     Set myExpSel = Nothing
    28. End Sub

    All of this code needs to go into to each button's click event. The only thing that should change is the connectionString.

    Does this make sense?

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Re: Outlook 2003: How to programmitically add code to ThisOutlookSession

    The connectionString is stored in a listview that is in a form called DBadd. So it should read like this:

    connectionString = DBadd.lstDBadd.ListItems(commandBarButton1.Caption).ListSubItems(3)

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Re: Outlook 2003: How to programmitically add code to ThisOutlookSession

    OK RobDog, I really need you to wrap your brain around this, because I think I am really close to implementing your solution, but I am stuck on debug.

    All of the settings for each button are stored in the registry (see pic below)


    Here is my code that loads each DB's details into a 3D array. Then for every registry entry, a button is added to mnuHoldsARs which is a PopUpButton passed from Application_MAPILogonComplete. I get an error at the line in red. The error is: "Object variable or With block variable not set." Could you look at my code and tell me where I've gone wrong?

    VB Code:
    1. Private Sub Populate_CB_Array(menuHoldsARs As CommandBarPopup)
    2. 'Takes all AR Tracker DBs from the Registry and adds a command
    3. 'bar button for each DB.
    4. 'The "AR Tracker DB Count" registry contains the name of each DB
    5. 'There is also a registry entry for each DB listed in "AR Tracker DB Count"
    6. 'For each DB registry entry, there is a Paths and Description section
    7. 'that contains the path and description of each DB.
    8.    
    9.     Dim DB_names As Variant
    10.     Dim CB_Array() As Office.CommandBarButton
    11.     Dim i As Integer
    12.     Dim DB_name_path_description() As String
    13.    
    14.     'get DB Names from Registry
    15.     DB_names = GetAllSettings("AR Tracker DB Count", "DB Count") 'ReDims automatically based on num of reg entries
    16.     ReDim DB_name_path_description(1, 1, UBound(DB_names) - 1)
    17.     ReDim CB_Array(UBound(DB_names) - 1)
    18.    
    19.    
    20.     For i = 0 To UBound(DB_names) - 1
    21.         'populate array with registry info
    22.         '3D array has DB_Name as Page, 1 - 2D element per page (path = (0,0), description = (0,1))
    23.         DB_name_path_description(0, 0, i) = GetSetting(DB_names(0, i), "Paths", "DB_Path")
    24.         DB_name_path_description(0, 1, i) = GetSetting(DB_names(0, i), "Description", "DB_Description")
    25.        
    26.         'populate CommandBarButton Array
    27.         [COLOR=Red][B]CB_Array(i) = menuHoldsARs.Controls.Add(msoControlButton, 1, , , False)[/B][/COLOR]        
    28.         With CB_Array(i)
    29.             .BeginGroup = False
    30.             .Caption = DB_names(0, i)
    31.             .Enabled = True
    32.             .Style = msoControlCustom
    33.             .Tag = i & ", " & DB_names(i) 'identify each button, Syntax: <button#, "DBname">
    34.             .TooltipText = DB_name_path_description(0, 1, DB_names(i)) 'DB description as ToolTipText
    35.             .Visible = True
    36.         End With
    37.         Set CB_Array(i) = oMnuTrackerSub1 'Set each button to have WithEvents
    38.     Next i
    39.    
    40. End Sub

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Re: Outlook 2003: How to programmitically add code to ThisOutlookSession

    OK, I've gotten past this error by using "Set" (yeah I'm stupid)... The only problem I am having with this is getting the click event to occur. The click event only gets set for the last button in the array. How do I get around this?

    VB Code:
    1. Private Sub Populate_CB_Array(menuHoldsARs As CommandBarPopup)
    2. 'Takes all AR Tracker DBs from the Registry and adds a command
    3. 'bar button for each DB.
    4. 'The "AR Tracker DB Count" registry contains the name of each DB
    5. 'There is also a registry entry for each DB listed in "AR Tracker DB Count"
    6. 'For each DB registry entry, there is a Paths and Description section
    7. 'that contains the path and description of each DB.
    8.    
    9.     Dim DB_names As Variant
    10.     Dim CB_Array() As Office.CommandBarButton
    11.     Dim i As Integer
    12.     Dim DB_name_path_description() As String
    13.    
    14.     'get DB Names from Registry
    15.     DB_names = GetAllSettings("AR Tracker DB Count", "DB Count") 'ReDims automatically based on num of reg entries
    16.     ReDim DB_name_path_description(1, 1, UBound(DB_names))
    17.     ReDim CB_Array(UBound(DB_names))
    18.    
    19.    
    20.     For i = 0 To UBound(DB_names)
    21.         'Debug.Print "(i, 0): " & DB_names(i, 0) _
    22.         & vbCr & "(0, i): " & DB_names(0, i)
    23.         'populate array with registry info
    24.         '3D array has DB_Name as Page, 1 - 2D element per page (path = (0,0), description = (0,1))
    25.         DB_name_path_description(0, 0, i) = GetSetting(DB_names(i, 0), "Paths", "DB Path")
    26.         DB_name_path_description(0, 1, i) = GetSetting(DB_names(i, 0), "Description", "DB Description")
    27.        
    28.         'populate CommandBarButton Array
    29.         Set CB_Array(i) = menuHoldsARs.Controls.Add(msoControlButton, 1, , , False)
    30.         With CB_Array(i)
    31.             .BeginGroup = False
    32.             .Caption = DB_names(i, 0)
    33.             .Enabled = True
    34.             .Style = msoControlCustom
    35.             .Tag = i & ", " & DB_names(i, 0) 'identify each button, Syntax: <button#, "DBname">
    36.             .TooltipText = DB_name_path_description(0, 1, i) 'DB description as ToolTipText
    37.             .Visible = True
    38.         End With
    39.         Set CB_Array(i) = oMnuTrackerSub1 'Set each button to have WithEvents
    40.     Next i
    41.    
    42. End Sub

Posting Permissions

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



Click Here to Expand Forum to Full Width