Tried all other codes on similar pages but failed to work.
This is my current version. Works only if I currently have a new email window open and oddly, my code will paste the .body and cell range details into 2 separate new email windows.
I just want the code to open a new email window with contents .body and cell range details (contains chart). Anybody have any ideas where my code went wrong?
Sub pasting01()
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.TO = "xyz#anc.com"
.CC = "abc#xyz.com"
.Subject = "Test"
.Body = "Dear Mr Lee" & vbNewLine
ActiveSheet.Range("A1:J30").Copy
Set vInspector = OutMail.GetInspector
Set wEditor = vInspector.WordEditor
wEditor.Application.Selection.Start = Len(.Body)
wEditor.Application.Selection.End = wEditor.Application.Selection.Start
wEditor.Application.Selection.Paste
.display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
You have some errors on your code, try using Option Explicit top of your module
Option Explicit
Public Sub pasting01()
Dim Sht As Excel.Worksheet
Set Sht = ThisWorkbook.ActiveSheet
Dim rng As Range
Set rng = Sht.Range("A1:J30")
rng.Copy
Dim OutApp As Object
Set OutApp = CreateObject("Outlook.Application")
Dim OutMail As Object
Set OutMail = OutApp.CreateItem(0)
Dim vInspector As Object
Set vInspector = OutMail.GetInspector
Dim wEditor As Object
Set wEditor = vInspector.WordEditor
With OutMail
.TO = "xyz#anc.com"
.CC = "abc#xyz.com"
.Subject = "Test"
.display
wEditor.Paragraphs(1).Range.Text = "Dear Mr Lee" & vbCr
wEditor.Paragraphs(2).Range.Paste
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Can you mess about with the following to suit your purpose?
Option Explicit
Sub pasting01()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Dim myChart As Chart
Set myChart = ThisWorkbook.Worksheets("Sheet1").ChartObjects("Chart 1").Chart
Dim myPicture As String
Dim fileName As String
Dim myPath As String
myPicture = "Chart1.png"
myPath = "C:\Users\User\Desktop\"
fileName = myPath & myPicture
myChart.Export fileName
With OutMail
.TO = "xyz#anc.com"
.CC = "abc#xyz.com"
.Subject = "Test"
.Body = "Dear Mr Lee" & vbNewLine
.Attachments.Add fileName
.HTMLBody = "<html><p>First Line... </p>" & _
"<img src=cid:" & Replace(myPicture, " ", "%20") & " height=2*240 width=2*180>" & _
"<p>Salutation</p>" & _
"<p>" & "More text" & "</p></html>"
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Kill fileName
End Sub
Result:
Related
I have the following code but it is not working. I am fairly new to VBA as well. The code works to populate the email template but as soon as I add the .Attachment.Add it does not work.
Sub CreateMail()
Dim objOutlook As Object
Dim objMail As Object
Dim rngTo As Range
Dim rngSubject As Range
Dim rngBody As Range
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
With ActiveSheet
Set rngTo = .Range("E2")
Set rngSubject = .Range("E3")
Set rngBody = .Range("E4")
.Attachments.Add "Z:\PHS 340B\Letters of Non-Compliance\..Resources\W9 Form\VPNA W-9 01 09 2017"
End With
With objMail
.to = rngTo.Value
.Subject = rngSubject.Value
.Body = rngBody.Value
.Display 'Instead of .Display, you can use .Send to send the email _
or .Save to save a copy in the drafts folder
End With
Set objOutlook = Nothing
Set objMail = Nothing
Set rngTo = Nothing
Set rngSubject = Nothing
Set rngBody = Nothing
End Sub
Try this:
Sub emailtest()
Dim objOutlook As Object
Dim objMail As Object
Dim rngTo As Range
Dim rngSubject As Range
Dim rngBody As Range
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
With ActiveSheet
Set rngTo = .Range("E2")
Set rngSubject = .Range("E3")
Set rngBody = .Range("E4")
End With
With objMail
.To = rngTo.Value
.Subject = rngSubject.Value
.Body = rngBody.Value
.Attachments.Add "Z:\PHS 340B\Letters of Non-Compliance\..Resources\W9 Form\VPNA W-9 01 09 2017"
.Display 'Instead of .Display, you can use .Send to send the email _
or .Save to save a copy in the drafts folder
End With
Set objOutlook = Nothing
Set objMail = Nothing
Set rngTo = Nothing
Set rngSubject = Nothing
Set rngBody = Nothing
End Sub
You need to use the .Attachments.Add when working within Outlook not Excel.
This simple script should illustrate the point of how to add attachments to an email, and then send the email.
Sub Mail_workbook_Outlook_1()
'Working in Excel 2000-2016
'This example send the last saved version of the Activeworkbook
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.to = "ron#debruin.nl"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = "Hi there"
.Attachments.Add ActiveWorkbook.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Send 'or use .Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
https://www.rondebruin.nl/win/s1/outlook/amail1.htm
I copy a range and paste into Outlook email with signature.
I want to reflect the range in center of body of email.
Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range
Set rng = Nothing
Set rng = ThisWorkbook.Sheets("Output").Range("D7:E18")
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = ""
.BCC = ""
.Subject = "Subject"
.Display
Dim wdDoc As Object
Dim wdRange As Object
Set wdDoc = OutMail.GetInspector.WordEditor
Set wdRange = wdDoc.Range(0, 0)
wdRange.InsertAfter vbCrLf & vbCrLf
rng.Copy
wdRange.Paste
Set OutMail = Nothing
Set OutApp = Nothing
End With
What the code does is, it centers not only the text but also the table (Excel Range) pasted in the outlook body.
Option Explicit
Const wdAlignRowCenter As Integer = 1
Const wdAlignParagraphCenter As Integer = 1
Sub Sample()
Dim OutApp As Object, OutMail As Object
Dim wdDoc As Object, wdRange As Object
Dim rng As Range
Dim i As Long
Set rng = ThisWorkbook.Sheets("Output").Range("D7:E18")
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = ""
.BCC = ""
.Subject = "Subject"
.Display
Set wdDoc = .GetInspector.WordEditor
Set wdRange = wdDoc.Range(0, 0)
wdRange.InsertAfter vbCrLf & vbCrLf
rng.Copy
wdRange.Paste
DoEvents
Set wdRange = wdDoc.Range(0, wdDoc.Characters.Count)
wdRange.ParagraphFormat.Alignment = wdAlignParagraphCenter
For i = 1 To wdRange.Tables.Count
wdRange.Tables(i).Rows.Alignment = wdAlignRowCenter
Next i
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Note:
BTW Set wdDoc = .GetInspector.WordEditor will give you error if the default editor is not MS Word...
If there are images in your body then you may have to handle them as well seperately
Mail is sent when the value in a cell of a specific row changes.
In addition we now want to send an Outlook task whenever that happens. The following first part is the email.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim OutApp As Object, OutMail As Object, strbody As String
If Target.Column = 44 Then
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Text "
On Error Resume Next
With OutMail
.To = Sheets("Param").Cells(3, 4)
.CC = ""
.BCC = ""
.Subject = "Text"
.Body = strbody
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Exit Sub
End If
End Sub
Until here the code works. I've added the part about the task and although the code works without the IF THEN statement I can't get it to trigger with it or I get a 424 error.
Private Sub SendTask()
Dim objOut As Outlook.Application
Dim objTask As Outlook.TaskItem
Dim blnCrt As Boolean
If Target.Column = 6 Then 'modification numéro agrément
On Error GoTo CreateOutlook
Set objOut = GetObject(, "Outlook.Application")
CreateItem:
On Error GoTo 0
Set objTask = objOut.CreateItem(olTaskItem)
With objTask
.Assign
.Subject = "You need to fix this!"
.Body = "Please fix this problem by " & Format(Now + 10, "mm/dd/yy")
.DueDate = CDate(Now + 10)
.Recipients.Add ("youremail#domain.com")
.Display
End With
If blnCrt = True Then objOut.Quit
Set objTask = Nothing
Set objOut = Nothing
Exit Sub
CreateOutlook:
Set objOut = CreateObject("Outlook.Application")
blnCrt = True
Resume CreateItem
End If
End Sub
New version of the code that seems to work as intended
Private Sub Worksheet_Change(ByVal target As Range)
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
If target.Column = 6 Then 'Modification of value in row 6
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olTaskItem)
With OutMail
.Assign
.Subject = "You need to fix this!"
.Body = "Please fix this problem by " & Format(Now + 10, "mm/dd/yy")
.DueDate = CDate(Now + 10)
.Recipients.Add ("youremail#domain.com")
.Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Den numèro d'agrément "
With OutMail
.To = Sheets("Param").Cells(3, 4)
.CC = ""
.BCC = ""
.Subject = "Fichier acquéreur: modification numéro agrément"
.Body = strbody
.Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
Exit Sub
End If
End Sub
I would like to copy a range from protected Excel sheet and paste it into Outlook as a picture.
My code is pasting the text then the picture, but at the same time deleting the text.
How can I paste the picture under the text.
Sub Send_Email()
Dim r As Range
Set r = Range("NR7:OD39")
Dim outlookApp As Outlook.Application
Set outlookApp = CreateObject("Outlook.Application")
Dim OutMail As Outlook.MailItem
Set OutMail = outlookApp.CreateItem(olMailItem)
Dim StrFileName As String
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Sheets("table1").Select
ActiveSheet.Unprotect Password:="blabla"
ActiveSheet.Outline.ShowLevels RowLevels:=8, ColumnLevels:=8
r.Select
r.Copy
OutMail.Display
Dim Email As Word.Document
Set Email = OutMail.GetInspector.WordEditor
With OutMail
.To = "Name.surname#amazon.com"
.CC = "Surname.Name#amazon.com"
.Subject = "Subject"
.Body = "Hi everybody," & vbNewLine & "actual Status"
.Display
End With
Email.Range.PasteAndFormat wdChartPicture
ActiveSheet.Outline.ShowLevels RowLevels:=1, ColumnLevels:=1
ActiveSheet.Protect Password:="blabla"
End Sub
Starting with this line
Set Email = OutMail.GetInspector.WordEditor
this should do it:
Dim ran as Word.Range
Set Email = OutMail.GetInspector.WordEditor
With OutMail
.To = "Name.surname#amazon.com"
.cc = "Surname.Name#amazon.com"
.Subject = "Subject"
.Body = "Hi everybody," & vbNewLine & "actual Status"
.Display
End With
Email.Range.InsertAfter vbCrLf
Set ran = Email.Range(Email.Content.End - 1, Email.Content.End - 1)
ran.PasteAndFormat wdChartPicture
I am working on a VBA script for mailing through Outlook in Excel 2010. Everything runs fine with one exception: the .SentOnBehalfofName line will not work. Here is the complete code
Sub Mail()
' Working in Office 2010-2013
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim strbody As String ' This is for the Body of the email
Dim signature As String ' This is for the email signature
On Error Resume Next
'Set OutMail = Nothing
'Set OutApp = Nothing
Dim sh As Worksheet
Set sh = Sheets("Mail")
strbody = sh.Range("C9").Value
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail ' This inserts the email signature
.Display
End With
signature = OutMail.HTMLBody
With OutMail
'.Display
.To = sh.Range("C5")
.CC = sh.Range("C6")
.BCC = sh.Range("C7")
.Subject = sh.Range("C8").Value
.HTMLBody = "<br>" & strbody & fncRangeToHtml(sh.Range("C13").Value, sh.Range("C14").Value) & signature
.SentOnBehalfOfName = sh.Range("C4").Value
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
If I remove this section the .SentOnBehalfOf works, but I lose my signature line:
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail ' This inserts the email signature
.Display
End With
signature = OutMail.HTMLBody
If I put this back in the code, I get my signature line back, but I lose my ability to send on behalf of another party.
I'm looking for a solution that allows me to do both. Any help would be appreciated.
Here is my solution. I needed to move the .SentOnBehalfOfName to the first statement in the WITH Command, then .Display immediately after that. I replace the string for signature line with .HTMLBody to pull in the signature line. Code runs fine now!
I don't know why the statements need to be in this order, but it works.......
Sub Mail()
' Working in Office 2010-2013
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim strbody As String ' This is for the Body of the email
On Error Resume Next
'Set OutMail = Nothing
'Set OutApp = Nothing
Dim sh As Worksheet
Set sh = Sheets("Mail")
strbody = sh.Range("C9").Value
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.SentOnBehalfOfName = sh.Range("C4")
.Display
.To = sh.Range("C5")
.CC = sh.Range("C6")
.BCC = sh.Range("C7")
.Subject = sh.Range("C8").Value
.HTMLBody = "<br>" & strbody & fncRangeToHtml(sh.Range("C13").Value, sh.Range("C14").Value) & .HTMLBody
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub