Using Option Button to select Recipient in VBA in Word

mchapa89

New Member
Joined
Dec 12, 2016
Messages
5
I have a word document that, upon clicking a button, attaches itself to an email. I have 10 option buttons that I would like the user to choose from, to designate who should receive the email address. How can I change my code to allow the VBA to select the corresponding email address for that option button and send it only to them?

For example, let's say one of the option buttons said
-Sally
-Mark

If my user selected Sally, my code would say
"If opt_Sally, then shenderson@fakecompany.com; elseif opt_Mark, then mrichardson@fakecompany.com"
-------------------------------------------


Private Sub CommandButton1_Click()

Dim OL As Object
Dim EmailItem As Object
Dim Doc As Document


Application.ScreenUpdating = False
Set OL = CreateObject("Outlook.Application")
Set EmailItem = OL.CreateItem(olMailItem)
Set Doc = ActiveDocument
Doc.Save

With EmailItem
.Subject = "New Vehicle Equipment Trouble Report"
.To =

.Attachments.Add Doc.FullName
.Display
'.Send

End With
Application.ScreenUpdating = True
Set Doc = Nothing
Set OL = Nothing
Set EmailItem = Nothing
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
this is the downside of option buttons, you must reprogram to add/remove buttons/people.

Instead , use a data list. Then the vb would scan everything in the list and only send those with say an X in the Send column.
To edit, just add a new name to the list. The vb code does not change.
 
Upvote 0
this is the downside of option buttons, you must reprogram to add/remove buttons/people.

Instead , use a data list. Then the vb would scan everything in the list and only send those with say an X in the Send column.
To edit, just add a new name to the list. The vb code does not change.
Thanks Ranman, I am open to switching from option buttons to listbox or combobox. The set 10 recipients wouldn't change, so I'm not too worried about maintenance on the option buttons. However, in the code, I'm unsure how to call reference to whatever is selected in the listbox.

With EmailItem
.Subject = "New Vehicle Equipment Trouble Report"
.To = ???
 
Upvote 0
for single select list:
.to = lstBox

if you are selecting multiple items, then cycle thru the listbox getting all, then post to the .TO
Code:
For i = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(i) = True Then sList2Send =  ListBox1.List(i) & ";"
Next i
.TO = sList2Send
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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