Im using the below code for making a mail directly from my excel file. This program works perfectly on using the Microsoft Outlook.
The issue is that in office im using Outlook Express (on Windows XP). This program doesnt work there.
It is a request please help me in making it work with Outlook Express even if it stops working with Microsoft Outlook.
Thanks in advance.
Sub Mail_Selection()
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 = Selection.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 = "Order Details for- " & Selection.Cells(2, 2)
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007-2013
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 = ""
.CC = ""
.BCC = ""
.Subject = "Order of " & Selection.Cells(2, 3)
.Body = "Order:" & vbNewLine & _
"Date: " & Selection.Cells(2, 2) & vbNewLine & _
"Manufacturer Name: " & Selection.Cells(2, 1) & vbNewLine & _
"Party Name: " & Selection.Cells(2, 3) & vbNewLine & _
"Quantity: " & Selection.Cells(2, 4) & vbNewLine & _
"Order Details: " & Selection.Cells(2, 5) & vbNewLine & _
"Ex/Deld: " & vbNewLine & _
"Payment: " & Selection.Cells(2, 9) & " Day(s)" & vbNewLine & _
"wb: " & Selection.Cells(2, 6) & vbNewLine & vbNewLine & vbNewLine & _
"Conditions : Please give items wise details along with invoice." & vbNewLine & vbNewLine & vbNewLine & vbNewLine & _
"From," & vbNewLine & _
"Yash Agarwal" & vbNewLine & _
"Bangalore."
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display 'or use .Display
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
End Sub
Bookmarks