Results 1 to 21 of 21

Thread: [RESOLVED] Right-click options Open MailItem in Outlook App

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Resolved [RESOLVED] Right-click options Open MailItem in Outlook App

    I have coded a pretty extensive Excel project for use at work. It basically tracks issues with engineering projects. This "tracker" has a TON of features available and I would like to add one more. The feature I'm trying to add would allow a user to open an Outlook mail item, highlight text in the mail item, then right click in the window and select "Export to Tracker" from the right-click menu which would then bring up a prompt and ultimately copy the info over to my tracker. I have experience with Outlook Automation through Excel (even though its not much) so I know a little bit of Outlook VB but not much.

    Of course, any snipets of code would be great, however I really just want to know if I can add an option to the right-click menu through VB. If that is not possible, I can always add a toolbar button to perform the same task. The only problem with that is I don't know any Outlook VB

    Any help would be GREATLY appreciated!!!

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

    Re: Right-click options Open MailItem in Outlook App

    Outlook VBA is the same as Excel VBA. Adding a menu item to Outlook default Context menu is very difficult and I have not seen a single VB 6 or VBA solution suceed. Your better off creating a toolbar button on the Inspector window or creating a custom Context menu but only with your menu items on it. If you need the original items also you could try to duplicate their actions but too much work if you ask me.
    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

    Question Re: Right-click options Open MailItem in Outlook App

    Thanks Rob,

    I do realize that Excel and Outlook share the same VBA code, I am just not familiar with the objects, items, etc. that are native to Outlook only.

    For ease's sake, I think I'm going to go with adding a toolbar button to the inspector window. I can do this via GUI commands, but I'm not sure how to implement this with code. Do you have any examples of this code?

    Thanks again in advance!

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

    Re: Right-click options Open MailItem in Outlook App

    Yes, search by my username and Commandbar and/or toolbar/menu.
    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

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Re: Right-click options Open MailItem in Outlook App

    Hi RobDog,

    OK, I have two questions. First, I added your code to the ThisOutlookSession to add a menu option "Tools" -> "Add to AR Tracker..." which adds the menu item to the Outlook window. I would also like to add this menu item to the tools menu of an open email (basically added to EVERY email that is opened). How would I go about doing that?
    VB Code:
    1. Option Explicit
    2. 'Behind ThisOutlookSession
    3. Public WithEvents oMnuSaveAs As Office.CommandBarButton
    4.  
    5. Private Sub SyncMnuSaveAsButton(btn As Office.CommandBarButton)
    6.     Set oMnuSaveAs = btn
    7.     If btn Is Nothing Then
    8.         MsgBox "Sync. of '" & btn.Caption & "' button event failed!", vbCritical + vbOKOnly
    9.     End If
    10. End Sub
    11.  
    12. Private Sub Application_MAPILogonComplete()
    13.    
    14.     Dim oCBmnuTools As Office.CommandBarPopup
    15.     Dim oCBmnuSaveMe As Office.CommandBarButton
    16.    
    17.     '<ADD A MENU ITEM>
    18.     Set oCBmnuTools = Application.ActiveExplorer.CommandBars("Menu Bar").Controls("&Tools")
    19.     Set oCBmnuSaveMe = Application.ActiveExplorer.CommandBars("Menu Bar").FindControl(msoControlButton, 1, "888", True, True)
    20.     If TypeName(oCBmnuSaveMe) = "Nothing" Then
    21.         Set oCBmnuSaveMe = oCBmnuTools.Controls.Add(msoControlButton, 1, "888", , True)
    22.     End If
    23.     With oCBmnuSaveMe
    24.         .BeginGroup = True
    25.         .Caption = "Add to AR Tracker..."
    26.         .Enabled = True
    27.         .Style = msoControlCustom
    28.         .Tag = "888"
    29.         .Visible = True
    30.     End With
    31.     Call SyncMnuSaveAsButton(oCBmnuSaveMe)
    32.     '</ADD A MENU ITEM>
    33. End Sub
    34.  
    35. Private Sub oMnuSaveAs_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    36.     MsgBox "oMnuSaveAs_Click"
    37. End Sub

    Second, I would like the oMnuSaveAs_Click to start a form in which information about the engineering issue can be entered which will then get put in the tracker. If the active window is the inbox screen, i would like to select the mailitem that is highlighted in the window and then bring up a form to enter in more info about the info to track. Or, if the email is open, I would like to use the selected text and insert it into a form... the rest of the info in the form will be inserted into the tracker...

    I know this sounds like alot, but i basically am having trouble activating the inbox (i think its olFolderInbox) or activating an open mail item and selected text.

    As always, thanks a ton!!

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Question Re: Right-click options Open MailItem in Outlook App

    Hi RobDog,

    I really appreciate the code you gave me (above). I do have one issue with it. It seems that once I click Tools -> Add to AR Tracker... and execute my code, it seems to disable the command bar button. If I try to click it again, nothing happens. To solve this, I have to manually run the Application_MAPILogonComplete() event before the command bar button will function again. Is there a way around this? I tried to Call Application_MAPILogonComplete() after executing my code (it does call correctly and execute the code correctly), but for some reason, the command bar button still doesn't work. Any suggestions?

    BTW, please disregard my last post. That was some time ago and I am WAY farther into my project than that.

    Thanks again to the VBA guru!!!

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

    Re: Right-click options Open MailItem in Outlook App

    Do you have more then one explorer open (Outlook)? There is no disableing code above or do you mean it just looks enabled but doesnt work?[/color]
    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

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Re: Right-click options Open MailItem in Outlook App

    I only have one instance of Outlook open and I'm pretty sure I don't create a new explorer anywhere in code (if thats even possible). The button definitely looks enabled, but won't work. It does work, on the first try, however any subsequent tries always fail. This is such a small issue, but it's killing my debug time... anything at all to help would be awesome!! Thanks again, robdog!

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

    Re: Right-click options Open MailItem in Outlook App

    Do you have other code in the click event? Perhaps there is an error somewhere that is breaking the event. Or are you using it exactly as posted?
    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

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Re: Right-click options Open MailItem in Outlook App

    This is the code I am using in ThisOutlookSession:

    VB Code:
    1. Option Explicit
    2. 'Behind ThisOutlookSession
    3. Public WithEvents oMnuSaveAs As Office.CommandBarButton
    4.  
    5. Private Sub SyncMnuSaveAsButton(btn As Office.CommandBarButton)
    6.     Set oMnuSaveAs = btn
    7.     If btn Is Nothing Then
    8.         MsgBox "Sync. of '" & btn.Caption & "' button event failed!", vbCritical + vbOKOnly
    9.     End If
    10. End Sub
    11.  
    12. 'Public Sub Application_MAPILogonComplete()
    13. '
    14. '    Dim oCBmnuTools As Office.CommandBarPopup
    15. '    Dim oCBmnuSaveMe As Office.CommandBarButton
    16. '
    17. '    '<ADD A MENU ITEM>
    18. '    Set oCBmnuTools = Application.ActiveExplorer.CommandBars("Menu Bar").Controls("&Tools")
    19. '    Set oCBmnuSaveMe = Application.ActiveExplorer.CommandBars("Menu Bar").FindControl(msoControlButton, 1, "888", True, True)
    20. '    If TypeName(oCBmnuSaveMe) = "Nothing" Then
    21. '        Set oCBmnuSaveMe = oCBmnuTools.Controls.Add(msoControlButton, 1, "888", , True)
    22. '    End If
    23. '    With oCBmnuSaveMe
    24. '        .BeginGroup = True
    25. '        .Caption = "Add to AR Tracker..."
    26. '        .Enabled = True
    27. '        .Style = msoControlCustom
    28. '        .Tag = "888"
    29. '        .Visible = True
    30. '    End With
    31. '    Call SyncMnuSaveAsButton(oCBmnuSaveMe)
    32. '    '</ADD A MENU ITEM>
    33. 'End Sub
    34.  
    35. Public Sub Application_Startup()
    36.     Dim oCBmnuTools As Office.CommandBarPopup
    37.     Dim oCBmnuSaveMe As Office.CommandBarButton
    38.    
    39.     '<ADD A MENU ITEM>
    40.     Set oCBmnuTools = Application.ActiveExplorer.CommandBars("Menu Bar").Controls("&Tools")
    41.     Set oCBmnuSaveMe = Application.ActiveExplorer.CommandBars("Menu Bar").FindControl(msoControlButton, 1, "888", True, True)
    42.     If TypeName(oCBmnuSaveMe) = "Nothing" Then
    43.         Set oCBmnuSaveMe = oCBmnuTools.Controls.Add(msoControlButton, 1, "888", , False)
    44.     End If
    45.     With oCBmnuSaveMe
    46.         .BeginGroup = True
    47.         .Caption = "Add to AR Tracker..."
    48.         .Enabled = True
    49.         .Style = msoControlCustom
    50.         .Tag = "888"
    51.         .Visible = True
    52.         .OnAction = "oMnuSaveAs_Click()"
    53.     End With
    54.     Call SyncMnuSaveAsButton(oCBmnuSaveMe)
    55.     '</ADD A MENU ITEM>
    56. End Sub
    57.  
    58. Private Sub oMnuSaveAs_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    59. Dim oApp        As Outlook.Application
    60. Dim myExplorers As Outlook.Explorer
    61. Dim myExpSel    As Outlook.Selection
    62. Dim oEmail      As Outlook.MailItem
    63.  
    64. Set oApp = GetObject(, "Outlook.Application")
    65. Set myExplorers = oApp.ActiveExplorer
    66. Set myExpSel = myExplorers.Selection
    67. Set oEmail = myExpSel.Item(1)
    68.    
    69.     Call FindOutlookEmail(oEmail.EntryID, oEmail.Subject) 'Determine if its already in tracker, set recordneedstobedeleted flag
    70.     Call arInfo.AR_Menu_Click 'Add current email (calls Outlook_Emails_2_Access)
    71.     If recordNeedsDeleting Then Call DeleteRecord   'Delete selected records in SimilarARs form
    72.    
    73.    
    74.     Call Application_Startup
    75.    
    76.     Unload arInfo
    77.     Unload SimilarARs
    78.     Set oEmail = Nothing
    79.     Set myExplorers = Nothing
    80.     Set myExpSel = Nothing
    81. End Sub

    I tried to move the commandbar button code to the Application_Startup event from the Application_MAPILogonComplete event. It still didn't work. Hope this helps...

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

    Re: Right-click options Open MailItem in Outlook App

    Dont use the .OnAction property as we are already using the sync procedure to link up the button to the withevents.

    Also, since this is in outlook vba you dont need to define an application object, just use Application as its the default object of the current running instance.

    And dont use Application_Startup as it fires before the GUI is initialized and may give you issues initializing the button. The Application_MAPILogonComplete is the last event to fire and should be the one your using.

    Dont can the Application_Startup event again as its already initialized with the with events. all these duplicate calls may be breaking the event.
    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

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Re: Right-click options Open MailItem in Outlook App

    Wow, I think it had everything to do with the GetObject(, "Outlook.Application"). Thanks for the help, robdog!!!

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

    Re: Right-click options Open MailItem in Outlook App

    Cool, glad its Resolved then?
    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

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Re: [RESOLVED] Right-click options Open MailItem in Outlook App

    Yep!

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Re: [RESOLVED] Right-click options Open MailItem in Outlook App

    Sorry to keep re-opening this thread, but i have just one more question.

    Do you have any sample code that i can look at that would allow me to have a submenu under Tools -> Add to AR Tracker... -> SubMenuItem1, SubMenuItem2, etc?

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

    Re: [RESOLVED] Right-click options Open MailItem in Outlook App

    Its the same technique but the first main item should be added as a CommandBarPopup type instead. Then add menu items to it.
    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

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Re: [RESOLVED] Right-click options Open MailItem in Outlook App

    Awesome!! I was able to confiure the menu EXACTLY how I wanted. Thanks again for your help. I've posted my code in case anyone wants to see what i did...
    VB Code:
    1. Private Sub Application_MAPILogonComplete()
    2.     Dim arMenu As Office.CommandBarPopup
    3.     Dim currentMenuBar As Office.CommandBar
    4.     Dim oCBmnuTools As Office.CommandBarPopup
    5.     Dim oCBmnuAddToTrackerMain As Office.CommandBarPopup
    6.     Dim oCBmnuTracker1 As Office.CommandBarButton
    7.     Dim oCBmnuTrackerAdd As Office.CommandBarButton
    8.  
    9.     Set currentMenuBar = Application.ActiveExplorer.CommandBars.ActiveMenuBar
    10.     currentMenuBar.Reset
    11.     Set arMenu = currentMenuBar.Controls.Add(msoControlPopup, 1)
    12.     Set oCBmnuAddToTrackerMain = arMenu.Controls.Add(msoControlPopup, 1)
    13.     Set oCBmnuTrackerAdd = arMenu.Controls.Add(msoControlButton, 1)
    14.     Set oCBmnuTracker1 = oCBmnuAddToTrackerMain.Controls.Add(msoControlButton, 1, "888", , False)
    15.    
    16.     With arMenu
    17.         .BeginGroup = True
    18.         .Caption = "AR Tracker"
    19.         .Enabled = True
    20.         .Visible = True
    21.     End With
    22.     With oCBmnuAddToTrackerMain
    23.         .BeginGroup = True
    24.         .Caption = "Add to AR Tracker"
    25.         .Enabled = True
    26.         '.Style = msoControlCustom
    27.         .Tag = "888"
    28.         .Visible = True
    29.     End With
    30.     With oCBmnuTrackerAdd
    31.         .BeginGroup = True
    32.         .Caption = "Add Database to this list..."
    33.         .Enabled = True
    34.         .Style = msoControlCustom
    35.         .Visible = True
    36.     End With
    37.     With oCBmnuTracker1
    38.         .BeginGroup = False
    39.         .Caption = "Blackford Issues"
    40.         .Enabled = True
    41.         .Style = msoControlCustom
    42.         .Tag = "888"
    43.         .Visible = True
    44.     End With
    45.    
    46.     Set oMnuTrackerSub1 = oCBmnuTracker1 ' Set buttons to have WithEvents
    47.     Set oMnuTrackerAdd = oCBmnuTrackerAdd
    48.    
    49. End Sub

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

    Re: [RESOLVED] Right-click options Open MailItem in Outlook App

    but dont forget to change your .Tag property for each menu item as you may need to reference or find it later and currently they all have the same "888" tag.
    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

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Re: [RESOLVED] Right-click options Open MailItem in Outlook App

    Hi Robdog,

    I am having the same issue again as I was before. The problem is that when I use the menu bar buttons to run my code, the code will execute, however the next time I attempt to click the menu bar button, nothing happens. I am basically using my ThisOutlookSession as my main, where all function calls happen from ThisOutlookSession and everything ultimately returns to ThisOutlookSession to finish up. Here's my code for the entire ThisOutlookSession (sorry it is really long, but I REALLY AM IN DESPERATION!!). If an error occurs anywhere in the other functions, then a boolean flag called "quitting" is set and thus prevents any other function calls from ThisOutlookSession. Basically my program is worthless if I can't fix this problem!!

    VB Code:
    1. ' PART 1 of 2
    2.  
    3. Option Explicit
    4. 'Behind ThisOutlookSession
    5. Public WithEvents oMnuTrackerSub0 As Office.CommandBarButton
    6. Public WithEvents oMnuTrackerSub1 As Office.CommandBarButton
    7. Public WithEvents oMnuTrackerSub2 As Office.CommandBarButton
    8. Public WithEvents oMnuTrackerSub3 As Office.CommandBarButton
    9. Public WithEvents oMnuTrackerSub4 As Office.CommandBarButton
    10. Public WithEvents oMnuTrackerSub5 As Office.CommandBarButton
    11. Public WithEvents oMnuTrackerSub6 As Office.CommandBarButton
    12. Public WithEvents oMnuTrackerSub7 As Office.CommandBarButton
    13. Public WithEvents oMnuTrackerSub8 As Office.CommandBarButton
    14. Public WithEvents oMnuTrackerSub9 As Office.CommandBarButton
    15. Public WithEvents oMnuTrackerSub10 As Office.CommandBarButton
    16. Public WithEvents oMnuTrackerSub11 As Office.CommandBarButton
    17. Public WithEvents oMnuTrackerSub12 As Office.CommandBarButton
    18. Public WithEvents oMnuTrackerSub13 As Office.CommandBarButton
    19. Public WithEvents oMnuTrackerSub14 As Office.CommandBarButton
    20. Public WithEvents oMnuTrackerAdd As Office.CommandBarButton
    21. Public arInfo_Close As Label
    22. Public connectionString As String
    23. Public quitting As Boolean
    24.  
    25. Private Sub SyncMnuTracker1Button(btn As Office.CommandBarButton)
    26.     Set oMnuTrackerSub1 = btn
    27.    
    28.     If btn Is Nothing Then
    29.         MsgBox "Sync. of '" & btn.Caption & "' button event failed!", vbCritical + vbOKOnly
    30.     End If
    31. End Sub
    32.  
    33. Public Sub Application_MAPILogonComplete()
    34.     Dim arStatus As Office.CommandBar
    35.     Dim CBtxt_arStatus As Office.CommandBarControl
    36.     Dim arMenu As Office.CommandBarPopup
    37.     Dim currentMenuBar As Office.CommandBar
    38.     Dim oCBmnuTools As Office.CommandBarPopup
    39.     Dim oCBmnuAddToTrackerMain As Office.CommandBarPopup
    40.     Dim oCBButtons(100) As Controls
    41.     Dim oCBmnuTrackerAdd As Office.CommandBarButton
    42.    
    43.     '<ADD A MENU ITEM>
    44.     Set currentMenuBar = Application.ActiveExplorer.CommandBars.ActiveMenuBar
    45.     currentMenuBar.Reset
    46.     Set arMenu = currentMenuBar.Controls.Add(msoControlPopup, 1)
    47.     Set oCBmnuAddToTrackerMain = arMenu.Controls.Add(msoControlPopup, 1)
    48.     Set oCBmnuTrackerAdd = arMenu.Controls.Add(msoControlButton, 1)
    49.     Set arStatus = Application.ActiveExplorer.CommandBars(Application.ActiveExplorer.CommandBars.Count)
    50.     If Not arStatus.name = "AR Tracker Status ToolBar" Then
    51.         Set arStatus = Application.ActiveExplorer.CommandBars.Add("AR Tracker Status ToolBar", msoBarTop, False, True)
    52.         Set CBtxt_arStatus = arStatus.Controls.Add(1)
    53.     End If
    54.     Set CBtxt_arStatus = arStatus.FindControl(1)
    55.    
    56.     'Set oCBmnuTools = Application.ActiveExplorer.CommandBars("Menu Bar").Controls("&Tools")
    57.     'Set oCBmnuAddToTrackerMain = Application.ActiveExplorer.CommandBars("Menu Bar").FindControl(msoControlPopup, 1, "888", True, True)
    58.     ''oCBmnuTools.Reset
    59.     'If TypeName(oCBmnuAddToTrackerMain) = "Nothing" Then
    60.     '    Set oCBmnuAddToTrackerMain = oCBmnuTools.Controls.Add(msoControlPopup, 1)
    61.     'End If
    62.     'Set oCBmnuTracker1 = oCBmnuAddToTrackerMain.Controls.Add(msoControlButton, 1, "888", , False)
    63.     With Application.ActiveExplorer.CommandBars("AR Tracker Status ToolBar")
    64.         .Left = 200
    65.         .Visible = True
    66.     End With
    67.     With arMenu
    68.         .BeginGroup = True
    69.         .Caption = "A&R Tracker"
    70.         .Enabled = True
    71.         .Visible = True
    72.     End With
    73.     With oCBmnuAddToTrackerMain
    74.         .BeginGroup = True
    75.         .Caption = "Add to AR Tracker"
    76.         .Enabled = True
    77.         '.Style = msoControlCustom
    78.         '.Tag = "888"
    79.         .Visible = True
    80.     End With
    81.     With oCBmnuTrackerAdd
    82.         .BeginGroup = True
    83.         .Caption = "Add Database to this list..."
    84.         .Enabled = True
    85.         .Style = msoControlCustom
    86.         .Visible = True
    87.     End With
    88.     With CBtxt_arStatus
    89.         .BeginGroup = False
    90.         .Width = 2000
    91.         .Caption = "AR Tracker Status: Ready"
    92.         .Tag = "AR Tracker Status ToolBar"
    93.         .Enabled = True
    94.         .Visible = True
    95.     End With
    96.    
    97.     If Not restarting Then
    98.     'Set oMnuTrackerSub1 = oCBmnuTracker1 ' Set buttons to have WithEvents
    99.         Set oMnuTrackerAdd = oCBmnuTrackerAdd
    100.         Call Populate_CB_Array(oCBmnuAddToTrackerMain)
    101.     'Call SyncMnuTracker1Button(oCBmnuTracker1)
    102.     '</ADD A MENU ITEM>
    103.     End If
    104. End Sub
    105.  
    106. Private Sub oMnuTrackerAdd_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    107.     DBadd.Show
    108. End Sub
    109.  
    110. Private Sub oMnuTrackerSub0_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    111.     Call myClick_Event(Ctrl)
    112. End Sub
    113. Private Sub oMnuTrackerSub1_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    114.     Call myClick_Event(Ctrl)
    115. End Sub
    116. Private Sub oMnuTrackerSub2_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    117.     Call myClick_Event(Ctrl)
    118. End Sub
    119. Private Sub oMnuTrackerSub3_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    120.     Call myClick_Event(Ctrl)
    121. End Sub
    122. Private Sub oMnuTrackerSub4_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    123.     Call myClick_Event(Ctrl)
    124. End Sub
    125. Private Sub oMnuTrackerSub5_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    126.     Call myClick_Event(Ctrl)
    127. End Sub
    128. Private Sub oMnuTrackerSub6_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    129.     Call myClick_Event(Ctrl)
    130. End Sub
    131. Private Sub oMnuTrackerSub7_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    132.     Call myClick_Event(Ctrl)
    133. End Sub
    134. Private Sub oMnuTrackerSub8_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    135.     Call myClick_Event(Ctrl)
    136. End Sub
    137. Private Sub oMnuTrackerSub9_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    138.     Call myClick_Event(Ctrl)
    139. End Sub
    140. Private Sub oMnuTrackerSub10_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    141.     Call myClick_Event(Ctrl)
    142. End Sub
    143. Private Sub oMnuTrackerSub11_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    144.     Call myClick_Event(Ctrl)
    145. End Sub
    146. Private Sub oMnuTrackerSub12_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    147.     Call myClick_Event(Ctrl)
    148. End Sub
    149. Private Sub oMnuTrackerSub13_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    150.     Call myClick_Event(Ctrl)
    151. End Sub
    152. Private Sub oMnuTrackerSub14_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    153.     Call myClick_Event(Ctrl)
    154. End Sub

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Re: [RESOLVED] Right-click options Open MailItem in Outlook App

    VB Code:
    1. ' PART 2 OF 2
    2.  
    3. Private Sub Populate_CB_Array(menuHoldsARs As CommandBarPopup)
    4. 'Takes all AR Tracker DBs from the Registry and adds a command
    5. 'bar button for each DB.
    6. 'The "AR Tracker DB Count" registry contains the name of each DB
    7. 'There is also a registry entry for each DB listed in "AR Tracker DB Count"
    8. 'For each DB registry entry, there is a Paths and Description section
    9. 'that contains the path and description of each DB.
    10.  
    11. On Error GoTo No_bugs
    12.  
    13.     Dim CBbuttons(15) As Office.CommandBarButton
    14.     Dim DB_names As Variant
    15.     Dim CB_Array() As Office.CommandBarButton
    16.     Dim i As Integer
    17.     Dim DB_name_path_description() As String
    18.     Dim myCtrl As Office.CommandBarButton
    19.    
    20.     'get DB Names from Registry
    21.     DB_names = GetAllSettings("AR Tracker DB Count", "DB Count") 'ReDims automatically based on num of reg entries
    22.     ReDim DB_name_path_description(1, 1, UBound(DB_names))
    23.     ReDim CB_Array(UBound(DB_names))
    24.    
    25.     Set CBbuttons(0) = oMnuTrackerSub0
    26.     Set CBbuttons(1) = oMnuTrackerSub1
    27.     Set CBbuttons(2) = oMnuTrackerSub2
    28.     Set CBbuttons(3) = oMnuTrackerSub3
    29.     Set CBbuttons(4) = oMnuTrackerSub4
    30.     Set CBbuttons(5) = oMnuTrackerSub5
    31.     Set CBbuttons(6) = oMnuTrackerSub6
    32.     Set CBbuttons(7) = oMnuTrackerSub7
    33.     Set CBbuttons(8) = oMnuTrackerSub8
    34.     Set CBbuttons(9) = oMnuTrackerSub9
    35.     Set CBbuttons(10) = oMnuTrackerSub10
    36.     Set CBbuttons(11) = oMnuTrackerSub11
    37.     Set CBbuttons(12) = oMnuTrackerSub12
    38.     Set CBbuttons(13) = oMnuTrackerSub13
    39.     Set CBbuttons(14) = oMnuTrackerSub14
    40.    
    41.     For i = 0 To UBound(DB_names)
    42.         'populate array with registry info
    43.         '3D array has DB_Name as Page, 1 - 2D element per page (path = (0,0), description = (0,1))
    44.         DB_name_path_description(0, 0, i) = GetSetting(DB_names(i, 0), "Paths", "DB Path")
    45.         DB_name_path_description(0, 1, i) = GetSetting(DB_names(i, 0), "Description", "DB Description")
    46.        
    47.         'populate CommandBarButton Array
    48.         Set CB_Array(i) = menuHoldsARs.Controls.Add(msoControlButton, 1, , , False)
    49.         With CB_Array(i)
    50.             .BeginGroup = False
    51.             .Caption = DB_names(i, 0)
    52.             .Enabled = True
    53.             .Style = msoControlCustom
    54.             .Tag = i & ", " & DB_name_path_description(0, 0, i) 'identify each button, Syntax: <button#, "DB Path">
    55.             .TooltipText = DB_name_path_description(0, 1, i) 'DB description as ToolTipText
    56.             .Visible = True
    57.         End With
    58.         'Set CBbuttons(i) = CB_Array(i) 'Set each button to have WithEvents
    59.     Next i
    60.    
    61.     Set oMnuTrackerSub0 = CB_Array(0)
    62.     Set oMnuTrackerSub1 = CB_Array(1)
    63.     Set oMnuTrackerSub2 = CB_Array(2)
    64.     Set oMnuTrackerSub3 = CB_Array(3)
    65.     Set oMnuTrackerSub4 = CB_Array(4)
    66.     Set oMnuTrackerSub5 = CB_Array(5)
    67.     Set oMnuTrackerSub6 = CB_Array(6)
    68.     Set oMnuTrackerSub7 = CB_Array(7)
    69.     Set oMnuTrackerSub8 = CB_Array(8)
    70.     Set oMnuTrackerSub9 = CB_Array(9)
    71.     Set oMnuTrackerSub10 = CB_Array(10)
    72.     Set oMnuTrackerSub11 = CB_Array(11)
    73.     Set oMnuTrackerSub12 = CB_Array(12)
    74.     Set oMnuTrackerSub13 = CB_Array(13)
    75.     Set oMnuTrackerSub14 = CB_Array(14)
    76.    
    77. No_bugs:
    78. If Err.Number = "9" Then
    79. Else:   Menu_Text ("Error!")
    80.         MsgBox "Outlook encountered an error" _
    81.         & vbCr & "Error Number: " & Err.Number _
    82.         & vbCr & "Error: " & Err.description
    83. End If
    84. End Sub
    85.  
    86. Public Sub myClick_Event(Ctrl As Office.CommandBarButton)
    87. 'Dim oApp        As Outlook.Application
    88. Dim myExplorers As Outlook.Explorer
    89. Dim myExpSel    As Outlook.Selection
    90. Dim oEmail      As Outlook.MailItem
    91. 'Dim myStep As Label
    92.  
    93. Set myExplorers = Application.ActiveExplorer
    94. Set myExpSel = myExplorers.Selection
    95. Set oEmail = myExpSel.Item(1)
    96.  
    97.     quitting = False
    98.  
    99.     connectionString = Mid$(Ctrl.Tag, 4, Len(Ctrl.Tag))
    100.    
    101.     Pass_Connection_Path (connectionString)
    102.  
    103.     Call FindOutlookEmail(oEmail.EntryID, oEmail.Subject) 'Determine if its already in tracker, set recordneedstobedeleted flag
    104.  
    105.     If Not quitting Then
    106.         Call arInfo.AR_Menu_Click   'Add current email (calls Outlook_Emails_2_Access)
    107.     End If
    108.     If Not quitting Then
    109.         If recordNeedsDeleting Then Call DeleteRecord   'Delete selected records in SimilarARs form
    110.     End If
    111.    
    112.     Menu_Text ("Ready")
    113.     Unload arInfo
    114.     Unload Calendar
    115.     Unload DBadd
    116.     Unload DBaddORedit
    117.     Unload SimilarARs
    118.     If Connection_Exists Then Close_Connection
    119.     Set oEmail = Nothing
    120.     Set myExplorers = Nothing
    121.     Set myExpSel = Nothing
    122.     'Restart_Prog
    123. End Sub

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

    Re: [RESOLVED] Right-click options Open MailItem in Outlook App

    First, place a breakpoint at maillogoncomplete and step through the code to make sure everything is being created correctly. Then place another at the myclick event and track the code execution. I dont think you should be setting the array from the commandbuttons and then back again. This may be an issue but the step through should show you whats up.
    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

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