Solved

Visual Basic.NET and Lotus Notes Automation

Posted on 2013-06-17
2
394 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

710 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