Taking input for a VBA script - excel

I've a vba script which emails certain cell range values to particular people. but the problem is I've to edit the body of email every time I need to change something. Is there a way in which I can add a field like : "Enter the email address=" and I could enter the email and the script in the back end take that mail address and I'll be able to send it.
Set CH = ActiveChart CH.Parent.Name = "Rishab"
ActiveSheet.ChartObjects("Rishab").Height = RNG.Height
ActiveSheet.ChartObjects("Rishab").Width = RNG.Width
RNG.CopyPicture xlScreen, xlBitmap CH.Paste CH.Export "P:\ABC.png"
esubject = "Shift Handover_Night"
sendto = "ISHelpdeskTeam#appvion.com"
Set app = CreateObject("outlook.application")
Set itm = app.CreateItem(0)
With itm
.Subject = esubject
.To = sendto
.CC = ccto
.HTMLBody = "<img src='P:\ABC.png'/> <br> <br> Thank You <br> Regards <br> Helpdesk"
.Display
.send
End With
Set app = Nothing
Set itm = Nothing

You could use an input box to prompt the user.
EmailAddress = InputBox("Enter the email address")

Related

Insert table from excel to outlook by keeping format and adding text after the table

I found some VBA code that seems to work exactly the way I want.
However, I don't know how I can add some additional text after the table.
enter image description here
Sub esendtable()
Dim outlook As Object
Dim newEmail As Object
Dim xInspect As Object
Dim pageEditor As Object
Set outlook = CreateObject("Outlook.Application")
Set newEmail = outlook.CreateItem(0)
With newEmail
.To = ""
.CC = ""
.BCC = ""
.Subject = "Data"
.Body = "Please find the requested information" & vbCrLf & "Best Regards"
.Display
Set xInspect = newEmail.GetInspector
Set pageEditor = xInspect.WordEditor
Sheet5.Range("AA2:AB9").Copy
pageEditor.Application.Selection.Start = Len(.Body)
pageEditor.Application.Selection.End = pageEditor.Application.Selection.Start
pageEditor.Application.Selection.PasteSpecial (wdFormatPlainText)
.Display
'.Send
Set pageEditor = Nothing
Set xInspect = Nothing
End With
Set newEmail = Nothing
Set outlook = Nothing
End Sub
I have no experience with VBA and tried to add ".Body = "further text" below the line
"pageEditor.Application.Selection.PasteSpecial (wdFormatPlainText)"
but that did not work.
The Body property doesn't contain any markup, so you can lost any formatting in the message body when dealing with the Body property. Instead, you need to use the HTMLBody property which returns or sets a string representing the HTML body of the specified item. Setting the HTMLBody property will always update the Body property immediately. So, if you need to paste some content after all the data displayed in the message body I'd suggest looking for the tag in the HTML markup and paste the content before the closing tag, so you will be sure it is added after all the content displayed in the message body.

How to send mass emails in Excel?

This is how the message should look in the email:
This is my Excel sheet:
I have an Excel file with columns from A to M, A being Payer, and M being the email address. I need a macro to sort by email address and create an email with the data associated with that email address.
I need a message on the email that reads:
Hello team,
Please review the items below and provide your comments for the invoices due in the account regarding payment details, let us know if there is additional information needed from our end so that we can send it as soon as possible.
That message would have to be able to be changed and my signature at the end.
The data on the columns is:
A Payer
B Name
C Invoice Number
D Bill. Date
E Net due date
F Days past due
G Aging
H Value
I Cust. Mat.
J PO Number
K Bill.Doc.
L Delivery
M Email
I would like to be able to check the emails before sending them.
This is a handy piece of code that includes your default signature in an email.
It is also possible to add attachements (just to keep in mind). You will need to play with it using the .diplay property until it looks pretty and the info you need included is all being pulled in correctly.
You can then create another sub to to iterate though each line of data you have and send those emails.
Option Explicit
Sub EmailIncludingSignature()
Dim eSubject As String
Dim eRecipient As String
Dim eBody As String
Dim SAVELOC As String
Dim objOutlook As Object
Dim objEmail As Object
Dim objAttachment As Object
Dim S As String
'Setup Email
Set objOutlook = CreateObject("Outlook.Application")
Set objEmail = objOutlook.CreateItem(olMailItem)
'Set objAttachment = objEmail.Attachments
'objAttachment.Add SAVELOC & ".pdf"
'Email Signature
S = Environ("appdata") & "\Microsoft\Signatures\"
If Dir(S, vbDirectory) <> vbNullString Then S = S & Dir$(S & "*.htm") Else S = ""
S = CreateObject("Scripting.FileSystemObject").GetFile(S).OpenAsTextStream(1, -2).ReadAll
'Email parameters
eSubject = "Test Email Signature"
eRecipient = "cameron.critchlow#westinbearmountain.com"
eBody = "" & _
"<br>Hi All,<br><br>" & _
"Last line,<br><br>" & S
'Build Email
With objEmail
.To = eRecipient
.CC = ""
.BCC = ""
.Subject = eSubject
.BodyFormat = olFormatHTML ' send plain text message
.HTMLBody = eBody
.Display
'.Send
End With
End Sub
Do you have experience with VBA? does the above make sense?

Select a non-default sender using Excel VBA

How can I send from a secondary Outlook account using Excel VBA?
With OutMail
.to = Text(1)
.CC = Text(2)
.BCC = ""
.Subject =text(3)
.HTMLBody = Text(10)
.Display '.send
End With
I tried ".from".
The below is from a pretty all-purpose sub I use to send emails from VBA.
It takes three parameters:
OutApp is an Outlook.Application object
SendFromAddress is a string variable containing the address to send from
OutMail is the current mailitem object, which you already have as a variable looking at your code.
'Sender Address
If Len(SendFromAddress) > 0 Then
'if directly signed into the account:
For a = 1 To OutApp.Session.accounts.Count
If LCase(OutApp.Session.accounts.Item(a)) Like LCase(SendFromAddress) Then
Outmail.sendusingaccount = OutApp.Session.accounts.Item(a)
SendFromAddress = ""
Exit For
End If
Next
'If not directly signed in (shared mailbox):
If Len(SendFromAddress) > 0 Then Outmail.SentOnBehalfOfName = SendFromAddress
End If

Excel VBA attaching emails within an email

I am working on a project where I need to "draft" 5 emails, attach them to another email, and then send this nested email to a coworker.
My issue is that the 5 attached emails do not show a subject line. I wrote a little tester where the subject lines are also lost during the attaching
Private Sub emailtest()
'Declare Variables
Dim EmailMain As Outlook.MailItem
Dim EmailSub As Outlook.MailItem
Dim j As Long
'Create the main email object
Set EmailMain = Outlook.CreateItem(olMailItem)
With EmailMain
.To = "fake#fakeemail.org"
.Subject = "Testing Main"
.Body = "testing testing"
End With
'creating 5 email objects
For j = 1 To 5
'Create the sub email to be attached to the main email
Set EmailSub = Outlook.CreateItem(olMailItem)
'Details for this sub email
With EmailSub
.To = ""
.Subject = "Test: " & j
.Body = "Testing Nest: " & j
End With
'Attach the email to the main email
EmailMain.Attachments.Add EmailSub
Next j
EmailMain.Display
End Sub
After running this the displayed email has 5 emails, but they are all blank. No subject, no body, nothing.
Any help is appreciated.
You need to save the message being added as an attachment before it is attached:
EmailSub.Save
EmailMain.Attachments.Add EmailSub

Send a second E-Mail automatically once the first E-Mail is sent (Excel-VBA-Code)

I have the following VBA Code to send an E-Mail:
Sub First_Email()
If ExitAll = False Then
Dim OApp As Object, OMail As Object, signature As String
Set OApp = CreateObject("Outlook.Application")
Set OMail = OApp.CreateItem(0)
With OMail
.Display
End With
signature = OMail.HTMLbody
With OMail
.To = "test#test.de"
.Subject = "First E-Mail"
.HTMLbody = "<p> Once I click on the send button for this E-Mail I want that the E-Mail in the code below is sent as well.</p>"
End With
Set OMail = Nothing
Set OApp = Nothing
Else
End If
End Sub
This code opens Outlook with the E-Mail described above. Since the user should still have the chance to modify the E-Mail before it is sent the code does not automatically send the E-Mail.
Once the user clicks the Send Button the E-Mail is sent. Now, I want to achieve that the E-Mail below is automatically send as well once the user clicks the Send Button for the first E-Mail:
Sub Second_Email()
If ExitAll = False Then
Dim OApp As Object, OMail As Object, signature As String
Set OApp = CreateObject("Outlook.Application")
Set OMail = OApp.CreateItem(0)
With OMail
.Display
End With
signature = OMail.HTMLbody
With OMail
.To = "test#test.de"
.Subject = "Second E-Mail"
.HTMLbody = "<p> Once I click on the send button for the first E-Mail I want that this E-Mail is sent as well.</p>"
.send
End With
Set OMail = Nothing
Set OApp = Nothing
Else
End If
End Sub
In contrast to the first E-Mail the user does not have the chance to moidfy the second E-Mail before it is sent. It should immediately be sent once the user clicks the Send Button of the first E-Mail.
Do you know how I can achieve to send a second E-Mail automatically once the user clicks the Send Button in the first E-Mail?
You could use ItemSend in Outlook. In the ThisOutlookSession module.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If Item.To = "test#test.de" Then
If Item.Subject = "First E-Mail" Then
Set OMail = CreateItem(0)
With OMail
.To = "test#test.de"
.Subject = "Second E-Mail"
.HTMLbody = "<p> Once I click on the send button for the first E-Mail I want that this E-Mail is sent as well.</p>"
.send
End With
End If
end if
End Sub
Note: This will run on all mail not just the mail initiated by First_Email in Excel.

Resources