Copy and Paste chart into body of email VBA - vba

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

Related

VBA replacing ALL placeholders in Outlook html template with userform textbox and combobox

I'm not a programmer, but I like to create little macros for saving time on iterative tasks. Right now I was trying to create an userform able to replace on an HTML template few placeholders.
The code I've made so far:
Private Sub CommandButton1_Click()
Dim OutApp As Object
Dim OutMail As Outlook.MailItem
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = Application.CreateItemFromTemplate("C:\path\ciccio.oft")
With OutMail
.To = TextBox4.Text
.CC = ""
.BCC = ""
.HTMLBody = Replace(.HTMLBody, "###PLACEHOLDER1###", TextBox1.Text)
.HTMLBody = Replace(.HTMLBody, "###PLACEHOLDER2###", ComboBox1.Text)
Unload UserForm2
.Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
This works good on a plain text body, but on the HTML body just refuses to replace all ..
and seems even a little random.. so something is replaced and the rest not.
Any help?
Solved.
The issue is in how is formatted the html body. I've edited the placeholder words in other file and then copy directly on the template, saving it.

Change placeholders in Subject

I'm trying to create a template where I can have VBA prompt me to change items in the subject. I've a template email with the subject line that should be Project / GC/Client, and in the template I've written that as #0# / #1#.
I'm at the below, but it's erroring.
Private Sub m_Inspector_Activate()
Dim Item As MailItem
Dim Value As String
If TypeOf m_Inspector.CurrentItem Is MailItem Then
Set mail = m_Inspector.CurrentItem
If mail.Subject = "subject" Then
Value = InputBox("Project")
mail.Subject = Replace(mail.Subject, "#0#", Value)
Value = InputBox("GC/Client")
mail.Subject = Replace(mail.Subject, "#1#", Value)
End If
End If
End Sub
This code is used on a template button in Outlook. i.e. the template file it is launching contains the subject line of #0# / #1#. Ideally, after the template launches, the macro prompts the user to update those two fields with the proper subjects.
Sub CommandButton1_Click()
Set MyItem = Application.CreateItemFromTemplate _
("V:\All Folders\Templates\Freebie.oft")
MyItem.Display
End Sub
If all that you really want to do is open a template and replace text in the subject line, this may work for you:
Sub CommandButton1_Click()
Call OpenTemplate
End Sub
Sub OpenTemplate()
Dim OutMail As Outlook.MailItem
Set OutMail = Application.CreateItemFromTemplate("V:\All Folders\Templates\Freebie.oft")
On Error Resume Next
With OutMail
.To = ""
.CC = ""
.BCC = ""
.Subject = Replace(.Subject, "#0#", InputBox("Project"))
.Subject = Replace(.Subject, "#1#", InputBox("GC/Client"))
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
If we can avoid using the Inspector then this is much simpler

VBA Send Mail with TextBox from userform as body content on Windows 10

I've been using the code below without any problems for a while now. I have a Powerpoint slide, that when clicked opens a UserForm. That userform contains a button, named SendMail, and a TextBox.
the user insert comments into the Textbox, clicks SendMail and I get his comments in my e-mail box. This code doesn't seem to work properly, when a Windows10 user hits SendMail. I get an e-mail with a subject but the body is empty.
See below the code that works for any other Windows (Please note EmailBox is the name for the TextBox where the user inputs his questions):
Private Sub SendEmail1_Click()
Dim OutApp As Object
Dim OutMail As Object
Dim strText As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.Body = EmailBox
.To = "random.email#yahey.com"
.CC = ""
.BCC = ""
.Subject = "Question about random stuff"
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Unload Me
End Sub
Anyone has any ideas??
As per Nathan_Sav comment, added .value to the body line and it worked perfectly.
.Body = EmailBox.value
Thanks!

Insert filepath as hyperlink Excel VBA

I have a VBA set of code that generates an email and automatically sends it.
One of the things that I cannot get to work properly is putting a hyperlink to a specified folder location within the email.
Dim fpath As String
fpath = Worksheets("MS_JRNL_OPEN_TU_FR-4333635").Range("AD5").Value
"file://" & fpath & _
Essentially the user has to input a folder location when running the Macro which is in Cell AD5, but I want this is appear as the full folder location as a hyperlink once the email is generated.
Any help would be greatly appreciated
If you are currently using HTMLBody in your email code, it's quite easy to do. I'll assume you are using code similar to below. Take note of strbody and .HTMLBody. Assuming your fpath is formatted like C:\Users\tjb1\Desktop\file.docx then you don't need to add anything else to it. The section creating the hyperlink is "test link". You can change test link to say whatever you want or change the line to "" & fpath & "" to use the path as the link text.
Sub MailURL()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "test link"
On Error Resume Next
With OutMail
.To = "APerson#Somewhere.com"
.Subject = "Testing URL"
.HTMLBody = strbody
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
I found the code above at MrExcel and just formatted it a bit to work with your variable.

VBA in notepad to run code in Excel error

This is the code I have saved in Notepad. Do I need to change Excel.Applications?
Option Explicit
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("H:\shane.xlsm", 0, True)
xlApp.Run "Email"
xlBook.close
xlApp.Quit
Set xlBook = Nothing
Set xlApp = nothing
This is the code I have to send the email and when I test it works fine and will send me an email.
Option Explicit
Const strTo As String = "dvandervieren#enerplus.com"
Const strCC As String = "" '<~~ change "def#abc.com" to "" if you do not want to CC
Const strBCC As String = "" '<~~ change "ghi#abc.com" to "" if you do not want to BCC
Sub Email()
Dim OutApp As Object, OutMail As Object
Dim strbody As String, strSubject As String
strSubject = "Hello World"
strbody = "This is the message for the body"
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = strTo
.CC = strCC
.BCC = strBCC
.Subject = "This is the Subject line"
.Body = strbody
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Open Your excel document. Open the VB Editor. Find the excel document in the left hand window pane. Right click and select Insert>>Module. Move your code into the newly created module. You should then be able to call it using just the method name Email. You do not need to delcare an excel application as you are already inside of excel. – Sorceri