none
outlook : send email : wait for an answer RRS feed

  • Question

  • Good Morning,

    I have this code that when run it launches MS Outlook and the only thing you have to do is to click send.

    I would like to change it so that it loops through a set of records but it should not move to the next record until I click send on outlook. Can it be done? can my code request a acknowledgement from outlook?

    Thank you all

        Private Sub do_email_curr(the_email As String, the_pdf As String, the_inv As String)
            Dim Outlook As Microsoft.Office.Interop.Outlook.Application
            Dim Mail As Microsoft.Office.Interop.Outlook.MailItem
            Dim MMsg As String = "Please find attached Invoice, bla bla bla"

            Outlook = New Microsoft.Office.Interop.Outlook.Application()
            Mail = Outlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
            Mail.To = the_email
            Mail.Subject = "Invoice : " & the_inv
            Mail.Attachments.Add(the_pdf)
            Mail.Body = MMsg
            Mail.ReadReceiptRequested = True

            Mail.Display()
            Outlook = Nothing
            ReleaseComObject(Outlook)
        End Sub

        Private Sub ReleaseComObject(ByVal obj As Object)
            Try
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
                obj = Nothing
            Catch ex As Exception
                obj = Nothing
            End Try
        End Sub

    Friday, March 15, 2019 5:30 PM

All replies

  • Let's say by records you mean from a DataRow in a DataTable then have the DataTable setup as a Private property that is populated say in Form.Load event and a Private integer property.

    The DataTable would have a DataColumn added after loading the DataTable from the database table, using a For-Next statement set the DataColumn field.

    Pressing send looks at the private integer property, let's say its 1, get record with id of 1., send the message, increment the private integer by one. Pressing the send button would use the record id we just obtained.

    Here is how the new column is added to the DataTable.

    someDataTable.Columns.Add(New DataColumn() With {.ColumnName = "MailId", .DataType = GetType(Integer)})
    
    For rowIndex As Integer = 0 To someDataTable.Rows.Count - 1
        someDataTable.Rows(rowIndex).SetField(Of Integer)("MailId", rowIndex + 1)
    Next

    If the data is in a DataSet then access the DataTable via the Tables property of the DataSet. Same will work for say data read in using a TableAdapter, just need to access the underlying DataTable.

    Do I have a code sample, nope as you question was to vague in regards to "records".


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, March 15, 2019 11:57 PM
    Moderator
  • Hi Karen,

    Thank for having the time to answer my post

    I load the data to a datagridview.

    and I have button that will call my sub and process the current DGV record

    Private Sub load_main()
    Try
    Dim app_adpt As New Pervasive.Data.SqlClient.PsqlDataAdapter("select inv_num,cust_name,inv_total,cust_title,cust_cont,cust_email,file_name,cust_id from ar_inv order by inv_num;", app_con)
                Dim app_ds As New DataSet()
                app_adpt.Fill(app_ds)
                Me.dg_main.DataSource = app_ds.Tables(0)
            Catch Ex As Exception
                MsgBox("load_main : " & Err.Description, MsgBoxStyle.Critical, app_cia)
            Finally
                format_main()
            End Try
    End Sub


        Private Sub tsb_email_curr_Click(sender As Object, e As EventArgs) Handles tsb_email_curr.Click
            Dim the_pdf As String = Nothing
            Dim the_email As String = Nothing
    
            With Me.dg_main
                the_pdf = .Rows(.CurrentCell.RowIndex).Cells("file_name").Value
                the_email = .Rows(.CurrentCell.RowIndex).Cells("cust_email").Value.ToString.Trim
            End With
    
            do_email_curr(the_email, "C:\ISTS\PDFS\" & the_pdf & ".PDF", the_pdf)
    
        End Sub


    Saturday, March 16, 2019 3:26 PM
  • Hi,

    Do you want dg_main to move to the next line after each click?

            Dim nowIndex As Integer = Me.dg_main.CurrentRow.Index
            Dim _rowData As Object() = (TryCast(Me.dg_main.DataSource, DataTable)).Rows(nowIndex).ItemArray
            TryCast(Me.dg_main.DataSource, DataTable).Rows(nowIndex).ItemArray = (TryCast(Me.dg_main.DataSource, DataTable)).Rows(nowIndex + 1).ItemArray
            TryCast(Me.dg_main.DataSource, DataTable).Rows(nowIndex + 1).ItemArray = _rowData
            Me.dg_main.CurrentCell = Me.dg_main.Rows(nowIndex + 1).Cells(0)

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 20, 2019 6:19 AM
  • I don't know that I am understanding this correctly. 

    Are you wanting to wait for the user to press send after the .Display?  There is an event that is fired when the mail is sent. 

    I'd change the structure of the program so that outlook is only being started and closed once instead of every time a mail is sent.

    Here is a SWAG

    Imports Microsoft.Office.Interop
    Public Class Form1
    
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            If OutlookApp IsNot Nothing Then
                ReleaseComObject(OutlookApp)
                OutlookApp = Nothing
            End If
        End Sub
    
        Private Sub ReleaseComObject(ByVal obj As Object)
            Try
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
                obj = Nothing
            Catch ex As Exception
                obj = Nothing
            End Try
        End Sub
    
        Private OutlookApp As Microsoft.Office.Interop.Outlook.Application
        Private WithEvents Mail As Microsoft.Office.Interop.Outlook.MailItem
        Private MailItemFini As New Threading.ManualResetEvent(False)
    
        Private Sub do_email_curr(the_email As String, the_pdf As String, the_inv As String)
            Dim MMsg As String = "Please find attached Invoice, bla bla bla"
            If OutlookApp Is Nothing Then
                OutlookApp = New Microsoft.Office.Interop.Outlook.Application()
            End If
            Mail = CType(OutlookApp.CreateItem(Outlook.OlItemType.olMailItem), 
                    Outlook.MailItem)
            Mail.To = the_email
            Mail.Subject = "Invoice : " & the_inv
            Mail.Attachments.Add(the_pdf)
            Mail.Body = MMsg
            Mail.ReadReceiptRequested = True
    
            MailItemFini.Reset()
            Mail.Display()
            MailItemFini.WaitOne() 'wait for send to complete
        End Sub
    
        Private Sub Mail_Close(ByRef Cancel As Boolean) Handles Mail.Close
            If Not MailItemFini.WaitOne(0) Then
                MailItemFini.Set()
            End If
        End Sub
    
        Private Sub Mail_Send(ByRef Cancel As Boolean) Handles Mail.Send
            If Not MailItemFini.WaitOne(0) Then
                MailItemFini.Set()
            End If
        End Sub
    
        Private Sub tsb_email_curr_Click(sender As Object, e As EventArgs) Handles tsb_email_curr.Click
            Dim the_pdf As String = Nothing
            Dim the_email As String = Nothing
    
            With Me.dg_main
                the_pdf = .Rows(.CurrentCell.RowIndex).Cells("file_name").Value.ToString
                the_email = .Rows(.CurrentCell.RowIndex).Cells("cust_email").Value.ToString.Trim
            End With
    
            do_email_curr(the_email, "C:\ISTS\PDFS\" & the_pdf & ".PDF", the_pdf)
    
            '<<<<<<<<<<<<<<<<???????<<<<<<<<<<<<<<<<<<<<
            If Me.dg_main.RowCount > 0 Then
                Dim nextRow As Integer = Me.dg_main.CurrentRow.Index + 1
                If nextRow < Me.dg_main.RowCount - 1 Then
                    Me.dg_main.ClearSelection()
                    Me.dg_main.CurrentCell = Me.dg_main.Rows(nextRow).Cells(0)
                    Me.dg_main.Rows(nextRow).Selected = True
                End If
    
            End If
            '<<<<<<<<<<<<<<<<???????<<<<<<<<<<<<<<<<<<<<
        End Sub
    End Class
    


    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein


    • Edited by dbasnett Wednesday, March 20, 2019 2:58 PM
    Wednesday, March 20, 2019 2:10 PM
  • dbasnett,

    Thank you for answering my post

    It does not loop

    john

    Monday, March 25, 2019 5:53 PM
  • dbasnett,

    Thank you for answering my post

    It does not loop

    john


    What doesn't loop?  My understanding was that there wasn't a loop, that the button click was supposed to send to the next row in the DGV.

    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein

    Monday, March 25, 2019 7:12 PM