Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 27

Thread: How to add table in the outlook via excel.

  1. #1
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location

    Post How to add table in the outlook via excel.

    Hi Experts

    Could you please help me with the following issue.

    I have a macro in place from where I can send email to many people via excel to outlook. There is no issues when the body of the mail if it is a plain text. I am not able to send the table data to the user with this code. Could you please help me with it.

    The body of the mail is in E4 cell of the attached macro. Please let me know the way around for it. I am searching google from last 2 days, but fail to find concrete solution to it.

    Regards,
    Jaggi
    Attached Files Attached Files

  2. #2
    you can make a table, as a string, using html then
    .htmlbody = strtable

  3. #3
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location

    Post

    Quote Originally Posted by westconn1 View Post
    you can make a table, as a string, using html then
    .htmlbody = strtable
    Hi Westconn1

    Sorry for the late reply. Your suggestion for using HTML in the body worked fine. I stuck with one more amendment. I have used Sheet2 tab in the attached macro as a table in the outlook mail. What I am looking for is it possible to change the table data excluding heading for more than one mail. I have added few sample in RAW_DATA sheet in the attached macro. Is it possible for the macro to take the value from RAW_DATA sheet every time and update the table data for each email,

    Regards,
    JD
    Attached Files Attached Files

  4. #4
    Is it possible for the macro to take the value from RAW_DATA sheet every time and update the table data for each email,
    yes of course, you could use a lookup table, elsewhere on the sheet, or on a separate sheet

  5. #5
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location
    Hi Westconn1

    Could you please help me with the code. I really appreciate your help.

    Regards,
    JD

  6. #6
    The principles involved are similar to those described in http://www.gmayor.com/mail_merge_charts.htm. You need two ranges (which I have added to your worksheet) and I have changed the lines so that you can see what is happening in real time. I would also use the Inspector to edit the message directly. The only downside of that is that it must display the messages before sending them. You only actually need one of the macros, you can conditionally add the .Send line as required (see below).

    Option Explicit
    Sub Preview()
        SendEmail False
    lbl_Exit:
        Exit Sub
    End Sub
    
    Sub NoPreview()
        SendEmail True
    lbl_Exit:
        Exit Sub
    End Sub
    
    Sub SendEmail(Optional bNoPreview As Boolean)
    Dim iRec As Long
    Dim OutApp As Object
    Dim OutMail As Object
    Dim olInsp As Object
    Dim wdDoc As Object
    Dim wdRng As Object
    Dim rng As Range
    Dim StrBody As String
    Dim StrBody1 As String
    Dim i As Long
    Dim Subj As String
    Dim FilePath As String
    Dim EmailTo As String
    Dim CCto As String
        With Range("MergeData")
            For i = 1 To .Rows.Count
                Range("MergeRecord") = i - 1
                Set rng = Nothing
                Subj = .Cells(i, "A").Value
                FilePath = .Cells(i, "B").Value
                EmailTo = .Cells(i, "C").Value
                CCto = .Cells(i, "D").Value
                MsgBox Subj
    
                Application.DisplayAlerts = False
                Set rng = Sheets("Sheet2").Range("A1:E2").SpecialCells(xlCellTypeVisible)
                rng.Copy
    
                If rng Is Nothing Then
                    MsgBox "The selection is not a range or the sheet is protected" & _
                           vbNewLine & "please correct and try again.", vbOKOnly
                    Exit Sub
                End If
    
                With Application
                    .EnableEvents = False
                    .ScreenUpdating = False
                End With
    
                Set OutApp = CreateObject("Outlook.Application")
                Set OutMail = OutApp.CreateItem(0)
                StrBody = "Dear Sir," & vbCr & vbCr & _
                          "Please be advised that we have given entry outstanding in our books." & vbCr & vbCr
                StrBody1 = vbCr & vbCr & "We have attached copy document for your reference. Please could you have a look and provide your agreement and settlement date." & vbCr & vbCr & _
                           "Regards," & vbCr & vbCr
                On Error Resume Next
    
                With OutMail
                    .To = EmailTo
                    .CC = CCto
                    .BCC = ""
                    .Subject = Subj
                    .BodyFormat = 2
                    Set olInsp = .GetInspector
                    Set wdDoc = olInsp.WordEditor
                    Set wdRng = wdDoc.Range(0, 0)
                    wdRng.Text = StrBody
                    wdRng.collapse 0
                    wdRng.Paste
                    wdRng.collapse 0
                    wdRng.Text = StrBody1
                    .Attachments.Add FilePath
                    .Display
                    If bNoPreview Then
                        .Send
                    End If
                End With
                On Error GoTo 0
                With Application
                    .EnableEvents = True
                    .ScreenUpdating = True
                End With
                Set OutMail = Nothing
                Set OutApp = Nothing
    
                Application.DisplayAlerts = True
                Sheets("Email_Sheet").Cells(1, "A").Value = "Outlook sent Time, Dynamic msg preview  count  = " & i + 1
            Next i
        End With
    Cleanup:
        Set OutApp = Nothing
        Set OutMail = Nothing
        Set olInsp = Nothing
        Set wdDoc = Nothing
        Set wdRng = Nothing
        Set rng = Nothing
    lbl_Exit:
        Exit Sub
    End Sub
    Attached Files Attached Files
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location
    Hi Graham

    Thanks for the code. I really appreciate your help a big thanks from my end. This fixed my issue which I am trying from last few weeks.

    Regards,
    JD

  8. #8
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location
    Hi Graham

    You have added few data in Email_Sheet. Do I have to follow the same exercise for all the records.

    Regards,
    JD

  9. #9
    The macro uses the data on the first sheet, so you will have to include the extra fields if you want to use them in your e-mail, and include the extra rows in the named range. See my linked web page which covers the theory in a little more depth (though that is inserting a chart and not a worksheet range).
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  10. #10
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location
    Hi Graham

    I tweak the macro to pull the data from RAW_DATA sheet. Thanks for sharing the link it is really useful.

    I want to create the Data in the RAW_DATA sheet dynamic. Currently we have set the default limit for RAW_DATA i.e. from $A2 to $E6. I changed it now. Can we make it dynamic as in depend upon the data in the RAW_DATA sheet the record should set its limit. The count in this sheet should be same in the EMAIL_Sheet. I mean to say the Emails count to send should be the same in the data available in the RAW_Sheet.

    Regards,
    JD
    Attached Images Attached Images
    Attached Files Attached Files

  11. #11
    You can use the Raw_Data sheet as your mergedata range, but if you do that you will need to include in Raw_Data the other fields you want associated with the e-mail (which is why I added the extra fields to sheet 1). As you will have to add that data somewhere in your worksheet, it seems sensible to look it up and add it to the Raw_Data file, and increase the size of the range.

    As you want Raw_Data to be dynamic, then you will have to set the scope of the named range in code at the start of the main macro before using it to merge.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  12. #12
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location
    Hi Graham

    I just do not want the macro to bydefault create the number of email. Like if I set the range from A2-E10 as an example and the data in the RAW_DATA sheet is from A2-E6 then the macro also creates few more empty email to complete its limit from A2-E10. I just want the macro to create the number of email depend upon the count of entries available in the RAW_Data Sheet. The count of entries in the RAW_DATA sheet well vary every day from 30 - 80. The macro should create the email count depend upon the these entries.

    Sorry for troubling you so much. Please help me to set trigger on the RAW_Data sheet.

    Regards,
    JD

  13. #13
    You are missing my point. The MergeData named range must be limited to the range you want to work with. If you are using Raw_Data and the number of rows is dynamic, then you must set the named range in the macro to encompass only the rows with data e.g call the following macro before running the rest. You have also changed the loop for no obvious purpose that I can see?


    Sub SetRange()
    Dim xlSheet As Worksheet
    Dim LastRow As Long, LastCol As Long
    Dim rng As Range
        Set xlSheet = Sheets("RAW_Data")
        With xlSheet
            LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
            LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
            Application.Calculation = xlManual
            Names("MergeData").Delete
            ActiveWorkbook.Names.Add _
                    Name:="MergeData", _
                    RefersToR1C1:="=RAW_Data!R2C1:R" & LastRow & "C" & LastCol
            Application.Calculation = xlAutomatic
        End With
        Set x;Sheet = Nothing
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  14. #14
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location

    Post

    Hi Graham

    Sorry for the confusion. I was trying to change the code before, but now undo it with the one you provided to me. I am calling the above macro before running the other mocaro, but the information in the mail is getting mixed up. Please find the version of macro attached and let me know where I am making error in the code.

    Regards,
    JD
    Attached Files Attached Files

  15. #15
    As I said in my last two messages, you can either merge the data on the RAW_DATA page, (in which case you need to set the values of the table you wish to merge on Sheet 2 to reflect the columns of the MergeData range) or you can merge the data on the front page. Either way you must have ALL the fields for each record in the named range, as I showed in the first example I returned - which worked!
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  16. #16
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location

    Post

    Hi Graham

    Sorry for the trouble, but I am getting confuse here. I have no issue with the concept of pulling data from RAW_Data sheet to Email_Sheet and then filling the table in Sheet2. For Merge data we have already created "Mergedata" in "New manger" before and now we are creating a macro to set the dynamic range.

    When I trying to call the "SetRange" macro before the "Preview" macro the information in the outlook sets incorrect, but when I am trying without "SetRange" macro, the result is fine. I am really what are we doing here and why we have "Mergedata" in the "New Manager" field in excel. I have attached the updated macro with the mail. Please let me know how we can set the range dyanamic and the output is as per our expectation. We are going with the file you have provided me yesterday.

    Regards,
    JD
    Attached Files Attached Files

  17. #17
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location
    Oops sent you incorrect file..
    Attached Files Attached Files

  18. #18
    OK, I see what you mean. The problem now is that you have the named range on the Raw_Data range, whereas with this arrangement you need it to be the range of e-mail fields on the first page. This means changing the SetRange macro to use that worksheet and range. See attached.
    Attached Files Attached Files
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  19. #19
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location

    Post

    Hi Graham

    You are a champ!

    Thanks for all your help and sorry for the trouble.

    I was surfing from morning to check how can I set a default folder link for attachment in this macro.

    I will place all my files which needs to be attached with the mail with the name in the RAW_Data sheet - Column A - Ref

    Is it possible to check the folder with the Ref number and if the attach file is available attached it if not throw an error msg that the file is not available. I find - http://www.vbaexpress.com/forum/show...mail-using-VBA but will it work with the code of this macro.

    Really sorry for all the trouble.

    Regards,
    JD

  20. #20
    The following function will respond with True or False. You can call it as required to determine whether the files are present.

    Public Function FileExists(ByVal Filename As String) As Boolean
    Dim lngAttr As Long
        On Error GoTo NoFile
        lngAttr = GetAttr(Filename)
        If (lngAttr And vbDirectory) <> vbDirectory Then
            FileExists = True
        End If
    NoFile:
        Exit Function
    End Function
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •