Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 428
  • Last Modified:

Visual Basic.NET and Lotus Notes Automation

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
waverazor
Asked:
waverazor
  • 2
1 Solution
 
Sjef BosmanGroupware ConsultantCommented:
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
 
Sjef BosmanGroupware ConsultantCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now