0

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.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
Brandon
  • 3
  • 1
  • Add a TRY/CATCH in your full button code and put a break-point on CATCH. that the only method to see some errors. If click event don't catch, ensure sub signature is OK (or recreate it by double-clicking on button in form design). – tuyau2poil Mar 08 '23 at 09:04
  • It seems the Fluent UI can't find the callback with the right signature in your add-in. See my post for more information. – Eugene Astafiev Mar 08 '23 at 09:19

2 Answers2

0

The onAction callback (the click event handler) should have the following signature for buttons on the ribbon:

C#: void OnAction(IRibbonControl control)

VBA: Sub OnAction(control As IRibbonControl)

C++: HRESULT OnAction([in] IRibbonControl *pControl)

Visual Basic: Sub OnAction(control As IRibbonControl)

There are several ways of customizing the Fluent UI (aka Ribbon UI) in VSTO add-ins:

  1. Walkthrough: Create a custom tab by using Ribbon XML
  2. Walkthrough: Create a custom tab by using the Ribbon Designer

In case of using the ribbon designer your button's click event handler should look in the following way:

private void button1_Click(object sender, RibbonControlEventArgs e)
{
    toggleButton1.Checked = false;
}

By default, if a VSTO Add-in attempts to manipulate the Microsoft Office user interface (UI) and fails, no error message is displayed. However, you can configure Microsoft Office applications to display messages for errors that relate to the UI. You can use these messages to help determine why a custom ribbon does not appear, or why a ribbon appears but no controls appear. For Outlook, the Show VSTO Add-in user interface errors checkbox is located in the Developer section of the details pane.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
0

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.

InteXX
  • 6,135
  • 6
  • 43
  • 80