Email button

eliozo

Board Regular
Joined
Oct 22, 2010
Messages
80
Hello! i need a button where it will ask me to specify a cell. For example, i specified cell A1. Therefore automatically it will open the outlook and prepare an email with the content of A1, B1, C1, D1as per below. (3 other cells beside the one selected).
ps: ignore AW30 and AW31
Thank you!!

VBA Code:
Private Sub CommandButton196_Click()

Dim objOutlook As Object

Dim objNameSpace As Object

Dim objOlAccount As Object

Dim objMailItem As Object

Dim Recip As String

Dim Send As String

Dim rng As Range



On Error Resume Next

Set objOutlook = GetObject(, "Outlook.Application")

On Error GoTo 0



If objOutlook Is Nothing Then

Set objOutlook = CreateObject("Outlook.Application")

Set objNameSpace = objOutlook.GetNamespace("MAPI")

End If



Recip = "URGENT REMINDER! " & "Request # " & [A1].Value & " " & "Unit: " & [B1].Value & " " & "Description: " & [C1].Value & " " & "Request Date: " & [D1].Value & " Thank You!"

Send = [AW30].Value

SendCC = [AW31].Value



Set objMailItem = objOutlook.createitem(0)



With objMailItem

.To = Send

.CC = SendCC

.BCC = ""

.Subject = "Urgent Maintenance Request"

.Body = Recip



.display

End With



Set objMailItem = Nothing

Set objOlAccount = Nothing

Set objNameSpace = Nothing

Set objOutlook = Nothing

End Sub
 
Last edited by a moderator:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Try this out. I have set the reference to use Outlook. I have tried and tested and it works in my testing area.

VBA Code:
Sub sendmail()
'Set references to Outlook
Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem
Set olApp = CreateObject("Outlook.Application")
Set olMail = olApp.CreateItem(olMailItem)
Dim inP As String
'Use input box to select the cell
inP = InputBox("Which cell do you want to use", "Test")
Range(inP).Select
'Generate Email
With olMail
    .To = "Test@gmail.com"
    .CC = "Someone@gmail.com"
    .Subject = "Test Email"
    .Body = "Urgent attention " & ActiveCell.Value & " " & ActiveCell.Offset(0, 1).Value & " " & ActiveCell.Offset(0, 2).Value
    .Display 'Use .Display or change to .Send once tested

End With
Set olApp = Nothing

End Sub
 
Upvote 0
Try this out. I have set the reference to use Outlook. I have tried and tested and it works in my testing area.

VBA Code:
Sub sendmail()
'Set references to Outlook
Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem
Set olApp = CreateObject("Outlook.Application")
Set olMail = olApp.CreateItem(olMailItem)
Dim inP As String
'Use input box to select the cell
inP = InputBox("Which cell do you want to use", "Test")
Range(inP).Select
'Generate Email
With olMail
    .To = "Test@gmail.com"
    .CC = "Someone@gmail.com"
    .Subject = "Test Email"
    .Body = "Urgent attention " & ActiveCell.Value & " " & ActiveCell.Offset(0, 1).Value & " " & ActiveCell.Offset(0, 2).Value
    .Display 'Use .Display or change to .Send once tested

End With
Set olApp = Nothing

End Sub
Thank you but its giving me an compile error message: user-defined type not defined
And it is pointing to olApp As Outlook.Application
 
Upvote 0
You have to set the reference to use Outlook. In VBA got o the Tools Menu and then References, search down the list for Microsoft.Outlook XX and select the tickbox then it will work.
 
Upvote 0
Thank you ! Is it possible not to write the cell i want to use but click on it ?!
 
Upvote 0
You will need to create a UserForm in the VBA screen and from the toolbox add in a control called RefEdit this will allow you to select a cell. You would have to assign the code to a command button on the form, name the control inP change part of the code from the InputBox to the RefEdit control.

That wasn't part of your original question though.

Line to change

VBA Code:
inP = InputBox("Which cell do you want to use", "Test")

to
VBA Code:
inP = inP.Text
 
Upvote 0
can you please clarify your reply ? I did not get what i should do. I prefer in the vba code to include application.inputbox in order to be able to select a cell directly. Sorry for not clarifying this in the beginning.
 
Upvote 0
Ref edit is a control you add to a userform in the VBA screen this will allow you to select cells in worksheet and then it can run the rest of the code. To see it in action watch something like this on YouTube.
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,917
Members
449,093
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top