Solved

Visual Basic.NET and Lotus Notes Automation

Posted on 2013-06-17
2
361 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now