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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Dorababu MSenior Software EngineerCommented:
Where is app defined?

Dim app As Outlook.Application

Open in new window

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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

No error
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.