Results 1 to 3 of 3

Thread: Outlook automation with Visual Basic

  1. #1
    Join Date
    Apr 2009
    Posts
    47

    Outlook automation with Visual Basic

    Hi friends,

    I love to use Outlook very much so i thought that can we have a Outlook Automation with Visual Basic for this i have ask my friends is possible to that we can control Microsoft Outlook using Automation from Visual Basic, they told me yes everything is possible in Programming but they don't know what would be the code for it.Can anyone provide me the Code for this.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Outlook automation with Visual Basic

    For having Outlook automation with Visual basic you need to follow the below steps:-


    1. Open Visual basic and create a new Standard EXE project.
    2. Over here choose References from Project menu and select Microsoft Outlook.
    3. Now Add a button to your form, double click over it and code the following code


    Code:
     'Start Outlook
     ' If it is already running, you'll use the same instance...
       Dim olApp As Outlook.Application
       Set olApp = CreateObject("Outlook.Application")
        
     ' Logon. Doesn't hurt if you are already running and logged on...
       Dim olNs As Outlook.NameSpace
       Set olNs = olApp.GetNamespace("MAPI")
       olNs.Logon
    
     ' Create and Open a new contact.
       Dim olItem As Outlook.ContactItem
       Set olItem = olApp.CreateItem(olContactItem)
    
     ' Setup Contact information...
       With olItem
          .FullName = "James Smith"
          .Birthday = "9/15/1975"
          .CompanyName = "Microsoft"
          .HomeTelephoneNumber = "704-555-8888"
          .Email1Address = "someone@microsoft.com"
          .JobTitle = "Developer"
          .HomeAddress = "111 Main St." & vbCr & "Charlotte, NC 28226"
       End With
       
     ' Save Contact...
       olItem.Save
        
     ' Create a new appointment.
       Dim olAppt As Outlook.AppointmentItem
       Set olAppt = olApp.CreateItem(olAppointmentItem)
        
     ' Set start time for 2-minutes from now...
       olAppt.Start = Now() + (2# / 24# / 60#)
        
     ' Setup other appointment information...
       With olAppt
          .Duration = 60
          .Subject = "Meeting to discuss plans..."
          .Body = "Meeting with " & olItem.FullName & " to discuss plans."
          .Location = "Home Office"
          .ReminderMinutesBeforeStart = 1
          .ReminderSet = True
       End With
        
     ' Save Appointment...
       olAppt.Save
        
     ' Send a message to your new contact.
       Dim olMail As Outlook.MailItem
       Set olMail = olApp.CreateItem(olMailItem)
     ' Fill out & send message...
       olMail.To = olItem.Email1Address
       olMail.Subject = "About our meeting..."
       olMail.Body = _
            "Dear " & olItem.FirstName & ", " & vbCr & vbCr & vbTab & _
            "I'll see you in 2 minutes for our meeting!" & vbCr & vbCr & _
            "Btw: I've added you to my contact list."
       olMail.Send
        
     ' Clean up...
       MsgBox "All done...", vbMsgBoxSetForeground
       olNS.Logoff
       Set olNs = Nothing
       Set olMail = Nothing
       Set olAppt = Nothing
       Set olItem = Nothing
       Set olApp = Nothing

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Outlook automation with Visual Basic

    You can use this Code for Outlook automation with Visual Basic

    Code:
    using System.Runtime.InteropServices ; 
    using Outlook = Microsoft.Office.Interop.Outlook  ;
    
    // The Outlook Application Object
    Outlook.ApplicationClass myOutlookApplication = null;
    
    // Create the Application Object
    myOutlookApplication = new Outlook.ApplicationClass ();
    
    // Get the Namespace Object
    Outlook.NameSpace myNameSpace = myOutlookApplication.GetNamespace("MAPI");
    
    // Define a missing Object for COM interop
    object myMissing = System.Reflection.Missing.Value ;  
    
    // Logon to Namespace
    myNameSpace.Logon(myMissing, myMissing, myMissing, myMissing);
    
    // Create a new Contact
    Outlook.ContactItem myNewContact = (Outlook.ContactItem) myOutlookApplication.CreateItem (Outlook.OlItemType.olContactItem );
    
    // Setup Contact information...
    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";
    
    // Save Contact
    myNewContact.Save();
    
    // CleanUp
    Marshal.ReleaseComObject (myNewContact);
    
    // Create a new Appointment
    Outlook.AppointmentItemClass myNewAppointment = (Outlook.AppointmentItemClass) myOutlookApplication.CreateItem (Outlook.OlItemType.olAppointmentItem );
    
    // Schedule it for two minutes from now...
    DateTime myAppointmentDate = DateTime.Now.AddMinutes (2.0);
    myNewAppointment.Start = myAppointmentDate;
    
    // Set other appointment info...
    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;
    
    // Save Appointment
    myNewAppointment.Save();
    
    // CleanUp
    Marshal.ReleaseComObject (myNewAppointment);
    
    // Create a Mail Object
    Outlook.MailItemClass myNewMail = (Outlook.MailItemClass) myOutlookApplication.CreateItem (Outlook.OlItemType.olMailItem  ); 
    
    myNewMail.To = "someone@datalog.de";
    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!";
    
    // Send the message!
    	myNewMail.Send();
    
    // CleanUp
    Marshal.ReleaseComObject (myNewMail);
    
    myNameSpace.Logoff ();
    Marshal.ReleaseComObject (myNameSpace);
    Marshal.ReleaseComObject (myOutlookApplication);

Similar Threads

  1. Access Outlook from Visual Basic Application
    By Ebenezer in forum Windows Software
    Replies: 3
    Last Post: 11-06-2009, 07:38 PM
  2. what are Visual Basic IDE?
    By Naresh Modi in forum Software Development
    Replies: 2
    Last Post: 06-03-2009, 09:49 AM
  3. Is GUI same like Visual Basic ?
    By Caesar in forum Software Development
    Replies: 2
    Last Post: 02-03-2009, 01:32 PM
  4. Visual Basic 2005 or Visual Basic 6
    By Aasha in forum Software Development
    Replies: 5
    Last Post: 15-01-2009, 06:56 PM
  5. Visual Basic on LAN
    By djbbenn in forum Software Development
    Replies: 2
    Last Post: 05-08-2008, 02:15 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,161,995.18172 seconds with 17 queries