Link to home
Start Free TrialLog in
Avatar of dwe0608
dwe0608Flag for Australia

asked on

VB.NET - Setting the Status of a Message in Outlook

Hi Guys,

Using VB.NET 2017 and Outlook 365 ... the following code is used in an Outlook Office Add-in - after the execution of other code, this code is called to set the status of the Outlook Message ... it throws an exception on  Dim olNS As New Outlook.NameSpace ...

Would someone please assist me in the proper declarations of the Outlook Objects please ....?

    Public Sub SetCustomFlag(objMsg As Outlook.MailItem, olCategory As String, OlFlag As Outlook.OlFlagIcon)
        Dim app As New Outlook.Application
        Dim olNS As New Outlook.NameSpace ' <== Throwing an exception on this line ...
        olNS = app.GetNamespace("MAPI")

        Dim olFol As Outlook.Folder = olNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox)

        'MsgBox olNS.Accounts.Item(1).DisplayName '~~> usually email address
        'MsgBox olNS.Accounts.Item(1).SmtpAddress '~~> email address
        'MsgBox olNS.Accounts.Item(1).UserName '~~> displays the user name
        Try

            With objMsg
                .FlagIcon = OlFlag
                .Categories = olCategory
                '.FlagDueBy = Now + 3
                .FlagRequest = IIf(Len(Trim(olCategory)) > 0, "Saved to PDF " & olNS.Accounts.Item(1).UserName, "") '~~> displays the user name

                '.ReminderSet = True
                '.ReminderTime = Now + 2
                .Save()
            End With

        Catch e As System.Exception
            MsgBox(e.Message, vbCritical, "SetCustomFlag")
        End Try


    End Sub

Open in new window



MTIA

DWE
Avatar of Dorababu M
Dorababu M
Flag of India image

Try changing as below
 
 //Dim olNS As New Outlook.NameSpace ' <== Throwing an exception on this line ...
    //    olNS = app.GetNamespace("MAPI")
Dim olNS As New Outlook.NameSpace = app.GetNamespace("MAPI")

Open in new window

Avatar of dwe0608

ASKER

Hi Dorababu - thanks for the reply ... the following picture shows the error ...

User generated image
Where is app defined?

Dim app As Outlook.Application

Open in new window

Try some thing like this

Dim app As Outlook.Application
app = New Outlook.Application()
Dim olNs As Outlook.NameSpace = app.GetNamespace("MAPI")

Open in new window

Avatar of dwe0608

ASKER

Hi Dorababu - the same error is shown even when I unproc the line Dim app ....
ASKER CERTIFIED SOLUTION
Avatar of Dorababu M
Dorababu M
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dwe0608

ASKER

Hi Dorababu - see pic below
User generated image
Avatar of dwe0608

ASKER

Hi Dorababu - apologies - I removed the word New and inserted () at end of line as follows:

app = New Outlook.Application()
Dim olNs As Outlook.NameSpace = app.GetNamespace("MAPI")

Open in new window

So is it working now?
Avatar of dwe0608

ASKER

Many thanks for the prompt assistance ... appreciate it :-)
Avatar of dwe0608

ASKER

For those that may look at this question, with Dorababu's great assistance, the following function now works:

   
 Public Sub SetCustomFlag(objMsg As Outlook.MailItem, olCategory As String, OlFlag As Outlook.OlFlagIcon)
        Dim app As Outlook.Application
        app = New Outlook.Application()
        Dim olNS As Outlook.NameSpace = app.GetNamespace("MAPI")

        'Dim olFol As Outlook.Folder = olNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox)

        MsgBox(olNS.Accounts.Item(1).DisplayName) '~~> usually email address
        MsgBox(olNS.Accounts.Item(1).SmtpAddress) '~~> email address
        MsgBox(olNS.Accounts.Item(1).UserName) '~~> displays the user name
        Try

            With objMsg
                .FlagIcon = OlFlag
                .Categories = olCategory
                '.FlagDueBy = Now + 3
                .FlagRequest = IIf(Len(Trim(olCategory)) > 0, "Saved to PDF by " & olNS.Accounts.Item(1).UserName & " on " & Format(Now(), "dd MMM YYYY HH:mm"), "") '~~> displays the user name
                '.FlagRequest = IIf(Len(Trim(olCategory)) > 0, "Saved to PDF " & Format(Today(), "dd MMM YYYY HH:mm"), "") '~~> displays the user name
                '.ReminderSet = True
                '.ReminderTime = Now + 2
                .Save()
            End With

        Catch e As System.Exception
            MsgBox(e.Message, vbCritical, "SetCustomFlag")
        End Try


    End Sub

Open in new window