Results 1 to 3 of 3

Pasting Range of Cells into Outlook with Other Text in Body?

Threaded View

  1. #1
    Registered User
    Join Date
    04-14-2014
    Location
    Atlanta, GA, USA
    MS-Off Ver
    Excel 2016
    Posts
    46

    Pasting Range of Cells into Outlook with Other Text in Body?

    Hello! I am still looking for help with this code!

    I've searched for how to do this, including on Ron De Bruin's website, and have yet to see how you can copy a range of cells in Excel from a file into the body of an email that already has text plus attach a separate file to the email. I'm sure it's doable, but I'm way too new at all of this to figure out the correct code to use.

    The cells that need to be copied will always be in a file named SPSS CHECK--Daily ER Production Aggr by Region Type & ER.xlsx in columns A-K, but the number of rows in that range will vary by day. I've identified where I'd want these cells to be pasted in the code below, but with no attempt at coming up with the code for that.

    BTW, is there a way to add a hard space at the end of sentences so that two spaces always appear between the end of one sentence and the beginning of another, or is that an Outlook quirk that strips out extra spaces and reformats the text?

    Here is my code so far:
    Sub SaveToDir()
    ActiveWorkbook.Save
    
    Dim wbk As Workbook
    SaveDir = "F:\GroupShares\Employer Activity by Day\"
    'See if today's date (Now) is a Monday or not.  If Monday, it runs the first SaveName code; otherwise, it runs the second SaveName code.
    If (Weekday(Now(), 2) = 1) Then
        SaveName = "Daily Employer Production by Region, Manager, & Agent Report--" & Format(Now() - 3, "YYYY-MM-DD") & ".xlsx"
    Else
        SaveName = "Daily Employer Production by Region, Manager, & Agent Report--" & Format(Now() - 1, "YYYY-MM-DD") & ".xlsx"
    End If
    
     If Len(Dir(SaveDir & SaveName, vbDirectory)) > 0 Then 'Check to see if the file already exists
       Resp = MsgBox("File name: " & SaveName & vbCrLf & vbCrLf & "already exists in: " & vbCrLf & vbCrLf & SaveDir & vbCrLf & vbCrLf & "Press Okay to continue, Cancel to abort", vbOKCancel)
         If Resp = vbCancel Then
            Exit Sub
         End If
       For Each wbk In Workbooks             'Check to see if the file is open
         If wbk.Name = SaveName Then
            Resp2 = MsgBox(SaveName & " is open. Press OK to close the file or Cancel to abort", vbOKCancel)
         If Resp2 = vbOK Then
            Application.DisplayAlerts = False
            Workbooks(SaveName).Close
         Else
        Exit Sub
       End If
      End If
     Next
    End If
    
    ActiveWorkbook.Sheets.Copy  'Copy all sheets to a new workbook with no code modules
    Set wbk = ActiveWorkbook    'Copied sheets in the new workbook
    Application.DisplayAlerts = False
        wbk.Sheets("Sheet1").Delete
        wbk.SaveAs Filename:=SaveDir & SaveName, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False     'Saves the new file
        MsgBox ("File name:   " & SaveName & vbCrLf & vbCrLf & "has been saved to  " & vbCrLf & vbCrLf & SaveDir)
    
    Workbooks.Open Filename:="F:\GroupShares\Employer Activity by Day\SPSS CHECK--Daily ER Production Aggr by Region Type & ER.xlsx"
        Range("A1").Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlToRight)).Select
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .Weight = xlThin
        End With
    Selection.Copy
    
    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String
        Workbooks(SaveName).Activate
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        strbody = "<HTML><BODY><p style='font-family:calibri;font-size:14.5'>" & "Hi, <br><br>" & _
    "Here is the Daily Employer Production report for " & Application.Text(ActiveSheet.Range("D1"), "mmmm d, yyyy") & _
    ". " & " A summary of the production is below:  <br><br>" & _
    
    '***********This is where I want the pasted cells from the file to go.****************
    
    "If you have any questions regarding the accuracy of this report, please check to be sure numbers were entered correctly.  " & _
    " If there are still problems, please contact me. <br><br>" & _
    "Thanks,<br><br>Bryan<br></p>"
       On Error Resume Next
        With OutMail
            .to = "Person@MyDomain.com"
            .Subject = "Daily Employer Production Report for " & Application.Text(ActiveSheet.Range("D1"), "M/D/YYYY")
            .HTMLbody = strbody & .HTMLbody
            .Attachments.Add ActiveWorkbook.FullName   
            .Display 'or use .Send
        End With
        On Error GoTo 0
        Set OutMail = Nothing
        Set OutApp = Nothing
    Application.DisplayAlerts = True
    End Sub
    Last edited by bryanmarks; 06-05-2014 at 10:08 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Pasting Table Autoformat into Outlook Email Body
    By frolicols in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-11-2014, 11:30 AM
  2. VBA to select a Range of cells as Email Body In Outlook
    By TC922 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-16-2013, 11:06 AM
  3. [SOLVED] VBA Code to select Range of cells as Email Body In Outlook
    By naveenmarapaka in forum Outlook Programming / VBA / Macros
    Replies: 5
    Last Post: 09-16-2013, 06:43 AM
  4. Not sure how to split Outlook Body Text to Single Cells
    By aftabn10 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-17-2012, 08:13 AM
  5. Replies: 7
    Last Post: 08-11-2012, 02:39 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1