Dynamic Button for Email Macro

GarnesGambit

New Member
Joined
Feb 23, 2024
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have created a macro that generates emails with attachments. It works wonderfully attaching it to a button. BUT, I have about 300 rows in my original workbook, and initially I thought I would have to set 300 buttons per row and attach 300 different macros to them to activate.

What I'm wanting to do, is have one button, that dynamically selects the information for the cell that I am in. So if I click the button then click H6, it selects the info for my .to, .cc etc from row 6. Or backward clicking into the cell first then pressing the button perhaps?

So, my code is as below you'll see I have the email set to take the .to from A6, .cc from C6, part of the .subject from G6 and the .attachments name from H6. This would then change to A7, C7, G7, H7 etc etc. Each row is different depending on whether there is a 1 or blank in P:BL in my workbook.

What would I change in the below code to set this?

Thanks in advance!

My code

VBA Code:
Sub send_EMAIL()


Dim OutApp As Object
Dim Outmail As Object
Dim Strbody As String

Set OutApp = CreateObject("Outlook.Application")
Set Outmail = OutApp.CreateItem(0)

Strbody = "<BODY style = font-size:12pt; font-familt:Arial>" & _
"Hi all, <br><br> blah blah blah this is an example.<br><br>" & _
"also an example<br><br>" & _
"Still an example<br><br>" & _
"Thanks, <br> Joe Bloggs"


On Error Resume Next
With Outmail
.display
.to = Sheets("Sheet1").Range("A6").Value
.CC = Sheets("Sheet1").Range("c6").Value
.Bcc = ""
.Subject = "Planogram Update - " & Sheets("Sheet1").Range("g6").Value & " - " & Format(Date, "dd/mm/yy")
.HTMLBody = Strbody & .HTMLBody
.Attachments.Add "H:\Example\Example\Example\Example\" & Sheets("All Plans").Range("h6").Value & ".pdf"

End With
On Error GoTo 0

Set Outmail = Nothing

End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Apoligies I have tried to download xl2bb but it isn't working. Please find attached an image of my example workbook
 
Upvote 0

Attachments

  • Example of Workbook 1.jpg
    Example of Workbook 1.jpg
    173.1 KB · Views: 8
Upvote 0
Sorry, my last image wasn't completely clear
 

Attachments

  • Example of Workbook.jpg
    Example of Workbook.jpg
    181.6 KB · Views: 9
Upvote 0
I actually solved this myself haha...

Where the code has:
.to = Sheets("Sheet1").Range("A6").Value

replace this with
.to = Sheets("Sheet1").Cells(ActiveCell.Row, "A")
 
Upvote 0
Solution

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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