I want to have a button in Outlook that changes the category of the selected Outlook Appointment (while in the calendar window) to the 'Green Category'.
Sub set_to_solved()
Dim objApp As Outlook.Application
Dim citem As Outlook.AppointmentItem
citem.Categories = "Green Category"
End Sub
I'm thinking I'm not referencing the selected item.
Firstly, you never initialize the citem valuable, secondly, you never save the appointment after setting the Categories property.
dim citem As object
for each citem in Application.ActiveExplorer.Selection
if citem.Class = 26 Then 'olAppointment
citem.Categories = "Green Category"
citem.Save
End If
next
Use the Explorer.Selection property which returns a Selection object that contains the item or items that are selected in the explorer window. The location of a selection in the explorer can be in the view list, the appointment list or task list in the To-Do Bar, or the daily tasks list in a calendar view. For more information, see the Location property.
Sub GetSelectedItems()
Dim myOlExp As Outlook.Explorer
Dim myOlSel As Outlook.Selection
Dim oMail As Outlook.AppointmentItem
Dim oPA As Outlook.PropertyAccessor
Set myOlExp = Application.ActiveExplorer
Set myOlSel = myOlExp.Selection
Dim MsgTxt as String
For x = 1 To myOlSel.Count
If myOlSel.Item(x).Class = OlObjectClass.olAppointment Then
' For appointment item, use the Organizer property.
Set oAppt = myOlSel.Item(x)
MsgTxt = oAppt.Organizer
End If
Next
' Debug.Print MsgTxt
MsgBox MsgTxt
End Sub
Related
I have the below code to permanently delete mail from the inbox.
However, when responses to a meeting invite, to say the person has accepted the meeting do not delete.
When I click on that mail and run this code it does not delete?
Sub PermDelete(Item As Outlook.MailItem)
' First set a property to find it again later
Item.UserProperties.Add "Deleted", olText
Item.Save
Item.Delete
'Now go through the deleted folder, search for the property and delete item
Dim objDeletedFolder As Outlook.Folder
Dim objItem As Object
Dim objProperty As Variant
Set objDeletedFolder = Application.GetNamespace("MAPI"). _
GetDefaultFolder(olFolderDeletedItems)
For Each objItem In objDeletedFolder.items
Set objProperty = objItem.UserProperties.Find("Deleted")
If TypeName(objProperty) <> "Nothing" Then
objItem.Delete
End If
Next
End Sub
To run code that has an argument like (Item As Outlook.MailItem) you need to pass the information in this case Item.
You cannot run such code from a button.
You can run Sub delItemPermanently() from a button or F8 to step through.
Option Explicit
Sub delItemPermanently()
' Select a single item
' This line passes the item to PermDelete
PermDelete ActiveExplorer.Selection(1)
End Sub
Sub PermDelete(Item As Object)
' Notice Object not Mailitem
' This will accommodate mailitems as well
...
End Sub
In the code your function accepts an instance of the MailItem class only. But an Outlook folder may contain different types of items - appointments, documents, notes and etc. To differentiate them at runtime you can use the following construction:
Dim obj As Object
If TypeName(obj) = "MailItem" Then
' your code for mail items here
End If
So, you need to declare the function in the following way (if you don't need to do separate actions for different kind of items):
Sub PermDelete(Item As Object)
' First set a property to find it again later
Item.UserProperties.Add "Deleted", olText
Item.Save
Item.Delete
'Now go through the deleted folder, search for the property and delete item
Dim objDeletedFolder As Outlook.Folder
Dim objItem As Object
Dim objProperty As Variant
Set objDeletedFolder = Application.GetNamespace("MAPI"). _
GetDefaultFolder(olFolderDeletedItems)
For Each objItem In objDeletedFolder.items
Set objProperty = objItem.UserProperties.Find("Deleted")
If TypeName(objProperty) <> "Nothing" Then
objItem.Delete
End If
Next
End Sub
Set Item to a generic Object
Example on selected item
Option Explicit
Public Sub Example()
Dim obj As Object
Set obj = ActiveExplorer.Selection.Item(1)
obj.Delete
End Sub
need to determine if a mailitem is open.
I can't open email in modal form because users need to be able to view other emails.
right now I display mailitem.display true which causes a problem, it should be just mailitem.display but then the code will continue.
I need to have a loop that will continue while the email is still opened
You can iterate through all Application.Inspectors windows and check their CurrentItem property which returns an Object representing the current item being displayed in the inspector.
For Explorers check out the Application.Explorers property which returns an Explorers collection object that contains the Explorer objects representing all open explorers.
Sub GetSelectedItems()
Dim myOlExp As Outlook.Explorer
Dim myOlSel As Outlook.Selection
Dim MsgTxt As String
Dim x As Integer
MsgTxt = "You have selected items from: "
Set myOlExp = Application.Explorers.Item(1)
If myOlExp = "Inbox" Then
Set myOlSel = myOlExp.Selection
For x = 1 To myOlSel.Count
MsgTxt = MsgTxt & myOlSel.Item(x).SenderName & ";"
Next x
MsgBox MsgTxt
End If
End Sub
I have written a macro which should move my email conversation to my "TO DO" folder whenever I flag the email as important. I find that the move function does happen, but I get a copy (i.e. the thread shows in both my "TODO"folder and still remains in the "Inbox".
What is also interesting is that in this line of code "For Each MailItem In Conversation.GetRootItems" I would have expected since all the messages do get moved that MailItem>1, but in fact that bit of code only executes one time and then the loop completes. Any thoughts on how to do a true move as opposed to be what appears to be a copy?
''''
Public WithEvents GExplorer As Outlook.Explorer
Public WithEvents GMailItem As Outlook.MailItem
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Set GExplorer = Outlook.Application.ActiveExplorer 'IGNORE THIS'
Dim olNameSpace As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
Set olNameSpace = Application.GetNamespace("MAPI")
Set olFolder = olNameSpace.GetDefaultFolder(olFolderInbox)
Set Items = olFolder.Items
End Sub
Private Sub Items_ItemChange(ByVal Item As Object)
'this item/macro is used to move an email message once it has been flagged
Dim olNameSpace As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
Dim olInbox As Outlook.MAPIFolder
Set olNameSpace = Application.GetNamespace("MAPI")
Set olFolder = olNameSpace.Folders("DEBUG").Folders("TODO")
Stop 'THIS WAS FOR DEBUGGING
If TypeOf Item Is Outlook.MailItem And Item.FlagStatus = olFlagMarked Then
Set Conversation = Item.GetConversation
If Not IsNull(Conversation) Then
' Set ItemsTable = conversation.GetTable
'MsgBox Conversation.GetRootItems.Count
For Each MailItem In Conversation.GetRootItems ' Items in the conversation. ONLY RUNS ONCE'
If TypeOf MailItem Is Outlook.MailItem Then
Item.Move olFolder
End If
Next
End If
End If
End Sub
''''
That is because the same action (move) is repeated for the item changed and passed as a parameter to the ItemChange event handler. Instead, you must run the Move method against the item object in the loop:
For Each MailItem In Conversation.GetRootItems ' Items in the conversation. ONLY RUNS ONCE'
If TypeOf MailItem Is Outlook.MailItem Then
MailItem.Move olFolder
End If
Next
In addition to Eugene's suggestion (use MailItem instead of item when calling Move), you should never use "for each" with Outlook objects in loops that modify the collection you are iterating over - use a down loop from Count to 1 step -1.
Maybe you need to GetChildren of the conversation.
I am attempting to add convenience when adding notes to emails in Outlook.
My plan is to take my current procedure, which adds the notes to the selected email (as an attachment), and have it call a procedure which will set a UserProperty on the MailItem object so that I can easily see which emails have notes attached by adding a custom column to my email list view.
From scouring the internet I have pieced together the following.
Option Explicit
Public Sub MarkHasNote()
Dim Selection As Outlook.Selection
Dim UserDefinedFieldName As String
Dim objProperty As Outlook.UserProperty
Dim objItem As MailItem
UserDefinedFieldName = "Note"
Set objItem = GetCurrentItem()
Set objProperty = objItem.UserProperties.Add(UserDefinedFieldName, Outlook.OlUserPropertyType.olYesNo, olFormatYesNoIcon)
objProperty.Value = True
End Sub
Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Set objApp = Application
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
End Select
Set objApp = Nothing
End Function
I have set a breakpoint and checked the UserProperties of the MailItem. I see that the details are there and the value is set to "True". However, the email does not show the Yes/No icon in the "Note" column of the email pane of Outlook.
How do I get Outlook to show my user defined property value in the email pane when I add the column to the view?
A save is required for a selection. An Inspector item prompts for a save.
Private Sub MarkHasNote_DisplayTest()
' Add the UserProperty column with Field Chooser
' You can view the value toggling when you run through the code
Dim Selection As Selection
Dim UserDefinedFieldName As String
Dim objProperty As UserProperty
Dim objItem As mailItem
UserDefinedFieldName = "NoteTest"
Set objItem = GetCurrentItem()
Set objProperty = objItem.UserProperties.Add(UserDefinedFieldName, Outlook.OlUserPropertyType.olYesNo, olFormatYesNoIcon)
objProperty.Value = Not objProperty.Value
' Required for an explorer selection
objItem.Save
' For an inspector item there would be a prompt to save
' if not already done in the code
End Sub
I have a listener in VBA on my outlook box to perform an action if a receive a mail from a specific email.
The problem is that if I get a error mail (non-delivery email) then my condition is run on a mail which doesn't have that property so my method crashes.
I don't know what the subject may be either.
Does anyone have an idea if I can test if the property exists or if there is another property I can check for to identify if my sender matches?
Many thanks in advance
Sub SetFlagIcon()
Dim mpfInbox As Outlook.Folder
Dim obj As Outlook.MailItem
Dim i As Integer
Set mpfInbox = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Folders("Test")
' Loop all items in the Inbox\Test Folder
For i = 1 To mpfInbox.Items.Count
If mpfInbox.Items(i).Class = olMail Then
Set obj = mpfInbox.Items.Item(i)
If obj.SenderEmailAddress = "someone#example.com" Then
'Set the yellow flag icon
obj.FlagIcon = olYellowFlagIcon
obj.Save
End If
End If
Next
End Sub
Dim obj as a generic Object - there are objects other than MailItem in your Inbox, also to improve your loop try using Items.Restrict Method (Outlook)
Option Explicit
Sub SetFlagIcon()
Dim mpfInbox As Outlook.Folder
Dim obj As Object
Dim Items As Outlook.Items
Dim i As Long
Dim Filter As String
Set mpfInbox = Application.GetNamespace("MAPI").GetDefaultFolder _
(olFolderInbox).Folders("Temp")
Filter = "[SenderEmailAddress] = 'someone#example.com'"
Set Items = mpfInbox.Items.Restrict(Filter)
' Loop all items in the Inbox\Test Folder
For i = 1 To Items.Count
If Items(i).Class = olMail Then
Set obj = Items(i)
'Set the yellow flag icon
obj.FlagIcon = olYellowFlagIcon
obj.Save
End If
Next
End Sub
Items.Restrict Method Applies a filter to the Items collection, returning a new collection containing all of the items from the original that match the filter.