user9962623
user9962623

Reputation:

Outlook Add-In error

I am creating an add-in that lets users click a button and it opens a new email and auto fills fields and lets them edit the body before sending the email.

I am getting an error that does not let me use Outlook.Application

Error BC30111 'Application' is an interface type and cannot be used as an expression.

What am I doing wrong?

My Code:

Imports Microsoft.Office.Interop.Outlook
Imports Microsoft.Office.Tools.Ribbon

Public Class Ribbon1
    Private olMailItem As Object
    Private olImportanceHigh As OlImportance

    Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load

    End Sub

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

        Dim obApp As Object
        Dim NewMail As MailItem

        obApp = Outlook.Application
        NewMail = obApp.CreateItem(olMailItem)

        'You can change the concrete info as per your needs
        With NewMail
            .Subject = " Test Email"
            .To = "[email protected]"
            .Body = "This is just a test email template with Outlook VBA" & vbCrLf & vbCrLf & vbCrLf & "Yours Truly," & vbCrLf & vbCrLf & "John Smith"
            .Importance = olImportanceHigh
            .Display
        End With

        obApp = Nothing
        NewMail = Nothing

    End Sub
End Class

Upvotes: 0

Views: 200

Answers (2)

Mathieu Guindon
Mathieu Guindon

Reputation: 71247

Your VSTO add-in has an entry point. That would be a Partial Class possibly called ThisAddIn, with methods such as ThisAddIn_Startup and ThisAddIn_Shutdown (as per this article).

That class inherits a class that exposes an Application property, of type Outlook.Application - that's the object you want to be using.

So pass that object to your Ribbon1 instance when you create it at startup:

Private Sub ThisAddIn_Startup(object sender, System.EventArgs e) Handles Something(?).Startup
    Dim ribbon As Ribbon1 = New Ribbon1(Me.Application)
    ' ...
End Sub

Expose a constructor for your Ribbon1 class, so that you can pass the Application instance:

Private olApp As Outlook.Application

Public Sub New(ByVal app As Outlook.Application)
    olApp = app
End Sub

Now remove your local obApp As Outlook.Application declaration, and use the instance that was passed from the constructor instead.


EDIT: Scratch all that. Reading through the article I've linked to, there is a global Application instance readily available - just use it:

obApp = Globals.ThisAddIn.Application

With that, your Ribbon1 class doesn't need to receive the Application instance through its constructor. Although, it's very debatable whether it's a better design to use globals.

Upvotes: 1

nagarajannd
nagarajannd

Reputation: 715

Correction made from your code - declared obApp as Outlook.Application

added Set when creating instance of objects and disposing the object.

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

    Dim obApp As Outlook.Application
    Dim NewMail As MailItem

    Set obApp = New Outlook.Application
    Set NewMail = obApp.CreateItem(olMailItem)

    'You can change the concrete info as per your needs
    With NewMail
        .Subject = " Test Email"
        .To = "[email protected]"
        .Body = "This is just a test email template with Outlook VBA" & vbCrLf & vbCrLf & vbCrLf & "Yours Truly," & vbCrLf & vbCrLf & "John Smith"
        .Importance = olImportanceHigh
        .Display
    End With

    Set obApp = Nothing
    Set NewMail = Nothing

End Sub

Upvotes: 0

Related Questions