Search code examples
excelvbalotusrichtext

Runtime Error 13 - Type Mismatch when trying to edit richtext item of Lotus Notes


I'm trying to edit an existing notes document via VBA and then send it automatically.

I've already created pretty much everything - just need to figure out how exactly I can add a certain text at a certain position within the richtext element.

Sub sendMail() 'inputIndID As String, inputRecipient As String, inputIncDescription As String)

Dim mailDB As Object
Dim mailToSend As Object
Dim body As Object
Dim session As Object
Dim view As Object
Dim entries As Object
Dim docIDs() As String
Dim docSubjects() As String
Dim incID, incDescription As String
Dim element As String
Dim bodyNavigator As Object

incID = "<INC-ID>"
incDescription = "<INC-Betreff>"

'Start a session to notes
Set session = CreateObject("Notes.NotesSession")
'This line prompts for password of current ID noted in Notes.INI
'Call Session.Initialize
'or use below to supply password of the current ID

'Open the mail database in notes
Set mailDB = session.GetDatabase("Eschen10/Presta", "mail\qcpcsupport.nsf")
If mailDB.IsOpen = False Then
    Call mailDB.Open
End If

'Search for all the messages in the folder "Umfrage"
Set view = mailDB.GetView("Umfrage")
Set entries = view.AllEntries
If entries.Count = 0 Then
    MsgBox "Keine Nachricht im Umfrage Ordner."
End If
ReDim docIDs(entries.Count - 1)
ReDim docSubjects(entries.Count - 1)
Set entry = entries.GetFirstEntry
Do Until entry Is Nothing
    docIDs(i) = entry.NoteID
    docSubjects(i) = entry.Document.GetItemValue("Subject")(0) 'based on standard R5 mail template column order
    'If the documents title matches the searched one it will be taken and worked with later
    If docSubjects(i) = "Umfrage PC-Support Servicequalität" Then
        Set mailToSend = entry.Document
    End If
    i = i + 1
    Set entry = entries.GetNextEntry(entry)
Loop


'Set the recipient
Call mailToSend.ReplaceItemValue("SendTo", "[email protected]")

'Get and change the body content
Set body = mailToSend.GetFirstItem("Body")
Set bodyNavigator = body.CreateNavigator()

'Replace markers with correct text

element = "<"

If (body.Type = RICHTEXT) Then
    Call bodyNavigator.FindFirstString(element)
    Call body.BeginInsert(bodyNavigator, True)
    Call body.AppendText("123456")
    Call bodyNavigator.FindNextString(element)
    Call body.BeginInsert(bodyNavigator, True)
    Call body.AppendText("Antrag Guest WLAN")
End If


'Example to save the message (optional)
mailToSend.SaveMessageOnSend = True

'Send the document
'Gets the mail to appear in the Sent items folder
mailToSend.Save True, False
Call mailToSend.ReplaceItemValue("PostedDate", Now())
Call mailToSend.Send(False)

'changes the body back and saves the document in the folder "Umfrage" so it can be resent next time
Call mailToSend.PutInFolder("Umfrage")

'Clean Up
Set mailDB = Nothing
Set mailToSend = Nothing
Set body = Nothing
Set session = Nothing


End Sub

Currently I am failing at the following line:

Call body.BeginInsert(bodyNavigator, True)

I get the error - Runtime Error 13 - Type Mismatch

I also already tried to give all variables the correct data type of Lotus Notes - but then I have the problem with each of those variables.

Is there a way I can "force" the bodynavigator to be of the correct type? Or where do I have my mistake? Am I missing a library or anything?

Thanks in advance!!!

Regards, Simon


Solution

  • Did you read the documentation for NotesRichtextNavigator?

    You find the following information there:

    Parameters
    target$

    String. The search string.

    options$

    Long. Any of the following. Specify multiple options by combining them with addition or logical ORs.

    • RT_FIND_ACCENTINSENSITIVE (4) (default is accent sensitive)
    • RT_FIND_CASEINSENSITIVE (1) (default is case sensitive)
    • RT_FIND_PITCHINSENSITIVE (2) (default is pitch insensitive)

    So: Your second parameter "true" is simply the wrong type... therefor the type mismatch...