Hey all,
I've been using the below code for some time now, and it works great, but I'm looking to automate it. The code sends an email of a range to me directly. However, I'm looking for the code to now reference a cell for an email address, rather than me typing in my address manually. (I want it to automatically CC members based on cell data) Problem being, when I use the range function, it's not working.
I put the problem areas in BOLD. thought's on why this isn't working as intended?
Also, while I'm here... for some reason when it sends the email, it strips it of some of it's formatting. (Column width is incorrect, grid lines show) Any Idea how to instruct it to keep all formatting?
Thanks in advance for any help!
Chris
---------------
I've been using the below code for some time now, and it works great, but I'm looking to automate it. The code sends an email of a range to me directly. However, I'm looking for the code to now reference a cell for an email address, rather than me typing in my address manually. (I want it to automatically CC members based on cell data) Problem being, when I use the range function, it's not working.
I put the problem areas in BOLD. thought's on why this isn't working as intended?
Also, while I'm here... for some reason when it sends the email, it strips it of some of it's formatting. (Column width is incorrect, grid lines show) Any Idea how to instruct it to keep all formatting?
Thanks in advance for any help!
Chris
---------------
Rich (BB code):
Sub Mail_Range()
If MsgBox("Submissions are FINAL. Are you sure you want to submit?", vbYesNo) = vbNo Then Exit Sub
Dim Source As Range
Dim Dest As Workbook
Dim wb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim OutApp As Object
Dim OutMail As Object
Set Source = Nothing
On Error Resume Next
Set Source = Range("G1:N49").SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Source Is Nothing Then
MsgBox "The source is not a range or the sheet is protected, please correct and try again.", vbOKOnly
Exit Sub
End If
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set wb = ActiveWorkbook
Set Dest = Workbooks.Add(xlWBATWorksheet)
Source.Copy
With Dest.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial Paste:=xlPasteValues
.Cells(1).PasteSpecial Paste:=xlPasteFormats
.Cells(1).Select
Application.CutCopyMode = False
End With
TempFilePath = Environ$("temp") & "\"
TempFileName = Range("J3,J11")
If Val(Application.Version) < 12 Then
FileExtStr = ".xls": FileFormatNum = -4143
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With Dest
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
On Error Resume Next
With OutMail
.to = Range("T3")
.CC = ""
.BCC = ""
.Subject = Range("J11")
.Body = " "
.Attachments.Add Dest.FullName
.Send
End With
On Error GoTo 0
.Close savechanges:=False
End With
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
MsgBox ("Form submitted. Please check your sent mail for confirmation and keep for your records.")
End Sub
Last edited by a moderator: