PDA

View Full Version : VBA to add a bcc



warren66
10-02-2020, 12:48 PM
New to the forum; can anyone help me with the VBA code to add a specific bcc address to selected emails in the draft folder?

It needs to be run on just the selected drafts - not the entire folder or a global rule.

It's times like this I wish Outlook VBA had a macro recorder like its siblings Word and Excel!

Anyone?

Any help much appreciated.

gmayor
10-02-2020, 08:48 PM
Maybe something like


Sub AddBCC()
Dim olFolder As Outlook.Folder
Dim olItem As Outlook.MailItem
Dim strBCC As String
On Error Resume Next
strBCC = "someone@somewhere.com"
Set olFolder = Session.GetDefaultFolder(olFolderDrafts)
For Each olItem In ActiveExplorer.Selection
If olItem.Parent = olFolder And TypeName(olItem) = "MailItem" Then
olItem.Recipients.Add(strBCC).Type = 3
olItem.Save
End If
Next olItem
lbl_Exit:
Set olFolder = Nothing
Set olItem = Nothing
Exit Sub
End Sub

warren66
10-03-2020, 01:27 AM
Thanks for this, Graham. Running the code adds the name of the recipient, but to the "Send To" field.

What should the code be for adding it to the Bcc field instead?

Very grateful for your input.

Warren

gmayor
10-03-2020, 02:21 AM
The line
olItem.Recipients.Add(strBCC).Type = 3adds the name defined in strBCC as BCC. Type = 3 is BCC.
If there is no existing BCC, you could probably use
olItem.BCC = strBCCinstead of the above line.

warren66
10-04-2020, 03:29 AM
The line
olItem.Recipients.Add(strBCC).Type = 3adds the name defined in strBCC as BCC. Type = 3 is BCC.
If there is no existing BCC, you could probably use
olItem.BCC = strBCCinstead of the above line.

That works perfectly now thanks, Graham.

The macro runs from the Developer window, but when I assign it to a button on the QAT and click it, it does nothing. Same with other macros, but if I add buttons for the built-in commands, like say "Reply", they work.

Would you happen to know if there is a setting I need to make in Outlook to get macros to run from the QAT? I have no such problems running macros from Excel and Word QATs...

Thanks again

W

gmayor
10-04-2020, 06:29 AM
I can't think of one and it runs from the QAT here.
There can be a security issue with Outlook - see
https://www.gmayor.com/create_and_employ_a_digital_cert.htm (http://www.gmayor.com/create_and_employ_a_digital_cert.htm)