Link to home
Start Free TrialLog in
Avatar of RichardFox
RichardFox

asked on

Creating and sending an Appointment item with VB script

I have a simple web page, to learn how to create and send an appointment item programmatically from a web app. I tried in javascript, but am not making too much progress. Here is what I have so far for VB:

<html>
<head>
<title>Appointment Tester</title>
<script language=vbscript>
                        
      function createapt()
      {
            Set myOlApp = CreateObject("Outlook.Application")
            Set myNameSpace = myOlApp.GetNamespace("MAPI")
            Set myOutBox = myNameSpace.GetDefaultFolder(olFolderOutbox)
            Set myitem = myOlApp.CreateItem(olAppointmentItem)
            myitem.MeetingStatus = olMeeting
            Set recip = myitem.Recipients.Add(@"rfox@myserver.com")
            recip.Type = olRequired
            recip.Resolve
            myitem.Subject = "Come To My Meeting"
            myitem.Start = #8/25/2004 1:30:00 PM#
            myitem.Duration = 5
            myitem.Send
            Set myitem = Nothing
      }
</script>
</head>
<body>
      <form ID="Form1">
            <input type="button" value="Add Appointment" onclick="createapt();" ID="Button1" NAME="Button1">
      </form>
</body>
</html>

Needless to say, it's not working or I wouldn't need you all! Can someone help?
ASKER CERTIFIED SOLUTION
Avatar of Smallint
Smallint

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hamood
hamood

I think you can not send email through simple web page using outlook. You have to use some other technique for sending email. like you can use CDO method for sending messages

Here is sample

Sending email through Active Server Pages is not a difficult task. All you need is love. No, wait; all you need is the Collaborative Data Object, which ships with NT Option Pack 4. To make sure you have it installed, go to Start / Control Panel / Add/Remove Programs / NT Option Pack 4, and see if the SMTP Piece has been installed. If it is *not* installed, you will get an error when you try to run some of the code shown below (the error will read something like, "Invalid class string").

To create an instance of a CDO object in your ASP code, it is as simple as:

<%


Dim objCDO
Set objCDO = Server.CreateObject("CDONTS.NewMail")

%>
Now you're ready to send those emails off! CDONTS.NewMail has a few obvious, easy to use properties and methods. Let's look at some code:

<%


' This code assumes the above CDO
'instantiation code is included
objCDO.To = "mitchell@4guysfromrolla.com"
objCDO.From = "gates@microsoft.com"
objCDO.cc = "barksdale@netscape.com,jobs@apple.com"

Dim txtSubject
txtSubject = "Hello Scott! We were wanting your advice on some programming issues. Please come to Redmond at your earliest convenience for a very fat check!"

objCDO.Subject = "Attn: Scott, we need you!!"
objCDO.Body = txtSubject
objCDO.Send

%>
That's all there is to it. The properties are hopefully fairly obvious. In the above code an email is being sent to mitchelL@4guysfromrolla.com from gates@microsoft.com, and being CC'ed to barksdale@netscape.com and jobs@apple.com. The subject is simply the email message. The send method is what officially dispatches the email (don't forget this, or your email won't go anywhere!!)

CDO is fairly powerful (and bloated). You can send attachments, send to group lists, send schedules (like in Outlook).

I also have written an article which was published on 15Seconds.com dealing with uses of the CDO object. In the article (available through this URL) I discuss marketing through personalized emailing. The article outlines a good use for the CDO object.

Let's examine some of the other features of CDO. Below is a snippet of code which displays some of CDO's extraneous features:

<%

objCDO.To = "someone@xyz.com (John Doe)"
objCDO.From = "me@abc.com (Jane Doe)"
objCDO.bcc = "janedoe@aol.com" 'Blind cc
objCDO.Subject = "My Resume, per Request"
objCDO.Body = "Hello John. Here is a copy of my resume"
objCDO.Importance = 2 'High importance!
objCDO.AttachFile "\\server\jane\resume.doc","Resume.doc"
objCDO.Send 'Send off the email!

'Cleanup
Set objCDO = Nothing

%>

hamood
Avatar of RichardFox

ASKER

hamood, I specifically need to send an Outlook appointment item. I have already sent regular email through SMTP with no problem. But I think you have a good idea that I need CDO for this. Can you tell me something spcific to CDO and olAppointment items?

-rich
Smallint,

getting error "ActiveX component can't create obkect 'Outlook.Application'
Why doesn't this work?

<html>
<head>
<title>Outlook Mail Tester</title>

</head>
<body>
    <script language=vbscript>
    dim objOutlk      'Outlook
    dim objMail      'Email item
    dim strMsg
    const olMailItem = 0
    'Create a new message
          set objOutlk = createobject("Outlook.Application")
          set objMail = objOutlk.createitem(olMailItem)
          objMail.To = "rfox@esops.com"
          objMail.cc = "" 
          objMail.subject = "Any subject"
          objMail.body = "Any message"
          objMail.Send
    'Clean up
    set objMail = nothing
    set objOutlk = nothing
      </script>            
</body>
</html>

I'm agree with hammod that using outlook client objects is not the best solution for sending mail or appointments. A clean solution always will be based on server objects, but it depends on your intention and needings.

Code I posted works at least under win XP, IE 6 and Outlook XP.

If you are getting "ActiveX component can't ..." most probable cause is you have security settings of IE too high. Review this settings, specially ActiveX related settings.