Solved

Visual Basic.NET and Lotus Notes Automation

Posted on 2013-06-17
2
377 Views
Last Modified: 2013-10-20
Using vb.net 2008.  I use the code below to automate a Lotus Notes session.  Increasingly, users are getting Runtime COM exceptions due to RPC_E_DISCONNECTED errors.  Any help would be appreciated in resolving the problem.  

Sub Send()

        Dim sess As Object, db As Object, doc As Object, stream As Object, ws As Object, uidoc As Object
        Dim mimeBody As Object, mimeHtml As Object, mimeFile As Object, mimeHeader As Object

        Dim convertMime As Boolean
        Const ENC_QUOTED_PRINTABLE = 1726
        Const ENC_IDENTITY_8BIT = 1729
        Const ENC_NONE = 1725

        Try
            ' Create an email doc
            sess = CreateObject("Notes.NotesSession")
            ws = CreateObject("Notes.NotesUiWorkspace")
            db = sess.GetDatabase("", "", False)

            'Call db.OpenMail()
            If Not db.IsOpen Then db.OpenMail()
            doc = db.CreateDocument()
            Call doc.ReplaceItemValue("Form", "Memo")

            ' add the body as a mime html part
            convertMime = sess.convertMime
            sess.convertMime = False
            stream = sess.CreateStream()

            stream.WriteText(strBody)

            If Attachment = "Y" Then
                Dim profdoc As Object
                Dim sig As Object
                profdoc = db.GetProfileDocument("CalendarProfile")
                sig = profdoc.GetItemValue("Signature")(0) 
                If InStr(1, sig, ".html", vbTextCompare) > 0 Then
                    Dim strFileLoc As String = sig
                    Dim strDir As String
                    strDir = Path.GetDirectoryName(sig) & "\"
                    Dim strFileNm As String
                    strFileNm = Path.GetFileName(sig)
                    Dim oFile As File
                    Dim oRead As StreamReader
                    oRead = oFile.OpenText(sig)
                    sig = oRead.ReadToEnd()

                    stream.WriteText(sig)
                Else
                    stream.WriteText(sig.ToString.Replace(vbCrLf, "<br>")) 
                End If

                stream.WriteText("<br><br>")
             End If

            mimeBody = doc.GetMIMEEntity("Body")

            If mimeBody Is Nothing Then
                mimeBody = doc.CreateMIMEEntity("Body")
            End If

            mimeHtml = mimeBody.CreateChildEntity()
            Call mimeHtml.SetContentFromText(stream, "text/html; charset=""iso-8859-1""", ENC_QUOTED_PRINTABLE)
            Call stream.Close()

            If Attachment = "Y" Then
                If Multi = "Y" Then
                    Dim sqlStr As New StringBuilder
                    Dim dtr As SqlClient.SqlDataReader
                    Dim cn As SqlConnection
                    Dim cmd As SqlCommand

                    cn = New SqlConnection(My.Settings.conn)

                    sqlStr.Append("Select OrderNo ")
                    sqlStr.Append("From cvuNewOrder ")
                    sqlStr.Append("Where CustNo=")
                    sqlStr.Append(CustNoOrderNo)
                    sqlStr.Append(" And OrderDate='")
                    sqlStr.Append(TransDate)
                    sqlStr.Append("'")
                    sqlStr.Append(" And SBU='")
                    sqlStr.Append(SBU)
                    sqlStr.Append("'")

                    cmd = New SqlCommand(sqlStr.ToString, cn)

                    cn.Open()

                    dtr = cmd.ExecuteReader()

                    Do While (dtr.Read())
                        Dim strFilePath As String = ""
                        strFilePath = GetFile(dtr("OrderNo").ToString())
                        If strFilePath.Length > 0 Then
                            mimeFile = mimeBody.CreateChildEntity()
                            mimeHeader = mimeFile.CreateHeader("Content-Transfer-Encoding")
                            Call mimeHeader.SetHeaderVal("binary")
                            mimeHeader = mimeFile.CreateHeader("Content-Disposition")
                            Call mimeHeader.SetHeaderVal("attachment; filename=" & "Order Acknowledgement" & dtr("OrderNo").ToString() & ".pdf")
                            Call stream.Open(strFilePath, "binary")
                            Call mimeFile.SetContentFromBytes(stream, "text/plain", ENC_NONE)
                            Call mimeFile.EncodeContent(ENC_IDENTITY_8BIT)
                            Call stream.Close()
                        End If
                    Loop

                    dtr.Close()
                    dtr = Nothing

                    cn.Close()
                    cn = Nothing
                Else
                    Dim strFilePath As String = ""
                    strFilePath = GetIMSFile(CustNoOrderNo)
                    If strFilePath.Length > 0 Then
                        mimeFile = mimeBody.CreateChildEntity()
                        mimeHeader = mimeFile.CreateHeader("Content-Transfer-Encoding")
                        Call mimeHeader.SetHeaderVal("binary")
                        mimeHeader = mimeFile.CreateHeader("Content-Disposition")
                        Call mimeHeader.SetHeaderVal("attachment; filename=" & "Order Acknowledgement" & CustNoOrderNo & ".pdf")
                        Call stream.Open(strFilePath, "binary")
                        Call mimeFile.SetContentFromBytes(stream, "text/plain", ENC_NONE)
                        Call mimeFile.EncodeContent(ENC_IDENTITY_8BIT)
                        Call stream.Close()
                    End If
                End If
            End If

            Dim strNotesVersion As String
            strNotesVersion = sess.NotesVersion

            'copy generated HTML to clipboard
            If Attachment = "Y" Then
                Call doc.Save(True, False)
                uidoc = ws.EditDocument(True, doc)
                Call uidoc.FieldSetText("EnterSendTo", [To])
                Call uidoc.FieldSetText("EnterCopyTo", [CC])
                Call uidoc.FieldSetText("Subject", Subject)
            Else
                Call doc.Save(True, False)
                uidoc = ws.EditDocument(True, doc)

                Call doc.Remove(True)
                Call uidoc.GotoField("Body")
                Call uidoc.SelectAll()
                Call uidoc.Copy()
                uidoc.Document.SaveOptions = "0"
                uidoc.Document.MailOptions = "0"
                Call uidoc.Close()

                'compose a new memo and paste the body
                uidoc = ws.ComposeDocument(db.Server, db.filePath, "Memo")
                Call uidoc.GotoField("Body")
                Call uidoc.Paste()

                Call uidoc.FieldSetText("EnterSendTo", [To])
                Call uidoc.FieldSetText("EnterCopyTo", [CC])
                Call uidoc.FieldSetText("Subject", Subject)

            End If

            ' bring the window to the front
            Call AppActivate(uidoc.WindowTitle)
        Catch ae As ArgumentException
        Catch io As System.IO.FileNotFoundException
            'Catch comex As System.Runtime.InteropServices.COMException
        Catch ex As Exception
            Dim strError As StringBuilder = New StringBuilder()

            strError.Append("An error has occurred in this application. ")
            strError.Append(Environment.NewLine)
            strError.Append("Please contact your technical support person and ")
            strError.Append(Environment.NewLine)
            strError.Append("tell them this information: SendMail.Send() ")
            strError.Append(Environment.NewLine)
            strError.Append(Environment.NewLine)
            strError.Append(ex.ToString())
            MessageBox.Show(strError.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If Not sess Is Nothing Then
                sess = Nothing
            End If
            If Not ws Is Nothing Then
                ws = Nothing
            End If
            If Not db Is Nothing Then
                db = Nothing
            End If
            If Not uidoc Is Nothing Then
                uidoc = Nothing
            End If
            If Not doc Is Nothing Then
                doc = Nothing
            End If
            If Not stream Is Nothing Then
                stream = Nothing
            End If
            If Not mimeBody Is Nothing Then
                mimeBody = Nothing
            End If
            If Not mimeHtml Is Nothing Then
                mimeHtml = Nothing
            End If
        End Try

    End Sub

Open in new window


COm Exception error details
0
Comment
Question by:waverazor
  • 2
2 Comments
 
LVL 46

Accepted Solution

by:
Sjef Bosman earned 450 total points
ID: 39253303
For what it's worth: http://stackoverflow.com/questions/2421188/comexception-0x80010108-rpc-e-disconnected-when-closing-excel-workbook.

Does the user need interaction with the mail before it gets sent?
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 39586583
A "C"? If it didn't help, you could have the question deleted, although you should have responded to my suggestion and question earlier...
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question