Option Explicit
Public gvFile
Sub FilterRecs()
Range("A1").Select
ActiveSheet.UsedRange.Select
Selection.AutoFilter
ActiveSheet.UsedRange.AutoFilter Field:=2, Criteria1:="Arkham"
End Sub
Sub CopyData2Send()
' ExportData2eMail Macro
Dim wsNew As Worksheet
gvFile = "C:\temp\file1email.xlsx"
ActiveSheet.UsedRange.Select
Selection.Copy
Sheets.Add After:=ActiveSheet
Set wsNew = ActiveSheet
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Cells.Select
Cells.EntireColumn.AutoFit
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.249977111117893
.PatternTintAndShade = 0
End With
Range("A1").Select
wsNew.Select
wsNew.Copy
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=gvFile, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False ', ConflictResolution:=True
Application.DisplayAlerts = True
ActiveWindow.Close
Set wsNew = Nothing
End Sub
Public Sub sendGrpEmail()
Dim vTO, vSubj, vBody
Const kbch = 5
Dim i As Integer
'get the filtered data
'CopyData2Send
gvFile = "C:\temp\file1email.xlsx"
'get the target emails
Sheets("emails").Activate 'goto the email list
Range("B2").Select
vSubj = Range("D2").Value
vBody = Range("E2").Value
'collect all emails
While ActiveCell.Value <> ""
vTO = vTO & ActiveCell.Offset(0, 1).Value & ";" & ActiveCell.Offset(0, 2).Value & vbCrLf
ActiveCell.Offset(1, 0).Select 'next row
Wend
'vSubj = "my subject"
'vBody = "my body"
'send the file to target emails
Send1Email vTO, vSubj, vBody, gvFile
End Sub
Public Function Send1Email(ByVal pvTo, ByVal pvSubj, ByVal pvBody, Optional ByVal pvFile) As Boolean
Dim oMail As Outlook.MailItem
Dim oApp As Outlook.Application
On Error GoTo ErrMail
'NOTE : YOU MUST HAVE THE OUTLOOK X.x Object Library REFERENCE CHECKED IN VBE; ctl-G, menu,tools, references, Microsoft Outlook XX Object library
Set oApp = GetApplication("Outlook.Application") 'it may be open already so use this
'Set oApp = CreateObject("Outlook.Application") 'not this
Set oMail = oApp.CreateItem(olMailItem)
With oMail
.To = pvTo
.Subject = pvSubj
If Not IsMissing(pvFile) Then .Attachments.Add pvFile, olByValue, 1
.HTMLBody = pvBody
'If Not IsNull(pvBody) Then .Body = pvBody
.Display True
'.Send
End With
Send1Email = True
endit:
Set oMail = Nothing
Set oApp = Nothing
Exit Function
ErrMail:
MsgBox Err.Description, vbCritical, Err
Resume endit
End Function
Function GetApplication(className As String) As Object
' function to encapsulate the instantiation of an application object
Dim theApp As Object
On Error Resume Next
Set theApp = GetObject(, className)
If Err.Number <> 0 Then
MsgBox "Unable to Get" & className & ", attempting to CreateObject"
Set theApp = CreateObject(className)
End If
If theApp Is Nothing Then
Err.Raise Err.Number, Err.Source, "Unable to Get or Create the " & className & "!"
Set GetApplication = Nothing
End If
'MsgBox "Successfully got a handle on Outlook Application, returning to caller"
Set GetApplication = theApp
End Function
[\code]