dwe0608
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 ....?
MTIA
DWE
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
MTIA
DWE
Where is app defined?
Dim app As Outlook.Application
Try some thing like this
Dim app As Outlook.Application
app = New Outlook.Application()
Dim olNs As Outlook.NameSpace = app.GetNamespace("MAPI")
ASKER
Hi Dorababu - the same error is shown even when I unproc the line Dim app ....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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")
So is it working now?
ASKER
Many thanks for the prompt assistance ... appreciate it :-)
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