Wednesday, 14 July 2010

Appointment in Outlook Express in c# and VB.NET

Something a little more challenging from a Wednesday lunchtime. Here is a little script that you can copy and paste into your page load to create an outlook appointment.

In C# enter the following code :

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Runtime.InteropServices;using Microsoft.Office.Interop.Outlook;using Outlook = Microsoft.Office.Interop.Outlook;
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {
Outlook.ApplicationClass myOutlookApplication = null;
myOutlookApplication = new Outlook.ApplicationClass ();
Outlook.NameSpace myNameSpace = myOutlookApplication.GetNamespace("MAPI");
object myMissing = System.Reflection.Missing.Value ;
myNameSpace.Logon(myMissing, myMissing, myMissing, myMissing);
Outlook.ContactItem myNewContact = (Outlook.ContactItem) myOutlookApplication.CreateItem (Outlook.OlItemType.olContactItem );
myNewContact.FullName = "Helmut Obertanner";System.DateTime myBirthDate = DateTime.Parse ("1.6.1968");myNewContact.Birthday = myBirthDate;myNewContact.CompanyName = "DATALOG Software AG";myNewContact.HomeTelephoneNumber = "49-89-888888888";myNewContact.Email1Address = "
someone@datalog.de";myNewContact.JobTitle = "Developer";myNewContact.HomeAddress = "Frundsbergstr. 60\n80637 Munich";
myNewContact.Save();
Marshal.ReleaseComObject (myNewContact);Outlook.AppointmentItem myNewAppointment = (Outlook.AppointmentItem)myOutlookApplication.CreateItem(Outlook.OlItemType.olAppointmentItem);
DateTime myAppointmentDate = DateTime.Now.AddMinutes (2.0);myNewAppointment.Start = myAppointmentDate;
myNewAppointment.Duration = 60;myNewAppointment.Subject = "Meeting to discuss plans...";
myNewAppointment.Body = "Meeting with Helmut to discuss plans.";myNewAppointment.Location = "Home Office";myNewAppointment.ReminderMinutesBeforeStart = 1;myNewAppointment.ReminderSet = true;
myNewAppointment.Save();
Marshal.ReleaseComObject (myNewAppointment);
Outlook.MailItem myNewMail = (Outlook.MailItem)myOutlookApplication.CreateItem(Outlook.OlItemType.olMailItem);
myNewMail.To = "
AN EMAIL ADDRESS TO SEND TO";myNewMail.Subject = "About our meeting...";myNewMail.Body = "Hi James,\n\n" + "\tI'll see you in two minutes for our meeting!\n\n" + "Btw: I've added you to my contact list!";
myNewMail.Send();
Marshal.ReleaseComObject (myNewMail);
myNameSpace.Logoff ();Marshal.ReleaseComObject (myNameSpace);Marshal.ReleaseComObject (myOutlookApplication);
}}


And the code in VB.NET is :

Sub calendar_date(ByVal theDate As Date)
Dim objcalendarDate As outlook_appt
objcalendarDate = New outlook_appt
Response.Clear()
Response.ContentType = "text/calendar"
Response.AddHeader("content-disposition", "attachment; filename=outlook_calendar_appointment.vcs")
Response.Write(objcalendarDate.outlookAppt(theDate))
Response.End()
End Sub

Private Function str_outlooktime(ByVal dat_time As Date) As String
str_outlooktime = Format(dat_time, "HHmmss")
End Function
Private Function str_outlookdate(ByVal dat_date As Date) As String
str_outlookdate = Format(dat_date, "yyyyMMdd")
End Function
Function outlookAppt(byval theDate As DateTime)
Dim outlookvcs As New System.Text.StringBuilder
Dim subject As String
Dim content As String
Dim startdate As DateTime
Dim enddate As DateTime
subject = "TESTING"
Dim getcode As String
getcode = "=12"
content = "http://localhost:3104/deviant/review_period.aspx?code" & getcode
Dim month As Integer
month = theDate.Month
Dim day As Integer
day = theDate.Day
Dim year As Integer
year = theDate.Year
startdate = New DateTime(year, month, day, 12, 0, 0)
enddate = New DateTime(year, month, day, 19, 30, 0)
With outlookvcs
.Append("BEGIN:VCALENDAR" & vbLf)
.Append("PRODID:-//Microsoft Corporation//Outlook 10.0 MIMEDIR//EN" & vbLf)
.Append("VERSION:1.0" & vbLf)
.Append("BEGIN:VEVENT" & vbLf)
.Append("DTSTART:" & str_outlookdate(startdate) & "T" & str_outlooktime(startdate) & "Z" & vbLf)
.Append("DTEND:" & str_outlookdate(enddate) & "T" & str_outlooktime(enddate) & "Z" & vbLf)
.Append("UID:1" & vbLf)
.Append("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" & content & "=0A" & vbLf)
.Append("SUMMARY;ENCODING=QUOTED-PRINTABLE:" & subject & vbLf)
.Append("PRIORITY:3" & vbLf)
.Append("END:VEVENT" & vbLf)
.Append("END:VCALENDAR" & vbLf)
End With
Return outlookvcs
End Function
OR
Sub outlook_appointment(ByVal code As String, ByVal thedate As DateTime, ByVal startTime As DateTime, ByVal endTime As DateTime)
Dim outlookvcs As New System.Text.StringBuilder
Dim subject As String
Dim content As String
' SET SUBJECT HEADER
subject = "PROJECT REVIEW MEETING"
' SET CONTENT [use '=0D' to make outlook perform a line break]
content = "Please click on the link to review the agenda for the meeting : http://localhost:2446/PM_2/prm_agenda.aspx?code" & "=" & getCode
Dim varStart_hour As String
Dim varStart_minute As String
Dim varStart_minute_st1 As String
varStart_hour = Left(startTime, 2)
varStart_minute_st1 = Left(startTime, 5)
varStart_minute = Right(varStart_minute_st1, 2)
varStart_hour = varStart_hour - 1
Dim varEnd_hour As String
Dim varEnd_minute As String
Dim varEnd_minute_st1 As String
varEnd_hour = Left(endTime, 2)
varEnd_minute_st1 = Left(startTime, 5)
varEnd_minute = Right(varEnd_minute_st1, 2)
varEnd_hour = varEnd_hour - 1

Dim varDate_month As String
Dim varDate_day As String
varDate_month = thedate.Month
varDate_day = thedate.Day
If Len(varDate_day) = 1 Then
varDate_day = "0" & varDate_day
End If
If Len(varDate_month) = 1 Then
varDate_month = "0" & varDate_month
End If
If Len(varEnd_hour) = 1 Then
varEnd_hour = "0" & varEnd_hour
End If
If Len(varStart_hour) = 1 Then
varStart_hour = "0" & varStart_hour
End If
Dim startString As String
Dim endString As String
startString = "DTSTART:" & thedate.Year & varDate_month & varDate_day & "T" & varStart_hour & varStart_minute & "00Z"
endString = "DTEND:" & thedate.Year & varDate_month & varDate_day & "T" & varEnd_hour & varEnd_minute & "00Z"
With outlookvcs
.Append("BEGIN:VCALENDAR" & vbLf)
.Append("PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN" & vbLf)
.Append("VERSION:1.0" & vbLf)
.Append("BEGIN:VEVENT" & vbLf)
.Append(startString & vbLf)
.Append(endString & vbLf)
.Append("UID:1" & vbLf)
.Append("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" & content & "=0A" & vbLf)
.Append("SUMMARY;ENCODING=QUOTED-PRINTABLE:" & subject & vbLf)
.Append("PRIORITY:3" & vbLf)
.Append("END:VEVENT" & vbLf)
.Append("END:VCALENDAR" & vbLf)
End With
Response.Clear()
Response.ContentType = "text/calendar"
Response.AddHeader("content-disposition", "attachment; filename=outlook_calendar_appointment.vcs")
Response.Write(outlookvcs)
Response.End()
End Sub


For the VB.NET code you will need to call the functions from within your page...

Convert string to integer for both C# and VB.NET

Simple one to start, how to convert a string to an integer...

in C# use the following code:

string stringOne;
int integerOne;

stringOne="10";
integerOne = Convert.ToInt32(stringOne);


and in VB.NET :

Dim stringOne as string = nothing
Dim integerOne as integer = nothing

stringOne = "10"
integerOne = Convert.ToInt32(
stringOne
)

and a little update today to include PHP :

$aString = "10";
$aNumber= (int)$aString ;

Hope this helps someone out there...keep tuned for more coming soon.
Just a quick hello. Being straight to the point, I want to use my blog to display all the need to know scripts and codes in both C# and VB.NET. Hopefully in time I will be able to show the comparisons with PHP and maybe other languages in the future. Keep watching this space. Thank you.