PDA

View Full Version : Create an appointment (from MS Project) to a non-default outlook calendar



OfirMarco
07-26-2017, 11:49 PM
Hello,
I am working on some VBA macros that exports Microsoft Project Task's data into a new appointment (on outlook calendar).
It sends an appointment to the added recipients and keeps it on the user's calendar as the organizer.
Some users will export a lot of tasks as appointments but they will not want it to fill in their default calendar.
So i am trying to find a way to add it to a non default calendar.
i have added a calendar names "MSP" below "My calendars" folder.
(i am using outlook 2016 but some of the users in the company are using outlook 2010 if it matters)
https://social.technet.microsoft.com/Forums/getfile/1103153

i tried a few methods to locate the calendar and add the meeting into it but it always places it in the default one.
Below i am adding the VBA code, without what i have tried, if you can help me find or suggest what i should add or what code lines to use to have it on the second calendar it would be very appreciated.
Basically what i need is:


Check if the user has another calendar named "MSP" if not, create it.
once the user has that calendar it should add the appointment to the "MSP" calendar.

Please ignore the Call ReplaceAppointments line, it is calling a procedure that overrides an existing appointment if dates were changed.
You can also ignore the code lines that are reflecting MS Project changes, just mind the outlook lines.
My Code:
Sub Export_Selection_To_Resources_OL_Calendar_Appointments_From_Other_Account()

Dim myOLApp As Outlook.Application
Dim myTask As Task
Dim myItem As Outlook.AppointmentItem
Dim x As Integer
Dim oAccount As Outlook.Account
Dim Ns As Outlook.NameSpace
Dim myDestFolder As Outlook.Folder

Application.Calculation = pjManual
Application.ScreenUpdating = False

On Error Resume Next
Set myOLApp = CreateObject("Outlook.Application")
Set Ns = Application.GetNamespace("MAPI")

For Each myTask In ActiveSelection.Tasks
Set myItem = myOLApp.CreateItem(olAppointmentItem)
With myItem
' Replace existing appointment
Call ReplaceAppointments(myTask.OutlineParent.OutlineParent.Name & " >> " & myTask.OutlineParent.Name & " >> " & myTask.Name & " (Project Task)")

.Start = myTask.Start
.End = myTask.Finish
.Subject = myTask.OutlineParent.OutlineParent.Name & " >> " & myTask.OutlineParent.Name & " >> " & myTask.Name & " (Project Task)"
.Categories = "Exported"
.Body = myTask.Notes
.BusyStatus = olFree
.Location = "TBD"
' .Recipients.Add (myTask.ResourceNames)
.OptionalAttendees = Replace(myTask.ResourceNames, ",", ";")
.Save
.MeetingStatus = 1
.ResponseRequested = True

.Move myDestFolder
.Send
End With

If Not (myTask Is Nothing) Then
myTask.Date1 = myTask.Start
myTask.Date2 = myTask.Finish
myTask.Text25 = "Appointment"
End If
Next myTask

x = MsgBox("All selected tasks exported to resources Outlook Calendar as appointments", vbOKOnly, "Export Completed") = vbOK

Application.Calculation = pjAutomatic
Application.ScreenUpdating = True

End Sub

skatonni
07-27-2017, 01:54 PM
For non-default folders use .Add

If you need more start here http://www.slipstick.com/developer/working-vba-nondefault-outlook-folders/


Set objAppt = newCalFolder.Items.Add(olAppointmentItem)

OfirMarco
07-29-2017, 10:18 PM
Hi,
Thanks for the reply.
I read about .Add, but not so sure how it chooses the specific calendar/folder?
where it points me to the desired "MSP" calendar?

OfirMarco
07-29-2017, 10:42 PM
Hi Again,

I have tried the explanations in the link you attached.
It makes sense but still something is not working right.

19936

As you can see, when i debug and passing this line of code, it still shows the variable as "Nothing".
Can it be something like i am using the wrong type of variable in the declaration?
How should the item be defined? is it calendar type, item type? not sure why nothing is working.

skatonni
07-31-2017, 08:48 AM
Hi Again,

I have tried the explanations in the link you attached.
It makes sense but still something is not working right.

19936

As you can see, when i debug and passing this line of code, it still shows the variable as "Nothing".
Can it be something like i am using the wrong type of variable in the declaration?
How should the item be defined? is it calendar type, item type? not sure why nothing is working.

The Application here is MS Project not Outlook.

You are setting newCalFolder to the items in the folder.

The root problem is the misuse of On Error Resume Next.


Sub Export_Selection_To_Resources_OL_Calendar_Appointments_From_Other_Account()

Dim myOLApp As Outlook.Application

Dim myObj As Object

Dim myItem As Outlook.AppointmentItem

Dim Ns As Outlook.NameSpace
Dim myDestFolder As Outlook.Folder

' Errors are hidden
' Failure is almost guaranteed when misused
'On Error Resume Next

Set myOLApp = CreateObject("Outlook.Application")

' Hidden failure point: Application = MS Project
'Set Ns = Application.GetNamespace("MAPI")

Set Ns = myOLApp.GetNamespace("MAPI")

Set myDestFolder = Ns.GetDefaultFolder(olFolderCalendar)
Set myDestFolder = myDestFolder.Folders("MSP")

' If this is not the default account then
'Set myDestFolder = Ns.Folders("Ofir... / mailbox name / email address of the other account")
'Set myDestFolder = myDestFolder.Folders("Calendar")
'Set myDestFolder = myDestFolder.Folders("MSP")
' This more flexible format can also be used for the default account

' Hidden failure point:
'For Each myTask In ActiveSelection.Tasks

For Each myObj In ActiveExplorer.Selection

' A selection (or folder) could contain any type of item
If myObj.Class = OlTask Then

'Set myItem = myOLApp.CreateItem(olAppointmentItem)
Set myItem = myDestFolder.items.Add(olAppointmentItem)

With myItem
.Subject = myObj.Subject
.Save
End With

End If
Next myObj

Set ActiveExplorer.CurrentFolder = myDestFolder

MsgBox "All selected tasks exported to resources Outlook Calendar as appointments", vbOKOnly, "Export Completed"

End Sub

OfirMarco
07-31-2017, 11:33 PM
The Application here is MS Project not Outlook.

You are setting newCalFolder to the items in the folder.

The root problem is the misuse of On Error Resume Next.

[/CODE]

Thanks for the clarifications about the issues.
The thing is, I have to use MS Project as the source application, the tasks with all the details comes from it.

When you use this loop:
For Each myObj In ActiveExplorer.Selection

' A selection (or folder) could contain any type of item
If myObj.Class = OlTask Then

'Set myItem = myOLApp.CreateItem(olAppointmentItem)
Set myItem = myDestFolder.items.Add(olAppointmentItem)

With myItem
.Subject = myObj.Subject
.Save
End With

End If
Next myObj
It does not loop the Project tasks.
myObj.Class = OlTask --> refers to an outlook task, not Project task.
That way, how can i use the data i need from MS Project (Task Name, Start,Finish, etc.)?

What i need is to loop on all "selected" MS Project tasks and create appointments on a secondary calendar, if there is a resource in resource names column than it should use it to add it as attendee and it will send an invitation to the user.

Also, what is the isse with "On error Resume Next" ?

skatonni
08-01-2017, 05:08 AM
On Error Resume Next is to bypasses known errors where you know what to do with the result. The bypass is turned off with an On Error GoTo 0 once the specific know error situation is over.

I confused myself since I have only Outlook to test with. Ignore the statement about MS project and Application being problem. In any case your efforts are hindered by hiding the error of pointing to items rather than a folder.

If you already have your code creating in the default calendar then you should be able to replace create with add once you correctly reference a folder.



Set myDestFolder = Ns.GetDefaultFolder(olFolderCalendar)
Set myDestFolder = myDestFolder.Folders("MSP")

' If this is not the default account then
'Set myDestFolder = Ns.Folders("Ofir... / mailbox name / email address of the other account")
'Set myDestFolder = myDestFolder.Folders("Calendar")
'Set myDestFolder = myDestFolder.Folders("MSP")
' This more flexible format can also be used for the default account

Set myItem = myOLApp.CreateItem(olAppointmentItem)
Set myItem = myDestFolder.items.Add(olAppointmentItem)

OfirMarco
08-06-2017, 04:38 AM
On Error Resume Next is to bypasses known errors where you know what to do with the result. The bypass is turned off with an On Error GoTo 0 once the specific know error situation is over.

I confused myself since I have only Outlook to test with. Ignore the statement about MS project and Application being problem. In any case your efforts are hindered by hiding the error of pointing to items rather than a folder.

If you already have your code creating in the default calendar then you should be able to replace create with add once you correctly reference a folder.



Set myDestFolder = Ns.GetDefaultFolder(olFolderCalendar)
Set myDestFolder = myDestFolder.Folders("MSP")

' If this is not the default account then
'Set myDestFolder = Ns.Folders("Ofir... / mailbox name / email address of the other account")
'Set myDestFolder = myDestFolder.Folders("Calendar")
'Set myDestFolder = myDestFolder.Folders("MSP")
' This more flexible format can also be used for the default account

Set myItem = myOLApp.CreateItem(olAppointmentItem)
Set myItem = myDestFolder.items.Add(olAppointmentItem)


It still not working well for me.
Still not placing it in the other calendar folder.
When i am debugging and looking at the "locals" after Set myDestFolder = myDestFolder.Folders("MSP") it is showing value as "Nothing" on myDestFolder.
Not identifying as "MSP".