Philip Scheer
Philip Scheer

Reputation: 241

Outlook VSTO Add-in Custom rule/script

I have a VBA Script which I want to turn into a VSTO add-in for Outlook.

In VBA I have a method called

Public Sub DruckeAnhaenge(oMail As Outlook.MailItem)

which pops up when I create a rule with "Run a script" then I can select this method. Which is there called "ThisOutlookSession.DruckeAnhaenge".

Now I created in Visual Studio a VSTO add-in which has exactly the same method, but it doesn't show up anymore.

The startup method is called from this add-in (checked it with a MsgBox). So my question is how I can get this method as rule/script as it is in the VBA Editor?

Here is my whole code from the Add-In.

Public Class ThisAddIn

    Private Sub ThisAddIn_Startup() Handles Me.Startup
        MsgBox("Add In wird erfolgreich ausgeführt")
    End Sub

    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown

    End Sub

    Private Declare Function ShellExecute Lib "shell32.dll" Alias _
        "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String,
        ByVal lpFile As String, ByVal lpParameters As String,
        ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

    Private WithEvents Items As Outlook.Items

    Public Sub DruckeAnhaenge(oMail As Outlook.MailItem)
        On Error Resume Next
        Dim colAtts As Outlook.Attachments
        Dim oAtt As Outlook.Attachment
        Dim sFile As String
        Dim sDirectory As String
        Dim sFileType As String

        sDirectory = "C:\Attachments\"

        'Set colAtts = oMail.Attachments

        If oMail.Attachments.Count Then
            For Each oAtt In oMail.Attachments

                ' This code looks at the last 4 characters in a filename
                sFileType = LCase$(Right$(oAtt.FileName, 4))

                Select Case sFileType

                ' Add additional file types below
                    Case ".xls", ".doc", ".pdf"
                        sFile = sDirectory & oAtt.FileName
                        oAtt.SaveAsFile(sFile)
                        ShellExecute(0, "print", sFile, vbNullString, vbNullString, 0)
                End Select
            Next
        End If
    End Sub
End Class

Upvotes: 1

Views: 244

Answers (1)

Eugene Astafiev
Eugene Astafiev

Reputation: 49397

Outlook doesn't provide any way for assigning COM add-ins to rules. So, you need to handle corresponding events that OOM provides. In that case you need to handle the NewMailEx event of the Application class which is fired when a new item is received in the Inbox. The NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs. You can use the Entry ID returned in the EntryIDCollection array to call the GetItemFromID method and process the item. Then you can call the DruckeAnhaenge method and pass the item there.

Upvotes: 1

Related Questions