I am trying to paste screenshots of tables from a number of intranet sites to an email.
Each screenshot is pasted into a new message, whereas I need them all in the same message.
How do I focus on the initial email to paste all screen shots when looping through all the urls?
I referred to Copying/Pasting Multiple Pictures with VBA (Excel to Outlook).
Here is my code:
Sub test()
Dim OutApp As Object
Dim OutMail As Object
Dim ob_urls(1 To 2) As String
Dim item As Variant
'urls
my_urls(1) = "work intranet url 1 - removed"
my_urls(2) = "work intranet url 2 - removed"
Set ie = New InternetExplorerMedium
ie.Visible = True
'loop through urls
For Each item In my_urls
ie.navigate item
'make page wide enough to see tables.
ie.Width = 1400
ie.Visible = True
Do
If ie.readyState = 4 Then
Exit Do
Else
DoEvents
End If
Loop
'delay to load before screen shot, javascript loading
Application.Wait (Now + TimeValue("0:00:7"))
'Print Screen
Application.SendKeys "(%{1068})" '
DoEvents
'Prepare the email
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = "someone#org.com"
.Subject = "test"
.display
.GetInspector.Activate
'Get its Word editor
OutMail.display
Dim wordDoc As Word.document
Set wordDoc = OutMail.GetInspector.WordEditor
Application.SendKeys "(^v)"
' Crop Image
For Each shp In wordDoc.InlineShapes
shp.PictureFormat.CropTop = 200
shp.PictureFormat.CropBottom = 200
Next
End With
On Error GoTo 0
'toggle true/false to refocus
ie.Visible = False
Next item
End Sub
I figured this one out.
Instead of:
'Prepare the email
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
Doing this gets the screenshots all on one email:
If OutApp Is Nothing Then Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
If OutMail Is Nothing Then Set OutMail = OutApp.CreateItem(0)
Peace.
Related
I'm trying to copy/paste the text and charts from a worksheet into an Outlook email. This works for the text in the cells, but not the charts (there are currently two charts, but I may add more later). I also noticed that the wEditor object is empty at runtime:
Sub Mail_Selection_Range_Outlook_Body()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim overDueSht As Worksheet
Set overDueSht = Worksheets("Overdue")
lastRowOverDueSht = overDueSht.Cells(Rows.Count, 3).End(xlUp).Row
On Error Resume Next
Set rng = overDueSht.Range("A1", overDueSht.Cells(lastRowOverDueSht, 10))
On Error GoTo 0
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = "my#email.com"
.CC = ""
.BCC = ""
.Subject = "Overdue Reports"
.HTMLBody = RangetoHTML(rng)
Set wEditor = OutApp.GetInspector.WordEditor
arCharts = Array(1, 2)
For x = 1 To UBound(arCharts)
overDueSht.ChartObjects(arCharts(x)).Activate
ActiveChart.Copy
wEditor.Application.Selection.Start = Len(OutMail.HTMLBody)
wEditor.Paragraphs(1).Range.Paste
wEditor.Application.Selection.End = wEditor.Application.Selection.Start
wEditor.Application.Selection.Paste
Next x
.send
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
I had similar problem as you and i found out that the remedium is to display message before pasting.
EDIT: Add references: Microsoft Outlook and Microsoft Word. It works for me after changes.
Sub Mail_Selection_Range_Outlook_Body()
Dim rng As Range
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim overDueSht As Worksheet
Set overDueSht = Worksheets("Overdue")
lastRowOverDueSht = overDueSht.Cells(Rows.Count, 3).End(xlUp).Row
On Error Resume Next
Set rng = overDueSht.Range("A1", overDueSht.Cells(lastRowOverDueSht, 10))
On Error GoTo 0
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set OutApp = New Outlook.Application
Set OutMail = OutApp.CreateItem(olMailItem)
On Error Resume Next
With OutMail
.To = "my#email.com"
.CC = ""
.BCC = ""
.Subject = "Overdue Reports"
.HTMLBody = "gfdgdsgfds"
.Display
Dim weditor As Word.Document
Set weditor = OutMail.GetInspector.WordEditor
For Each char_t In overDueSht.ChartObjects
char_t.Chart.ChartArea.Copy
weditor.Range(0, 0).Paste
Next
.Send
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
I am attempting to add a chart into the body of my email using GetInspector and WordEditor.
I am having difficulty simultaneously adding text into the body as well. Based on the paragraph position I choose, I can either have the text appear or the chart, but not both at the same time.
Code sample:
Sub generateEmail()
Dim OutApp as Object
Dim OutMail as Object
Dim filePath as String
Dim cht as ChartObject
Dim vInspector as Object
Dim wEditor as Object
Set cht = wsData.ChartObjects("Chart 2")
cht.copy
With wsHome
filePath = ""
'also including an attachment which is working fine
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Set vInspector = OutMail.GetInspector
Set wEditor = vInspector.WordEditor
On Error Resume Next
With OutMail
.to = "All"
.CC = ""
.BCC = ""
.Subject = "Test"
.display
wEditor.Paragraphs(1).Range.Text = "Please see attached"
wEditor.Paragraphs(2).Range.Paste
'if I comment out paragraph 1 and change the second line to paragraph 1
'the chart prints perfectly, but the text does not show
'the way its set up now, only the "Please see attached" shows up
.Attachments.Add (filePath)
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
I'm definitely missing something with the way paragraphs are read with the wordEditor, but have not used it enough to troubleshoot effectively.
On Error Resume Next hides the error that is occurring. Remove it and you'll get
Run-time error '5941': The requested member of the collection doesn't exist.
The email body only has 1 paragraph; you can't paste into paragraph 2 because it doesn't exist. Maybe Add a 2nd paragraph and then paste:
wEditor.Paragraphs(1).Range.Text = "Please see attached"
wEditor.Paragraphs.Add
wEditor.Paragraphs(2).Range.Paste
I am trying to attach a worksheet saved as a pdf as an attachment on an email.
I've managed to figure out how to save pdfs, and I know how to save worksheets as pdfs, but I want to be able to attach the pdf to the email without having a hard copy of the pdf saved down. Is this possible?
Feel free to use this code I found, with a little modification you can get it working quite easily! :)
Private Sub GenerateEmail(TemplateFileName As String, AttachmentFilePath As String, tableRange As Range, subjectText As String, Optional AdditionalCC As String)
Application.ScreenUpdating = False
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
'''' This will use a pre-existing template, otherwise use commented out line below is and cut this line out
Set OutMail = OutApp.CreateItemFromTemplate(TemplateFileName)
'Set OutMail = OutApp.CreateItem(0)
With OutMail
'.Recipients.Add AdditionalCC
'.sendTo = sendToText
'.CC = sendCCText
'.BCC = sendBCCText
'.Subject = subjectText
'''' Use this for current Workbook (ONLY IF SAVED) ie. You'll need to create a function to save the Workbook first
'.Attachments.Add (ActiveWorkbook.FullName)
'''' Use this if you know the location for the saved Workbook
.Attachments.Add (AttachmentFilePath)
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
If you'd like to review the website I acquired this from, click here
I have this code that I can use to send out e-mails with vba in Excel.
Using .body instead of .Inspector.WordEditor I can type exactly what I want to be in the e-mail, but I want the text of the e-mail to be a Word document with some pictures in it and stuff.
How would I go about that? I cannot get .Inspector.WordEditor to work the way that I want it to. (To be honest it does not work at all for me)
Sub Test1()
Dim networkstatus As Boolean
If InternetGetConnectedState(0&, 0&) Then
networkstatus = True
Else
networkstatus = False
End If
If Not networkstatus = True Then
Exit Sub
End If
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
On Error GoTo cleanup
For Each cell In Columns("B").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "?*#?*.?*" And _
LCase(Cells(cell.Row, "C").Value) = "yes" Then
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = cell.Value
.Subject = "Test"
.Inspector.WordEditor ("C:\test.docx")
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
End If
Next cell
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
Edit: I am aware that you can use HTMLBody, but I do not count on my colleagues to use that.
Send it as an attachment instead
.Attachments.Add("C:\test.docx")
I am trying to use the standard code to email a workbook to multiple recipients but build in some error handling that is likely to arise in my model.
If the email address is not available the cell where the email address would be found would read "Pending Search...".
If that is the case the loop simply needs to skip that cell and move on the the next email address.
Below is my code. The issue is coming from the IF/Then/Next line. I am getting a Next without For error. Any input would be greatly appreciated.
Sub Mail_workbook_Outlook_1()
Dim OutApp As Object
Dim OutMail As Object
Dim Position_In_Loop As Long
Dim Total_Emails As Long
Dim Email_Address As String
Dim Dashboard As Worksheet
Set Dashboard = ActiveWorkbook.Worksheets("Dashboard")
Dim Body As Range
Set Body = Dashboard.Range("F13")
Dim Attachment As Range
Set Attachment = Dashboard.Range("F24")
With Dashboard
Total_Emails = Dashboard.Range("G3")
End With
For Position_In_Loop = 1 To Total_Emails
Email_Address = Dashboard.Range("C3").Offset(Position_In_Loop, 0)
If Email_Address = "Pending Search..." Then Next Position_In_Loop
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = "Email_Address"
.CC = ""
.BCC = ""
.Subject = "Open Job Violations"
.Body = "Body"
.Attachments.Add (Attachment)
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Next Position_In_Loop
End Sub
You shouldn't increment the loop like that. You are going to skip emails. Wrap the whole the Outlook mail code in an If statement.
Option Explicit
Sub Mail_workbook_Outlook_1()
Dim OutApp As Object
Dim OutMail As Object
Dim Position_In_Loop As Long
Dim Total_Emails As Long
Dim Email_Address As String
Dim Dashboard As Worksheet
Set Dashboard = ActiveWorkbook.Worksheets("Dashboard")
Dim Body As Range
Set Body = Dashboard.Range("F13")
Dim Attachment As Range
Set Attachment = Dashboard.Range("F24")
With Dashboard
Total_Emails = Dashboard.Range("G3")
End With
For Position_In_Loop = 1 To Total_Emails
Email_Address = Dashboard.Range("C3").Offset(Position_In_Loop, 0)
If Email_Address <> "Pending Search..." Then
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = "Email_Address"
.CC = ""
.BCC = ""
.Subject = "Open Job Violations"
.Body = "Body"
.Attachments.Add Attachment
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End If
Next Position_In_Loop
End Sub
change this line
If Email_Address = "Pending Search..." Then Next Position_In_Loop
to
If Email_Address = "Pending Search..." Then
Position_In_Loop=Position_In_Loop+1
end if