Help with VBA - Putting a section of txt from excel into an email

peter_z

Board Regular
Joined
Feb 27, 2011
Messages
87
Hey Guys,

I currently have a spreadsheet which is being accessed by multiple users. A copy of the spread sheet is being saved and then sent as an attachment in outlook when you push a send email button. After this the copy is deleted.

There is a drop down box in excel that has "Cancelled" or "30 Days".

Any ideas how to select only a few fields when the user selected Cancelled and put it into the body of the email?

Code:
Sub SendEmails()
Dim aOutlook As Object
Dim aEmail As Object, x As Long
Dim rngeAddresses As Range, rngeCell As Range, strRecipients As String
Dim lngCount As Long
Dim strdate As Date, strTitle As String, strSource As String, StrOutput As String
Dim strImage As String, strImage2 As String
Dim colAttach As Object
Dim oAttach As Object
 
'Set Variables
strUNC = "File Path"
strSource = "Reference"
strTitle = "TitleRef"
'Set File Name
StrOutput = "NAME of File.xls"
 
StrOutput = strUNC & StrOutput
'Set the date for the email title
strdate = Worksheets(strTitle).Range("J8").Value
'Build recipients list
'With Worksheets(StrSource)
'    lngCount = .Range("S1").Value
'    'Debug.Print "lngCount = " & lngCount
'        For x = 1 To lngCount
'            'Debug.Print "x = " & x
'            If x = lngCount Then
'                strRecipients = strRecipients & .Range("P" & x + 1).Value
'            Else
'                strRecipients = strRecipients & .Range("P" & x + 1).Value & "; "
'            End If
'         Debug.Print "strRecipients = " & strRecipients
'         Next x
'End With
'set outlook
Set aOutlook = CreateObject("Outlook.Application")
Set aEmail = aOutlook.CreateItem(0)
'Set colAttach = CreateObject("Outlook.Attachments")
'Set oAttach = CreateObject("Outlook.Attachments")
 
'set Importance
aEmail.Importance = 2
'Set Subject
aEmail.Subject = "Bordereau " & strdate
'Set Body for mail
aEmail.body = " BODY OF EMAIL" 'Change Body Message
size=2>" & _
    "[IMG]http://www.mrexcel.com/forum/<font color=red>cid: & strImage2 & </font>[/IMG] "
    'aEmail.Save
    'aEmail.Display 'fill in the To, Subject, and Send. Or program it in.
'Set attachment
aEmail.Attachments.Add StrOutput
'Set Recipient
'aEmail.To = strRecipients
'Send Mail
aEmail.Display
'Pop up msg box advising who the email has been sent to
'MsgBox ("Email Sent to: " & strRecipients)
 
End Sub
 
Sub SaveWorksheetAsWorkbook()
'Working in 97-2010
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Destwb As Workbook
    Dim sourcewb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim strWorksheet As String
    Dim StrWorksheet2 As String
    Dim strSource As String
    Dim i As Long
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .DisplayAlerts = False
    End With
    strWorksheet = "OUTPUT"
    StrWorksheet2 = "ANALYSIS"
    strSource = "Reference"
 
    'Determine the Excel version and file extension/format
    FileFormatNum = 56
    'Save the new workbook/Mail it/Delete it
 
    'Set the folder it will save into
    TempFilePath = "link to filepath"    
    'Set the name of the file
    TempFileName = "Name of file.xls"          
    Set sourcewb = ActiveWorkbook
 
        'Copy the sheet to a new workbook
    sourcewb.Sheets(Array("STATEMENT", "REPORT")).Copy
 
    Set Destwb = ActiveWorkbook
    With Destwb
 
        .SaveAs TempFilePath & TempFileName & FileExtStr, _
                FileFormat:=FileFormatNum
        Workbooks(TempFileName & FileExtStr).Close True
 
    End With
Application.DisplayAlerts = False
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

Forum statistics

Threads
1,224,587
Messages
6,179,740
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top