Create new appointment on specific outlook calendar - vba

I'm looking for Outlook VBA code that will create a new task on a specific outlook calendar??? For example, when I run the macro a new task opens. When I click "save", it will add the task to my non-default calendar "Notes/Tasks/Reminders".

You need to get the target calendar folder and then use the Items.Add method to create a new appointment there. Read more about all possible ways in the
How To: Create a new Outlook Appointment item article.
Dim appItem as Outlook.AppointmentItem
Dim items as Outlook.Items
Set items = yourCalendarFolder.Items
Set appItem = items.Add(olAppointmentItem)

Related

How can I program Outlook to send an email in advance and send another if a reply isn't sent?

I'd like to program Outlook to send an email in advance, and, if no reply is sent to the target email by x date, send another email.
I've attempted experimentation, dabbling into Excel VBAs, but haven't found a solution.
I'm really quite unsure of how to do this, though I do have programming experience.
I'd like to program Outlook to send an email in advance
That is a very straightforward task. A lot of samples are available over the internet, for example, sample code in VB.NET:
Private Sub CreateSendItem(OutlookApp As Outlook._Application)
Dim mail As Outlook.MailItem = Nothing
Dim mailRecipients As Outlook.Recipients = Nothing
Dim mailRecipient As Outlook.Recipient = Nothing
Try
mail = OutlookApp.CreateItem(Outlook.OlItemType.olMailItem)
mail.Subject = "A programatically generated e-mail"
mailRecipients = mail.Recipients
mailRecipient = mailRecipients.Add("Eugene Astafiev")
mailRecipient.Resolve()
If (mailRecipient.Resolved) Then
mail.Send()
Else
System.Windows.Forms.MessageBox.Show(
"There is no such record in your address book.")
End If
Catch ex As Exception
System.Windows.Forms.MessageBox.Show(ex.Message,
"An exception is occured in the code of add-in.")
Finally
If Not IsNothing(mailRecipient) Then Marshal.ReleaseComObject(mailRecipient)
If Not IsNothing(mailRecipients) Then Marshal.ReleaseComObject(mailRecipients)
If Not IsNothing(mail) Then Marshal.ReleaseComObject(mail)
End Try
End Sub
Read more about that in the following articles:
How To: Create and send an Outlook message programmatically
How To: Fill TO,CC and BCC fields in Outlook programmatically
How to create and show a new Outlook mail item programmatically: C#, VB.NET
if no reply is sent to the target email by x date, send another email.
You can set the following properties on the email:
MailItem.TaskDueDate which sets a Date value that represents the due date of the task for this MailItem.
MailItem.ReminderSet which sets a Boolean value that is True if a reminder has been set for this item.
MailItem.ReminderTime which sets a Date indicating the date and time at which the reminder should occur for the specified item.
In the Application.Reminder event handler you may check whether the mail item was replied or forwarded by reading a low-level property value. The property you would read would be PR_LAST_VERB_EXECUTED (0x10810003). Values are listed below:
EXCHIVERB_REPLYTOSENDER = 102
EXCHIVERB_REPLYTOALL = 103
EXCHIVERB_FORWARD = 104
Please remember that you can use the PropertyAccessor for that:
lastVerbExecuted = mailItem.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x10810003")

Opening normal.dotm user form from document based on template

I have a user form called MetadataForm in my normal.dotm template which is used to collect some custom metadata. It's set to show when a new document is created like this:
Public Sub AutoNew()
Dim meta_form As Normal.MetadataForm
Set meta_form = New Normal.MetadataForm
meta_form.Show
End Sub
It works nicely when I create a new document directly from normal.dotm, but when I create a new document based on any other template I get an error "Object variable or With block variable not set".

Adding attendees to Outlook AppointmentItem (VB.NET)

I am trying to make an application in which i can send appointments to co-workers.
My code is as follows:
Public Sub SendAppointment()
Dim TempApp As Outlook.Application = New Outlook.Application()
'An AppointmentItem 'TempAppItem' object represent one appointment
Dim TempAppItem As Outlook.AppointmentItem = TempApp.CreateItem(Outlook.OlItemType.olAppointmentItem)
TempAppItem.Subject = Onderwerp.SelectedItem
TempAppItem.Body = Opmerking.Text
'Set Location
'TempAppItem.Location = "No Location"
'Set start and end date and times
TempAppItem.Start = Convert.ToDateTime(Btijd.SelectedItem)
TempAppItem.End = Convert.ToDateTime(Etijd.SelectedItem)
'Save to Calendar.
TempAppItem.RequiredAttendees("some#emailaddress.com")
TempAppItem.Send()
TempAppItem.Save()
TempApp = Nothing
TempAppItem = Nothing
End Sub
If I leave out:
TempAppItem.RequiredAttendees("some#emailaddress.com")
It creates the appointment in my own calendar. This proves the rest of the code works. But somehow no matter which argument i add to RequiredAttendees, I keep getting errors. Mostly saying "Too many arguments to 'Property RequiredAttendees As String'.
In my eyes the easiest would be if I can just use the users e-mailaddress as a value to a variable, but somehow it seems vb.net only uses the stored name of said contact, which doesn't seem to work :\
I can't find any explanation of how to properly set the RequiredAttendees property and I am starting to break my head over this. Can anyone help me out or at least push me in the right direction?

word vba: documents.add: put this new document on top

I am using VBA for MS Word. I created a macrofile (docm) to create a new word-document using documents.add....
I want to switch from my macro-document to my new created document on the screen:
Sub test()
Dim MacroDocument As Document
Set MacroDocument = ActiveDocument
Dim newDocument As Document
Set newDocument = Documents.Add
'try to show my macroDocument on the windows screen,
MacroDocument.Select
stop
' now to the new document
newDocument.Select
End Sub
Why doesn't it work?
Any ideas?
document.Select just selects the document but doesn't displays it.
Use MacroDocument.Activate and newDocumente.Activate instead.
When you add a document, the new document automatically becomes the active one and will replace the current one on your screen. Therefore, in most cases the task isn't to make it show but to keep the previous one on top. In such a case making the new document invisible is one of the options you might want to consider.
Set NewDocument = Documents.Add(Visible:=False)

Get All Tasks In Outlook WinForms

I will accept C# code as well. Will just convert it to VB.NET.
I'm having trouble retrieving tasks from outlook.
I have an application that writes a task to outlook.
The application can also mark a task as completed... but this is where my problem comes in.
What I want to achieve at the end is to mark a task as completed in my application and then it should also be marked as completed in outlook.
This is the code I have tried so far to retrieve the tasks, but now I dont know how to iterate through them to be able to mark a specific task as completed:
Dim namespce As Outlook.NameSpace
Dim tasks As Outlook.Items
Dim oApp = New Outlook.Application
namespce = oApp.GetNamespace("MAPI")
tasks = namespce.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks).Items
For Each task As Object In tasks
'From here on I dont know any more
Next
C# code:
foreach(Outlook.TaskItem task in tasks)
{
bool isCompleeted = //Check if your task is compleeted in your application you could use EntryID property to identify a task
if(isCompleeted == true && task.Status != OlTaskStatus.olTaskComplete)
{
task.MarkComplete();
task.Save();
}
}