Link to home
Start Free TrialLog in
Avatar of jkorz
jkorz

asked on

Create Lotus Notes Appointment From VB

I am working on modifying this code: http://www.fabalou.com/VBandVBA/lotusnotesappointment_ver6.asp
so that I don't have to open an instance of notes to make an appointment.

I can create the document and edit it just fine, but when I save it, it doesn't go on the calendar... any ideas?

Here's the function

Public Function SendNewNotesAppointment(ByVal UserName As String, ByVal Subject As String, ByVal Body As String, ByVal AppDate As Date, ByVal StartTime As Date, ByVal MinsDuration As Integer) As Boolean
    Dim MailDbName As String
    Dim strSTime As String
    Dim strETime As String
    Dim CalenDoc As Object
    Dim NotesDB As Object
    Dim MySession As Object
   
    On Error GoTo SendNotesErr
   
    strSTime = CStr(FormatDateTime(StartTime, vbShortTime))
    strETime = CStr(FormatDateTime(DateAdd("n", MinsDuration, StartTime), vbShortTime))
   
    MailDbName = "mail\" + UserName + ".nsf"
    Set MySession = CreateObject("Lotus.NotesSession")
    Call MySession.Initialize

    Set NotesDB = MySession.GETDATABASE("fsunotes1", MailDbName, False)
    If Not NotesDB.ISOPEN Then Exit Function
    Set CalenDoc = NotesDB.CREATEDOCUMENT
    Call CalenDoc.REPLACEITEMVALUE("Form", "Appointment")
    Call CalenDoc.REPLACEITEMVALUE("AppointmentType", "0")
    Call CalenDoc.REPLACEITEMVALUE("StartDate", CStr(FormatDateTime(AppDate, vbShortDate)))
    Call CalenDoc.REPLACEITEMVALUE("StartTime", strSTime)
    Call CalenDoc.REPLACEITEMVALUE("EndDate", CStr(FormatDateTime(AppDate, vbShortDate)))
    Call CalenDoc.REPLACEITEMVALUE("EndTime", strETime)
    Call CalenDoc.REPLACEITEMVALUE("Subject", Subject)
    Call CalenDoc.REPLACEITEMVALUE("Body", Body)
   
    Call CalenDoc.Save(False, False)
    Exit Function

SendNotesErr:
    SendNewNotesAppointment = False
End Function


Avatar of qwaletee
qwaletee

Hi jkorz,

You need to set CalendarDateTime (same as start date and time).

Best regards,
qwaletee
Oh, and that code really sucks badly, because it sets the date and time values as strings.  They need to be datetime variants.
Avatar of Sjef Bosman
You need a field CalendarDateTime, with the value of the starttime/date as a datetime value.
ASKER CERTIFIED SOLUTION
Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

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
sjef_bosman,

>     Call CalenDoc.ComputeWithForm(True, False)
OUch.  Hate that call.  Really resource intensive, and known to occasionally crash Notes.  So, better yet, don't.  CalendarDateTime should be enough (as we both pointed out).  And again, don't use String variables or CStr conversions.

- qwaletee
ComputeWithForm is indeed a Blunt Axe method, though very effective. If you look at the Properties in an appointment, you see lots of other fields and values in there. I hate surprises, and if there aren't many appointments generated this way...
Avatar of jkorz

ASKER

-qwaletee, if I put this in my code instead of setting dates / times manually:

CALENDOC.REPLACEITEMVALUE "CALENDARDATETIME". CDate(ApptDate & " " & strSTime)

do I use this to set the end time or do I need to make a daterange object?

CALENDOC.REPLACEITEMVALUE "EndTime", CDate(ApptDate & " " & strETime)

btw... I know the code sucks, I basically just took a shot in the dark and substituted REPLACEITEMVALUE for every FIELDSETTEXT in my UI automation version of the code (which sucks even worse, but works)
Avatar of jkorz

ASKER

ok, I got the function working... here's what I have

Public Function SendNewNotesAppointment(ByVal UserName As String, ByVal UserPassword As String, ByVal Subject As String, ByVal Body As String, ByVal ApptDate As String, ByVal StartTime As Date, ByVal MinsDuration As Integer) As Boolean
    Dim MailDbName As String
    Dim strSTime As String
    Dim strETime As String
    Dim CalenDoc As Object
    Dim NotesDB As Object
    Dim MySession As Object
    On Error GoTo SendNotesErr
   
    strSTime = CStr(FormatDateTime(StartTime, vbShortTime))
    strETime = CStr(FormatDateTime(DateAdd("n", MinsDuration, StartTime), vbShortTime))
   
    MailDbName = "mail\" + UserName + ".nsf"
    Set MySession = CreateObject("Lotus.NotesSession")
    Call MySession.Initialize(UserPassword)

    Set NotesDB = MySession.GETDATABASE("fsunotes1", MailDbName, False)
    If Not NotesDB.ISOPEN Then Exit Function
    Set CalenDoc = NotesDB.CREATEDOCUMENT
    CalenDoc.REPLACEITEMVALUE "Form", "Appointment"
    CalenDoc.REPLACEITEMVALUE "AppointmentType", "0"
    CalenDoc.REPLACEITEMVALUE "STARTDATETIME", CDate(ApptDate & " " & strSTime)
    CalenDoc.REPLACEITEMVALUE "CALENDARDATETIME", CDate(ApptDate & " " & strSTime)
    CalenDoc.REPLACEITEMVALUE "EndDateTime", CDate(ApptDate & " " & strETime)
    CalenDoc.REPLACEITEMVALUE "Subject", Subject
    CalenDoc.REPLACEITEMVALUE "Body", Body
    CalenDoc.REPLACEITEMVALUE "MeetingType", 1
    CalenDoc.REPLACEITEMVALUE "$Alarm", 1
    CalenDoc.REPLACEITEMVALUE "$Alarm", -30
   
    CalenDoc.COMPUTEWITHFORM True, False
    CalenDoc.Save False, False
    SendNewNotesAppointment = True
   
    Exit Function
SendNotesErr:
    SendNewNotesAppointment = False
End Function

It didn't work with just the CALENDARDATETIME, it needed the STARTDATETIME set as well... it also wouldn't work without COMPUTEWITHFORM

there a couple other wierd little problems too, when I go to look at the new appointment in the calendar. first of all, the little guy isn't on the left side of the green appointment block in calendar view (and it is for any manually scheduled appts) and the appointment isn't editable... any ideas why this would happen? I did a watch on the document object that I created... it seems to have WAY more items than a manually scheduled document.
Did you look at it when the document was open, or from a view? Opened documents have many more fields, usually.
Avatar of jkorz

ASKER

when the document was open I noticed that it couldn't be updated and when it was in the view, I noticed that there was no little guy. I can't help but think the two are related.
Still an open item for you?

Here's how I go about debugging this sort of thing.  First, do structured text export of a manually created appointment.  Then, try to recreate that appointment using code.  if it does not work, structured text export the automated one, and compare the two exports to see what is different.  Careful of data types!  The structured text export won't clearly show what is datetime or numeric.

To compare the two exports, it pays to export with a line braak of 9999, then sort the two files and use a compare utility (even a brain-dead one like FC).

If you want an "export" that includes data type, you can model it on this:

Open "C:\output.txt" for output as #1
Dim s as new notesSession
Forall item in session.DocumentContext.items
    Print #1 , item.name , item.type , item.text
End Forall
Close #1

the VB equivalent woudl use FOR EACH instead of FORALL, and you would have to get some other way of obtaining the appropriate document and its items -- e.g., session.getDatabase("server","database").getDocumentByNoteID.("1b6a6").items
Avatar of jkorz

ASKER

qwaletee,

Thanks a bunch for that little routine.  A list like that was exactly what I was looking for when I was debugging this function.  The way I did it instead was to just put a watch on it and expand each item in the watch window (which was a real pain).  Too bad I already closed the question because I would have gave you some points for that.  Thanks though,

-jkorz
Avatar of jkorz

ASKER

I didn't realize until just now that this page turns up higher in the search results than the article I wrote explaining this function in detail along with some complementary functions. The article is located at:

http://www.vortexwd.net/lotusnotescalendaring1.asp