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
LVL 1
dwe0608Asked:
Who is Participating?
 
Dorababu MSenior Software EngineerCommented:
I didn't get anything like that check your function is having end or not

No error
0
 
Dorababu MSenior Software EngineerCommented:
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

0
 
dwe0608Author Commented:
Hi Dorababu - thanks for the reply ... the following picture shows the error ...

Error Demonstration ...
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Dorababu MSenior Software EngineerCommented:
Where is app defined?

Dim app As Outlook.Application

Open in new window

0
 
Dorababu MSenior Software EngineerCommented:
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

0
 
dwe0608Author Commented:
Hi Dorababu - the same error is shown even when I unproc the line Dim app ....
0
 
dwe0608Author Commented:
Hi Dorababu - see pic below
Error shown ...
0
 
dwe0608Author Commented:
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

0
 
Dorababu MSenior Software EngineerCommented:
So is it working now?
0
 
dwe0608Author Commented:
Many thanks for the prompt assistance ... appreciate it :-)
0
 
dwe0608Author Commented:
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.