Solved

Visual Basic.NET and Lotus Notes Automation

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

632 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