Search code examples
vb.netoutlookvstooutlook-addinribbonx

Outlook Ribbon on Click Event not working


I have a simple Outlook Ribon button I am trying to create to forward a selected email to a set address. The probelm is when I click the button, it is not runing the code. I tried adding several break points and they never happen, even on the If statment below. Does anyone have any ideas?

I have not worked with Visual Basic .NET in several years, and I feel like it must be something simple I am missing.

    Private Sub Button1_Click(sender As Object, application As Application) Handles Button1.Click
        Dim myinspector As Outlook.MailItem

        Dim myItem As Outlook.MailItem

        myinspector = application.ActiveInspector.CurrentItem

        If Not TypeName(myinspector) = "Nothing" Then
            myItem = myinspector.CurrentItem.Forward

            myItem.Display()

            myItem.Recipients.Add("Dan Wilson")

            myItem.Send()

        Else

            MsgBox("There is no active inspector.")

        End If

Full Project Link if wanted: https://drive.google.com/file/d/1zQfUilsTZ6tRjgT7gP0wI1j_CefdqvDW/view?usp=share_link

I tried break points in multible places. I looked at multble example sites to see if code was correct.


Solution

  • You have two handlers in your code for the Button1.Click event:

    Correct signature

    Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click
    End Sub
    

    Incorrect signature

    Private Sub Button1_Click(sender As Object, application As Application) Handles Button1.Click
    End Sub
    

    The second version (with the incorrect signature) is where your code is. There is no event with that signature, so it never gets called. The first version (with the correct signature) has no code in it to run, so that's why you're not seeing anything happen.

    It looks like you ended up with the double handlers because you were trying to get a reference to the current Outlook.MailItem using something similar to this:

    Dim myItem As Outlook.MailItem
    myItem = Application.ActiveInspector.CurrentItem
    

    That won't work. Here's the correct syntax:

    Dim myItem As Outlook.MailItem
    myItem = Globals.ThisAddIn.Application.ActiveInspector.CurrentItem
    

    Once you've got it working the way you want it, you can delete that other handler altogether.