473,382 Members | 1,646 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How do I Set or Clear the Visibility of the BCC Field in a MailItem using Outlook VBA

NeoPa
32,556 Expert Mod 16PB
I have developed some code to add an address to the BCC field in an Outlook MailItem :

Expand|Select|Wrap|Line Numbers
  1. Private Sub VBAInspectors_NewInspector(ByVal Inspector As Inspector)
  2.     With Inspector.CurrentItem
  3.         If .Class = olMail Then
  4.             ' Note the current visibility of the BCC field
  5.             With .Recipients.Add(conBCC)
  6.                 .Type = olBCC
  7.                 Call .Resolve
  8.             End With
  9.             'Reset visibility of BCC Field to original setting
  10.         End If
  11.     End With
  12. End Sub
I would like to replace the comments in lines #4 and #9 with code to note, and then reset, the visibility of the BCC field within the MailItem.

conBCC is a string constant that contains a valid address string with an @ within it.

In the window of the MailItem it is easy to see and set this setting using the View menu (in 2003 and other approaches to do a similar job in later versions). I'm interested in finding how to see and set this in VBA specifically.
Sep 5 '11 #1

✓ answered by NeoPa

I'm very happy as of right now. It's barely minutes before I need to leave for my appointment, but I've just got time to post the solution I managed to come up with (with no little input from you guys - as well as some from guys on LinkedIn). The solution I found, that I'm confident now does the trick, is :

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Private Const conBCC As String = "ValidEmailAddress"
  4.  
  5. Private WithEvents VBAInspectors As Inspectors
  6. Private WithEvents VBAInspector As Inspector
  7.  
  8. Private blnValidAddr As Boolean
  9.  
  10. Private Sub Application_Startup()
  11.     Set VBAInspectors = Application.Inspectors
  12.  
  13.     'Determine if conBCC is valid for use later on
  14.     If conBCC = "" Then Exit Sub
  15.     With Application.CreateItem(olMailItem)
  16.         With .Recipients.Add(conBCC)
  17.             blnValidAddr = .Resolve
  18.         End With
  19.         Call .Close(olDiscard)
  20.     End With
  21.     If Not blnValidAddr Then _
  22.         Call MsgBox(Prompt:="Unable to resolve '" & conBCC & "'.", _
  23.                     Buttons:=vbOKOnly, _
  24.                     Title:="Outlook Startup")
  25. End Sub
  26.  
  27. Private Sub VBAInspectors_NewInspector(ByVal Inspector As Inspector)
  28.     With Inspector.CurrentItem
  29.         If blnValidAddr _
  30.         And .Class = olMail Then _
  31.             Set VBAInspector = Inspector
  32.     End With
  33. End Sub
  34.  
  35. Private Sub VBAInspector_Activate()
  36.     Dim blnBCCVis As Boolean
  37.     Dim cbcBCC As CommandBarControl
  38.  
  39.     If VBAInspector Is Nothing Then Exit Sub
  40.     Set cbcBCC = VBAInspector.CommandBars("View").Controls("BCC Field")
  41.     blnBCCVis = cbcBCC.State
  42.     With VBAInspector.CurrentItem.Recipients.Add(conBCC)
  43.         .Type = olBCC
  44.         Call .Resolve
  45.     End With
  46.     If Not blnBCCVis Then Call cbcBCC.Execute
  47.     Set VBAInspector = Nothing
  48. End Sub
The Activate event of the Inspector occurs on various occasions, but that includes the one immediately after the Inspector has been created. The main benefit being that the item is displayed already by the time the Activate event is triggered. I ensure it is only run on this occasion by tying it to the valid content of the VBAInspector object.

I'm going to go ahead and set this as Best Answer. Not because I deserve the lion's share of the credit in this case, but simply because anyone searching for a similar answer should be able to find it by coming straight to this post.

Thanks again to all for your help and your 'Stick with me' attitude.

19 4787
Rabbit
12,516 Expert Mod 8TB
I don't think the BCC field has to visible to add a recipient to it.

So the only case you need to worry about is when they don't have it visible and you want to set it back to invisible after the e-mail is sent. In that particular situation, after they send the e-mail, I don't believe that setting persists.

However, if the goal is to add a BCC and then hide it from user so they don't know a BCC was there in the first place, I don't know of a way to do that.

I know there's a registry setting that controls the default of whether or not to show the BCC field, but I don't know of a method within Outlook that will do so dynamically.
Sep 6 '11 #2
NeoPa
32,556 Expert Mod 16PB
Setting the BCC value works fine. That is not at issue. Unfortunately, setting the value has an unfortunate side-effect of setting the visibility too.

My motive for resetting the visibility is not to hide anything from the user at all, but rather to avoid making changes to the user's settings that they have not chosen themselves. I have a fundamental belief in the naffness of software that changes settings on your behalf without your permission and simply don't want my code to be that naff. No more than I would ever consider entering someone's house as a guest and moving their furniture arround to suit my own personal tastes. It's just a wrong thing to do IMHO.

PS. The BCC visibility setting does persist for other emails once set this way, although you won't see it for sent emails (under any circumstances).
Sep 6 '11 #3
Rabbit
12,516 Expert Mod 8TB
I tried it in outlook 2007 and adding a recipient to the BCC field doesn't persist the setting for me.

Perhaps it does so for 2003? I am unable to test that as I don't have access to Office 2003 anymore.

You could set the registry value. But that doesn't change it for the currently loaded Outlook processes until it is restarted.
Sep 7 '11 #4
NeoPa
32,556 Expert Mod 16PB
Thanks Rabbit.

It certainly does so for 2003. I'll test on 2007 later to ensure we're talking about the same thing.

I found the registry setting you referred to. It's at HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\O utlook\Preferences\ShowBcc. I'm going to look into this as it could well prove a workable solution (The Office version Number in that is pretty easy to get from the interface I expect).

I still prefer the idea of using the Outlook interface if at all possible, but I can at least provide a working version that doesn't have naff side-effects in the mean time, so that's a big step forward.
Sep 7 '11 #5
NeoPa
32,556 Expert Mod 16PB
Some info on the Office Version number for Outlook :

Although Access provides the number in X.Y format (as required here), Outlook provides it as A.B.C.D, so the following (or equivalent) is required :

Expand|Select|Wrap|Line Numbers
  1. strVersion = Left(Application.Version, InStr(4, Application.Version, ".") - 1)
Sep 7 '11 #6
Rabbit
12,516 Expert Mod 8TB
I've yet to find anything in VBA that will allow me to set user preferences. Barring any such functionality, the only option is to use that registry key.

The only drawback is it won't reload that preference until they restart outlook.
Sep 7 '11 #7
NeoPa
32,556 Expert Mod 16PB
Rabbit:
The only drawback is it won't reload that preference until they restart outlook.
That's a shame, because it's a complete show-stopper ;-(

I'll do some testing now I have the Registry Read/Write code working (I thought I had that already but found all I had previously developed was some code to read values if they were REG_SZ. Pretty limited really. A bit of a shock as I remembered it as something that enabled reading and writing of the Registry generally.

Never mind. I spent a good number of hours today developing it to handle reading and writing of REG_DWORD, REG_SZ, REG_EXPAND_SZ, REG_MULTI_SZ & REG_BINARY, so I'm happy with that now (I struggled with converting negative numbers into a string and then with write permissions which confused me royally for ages until I realised it wasn't my user permissions - I'm a local Admin of course - that were the issue but the parameters I'd used to open the Registry Key). If the changing of the Registry value doesn't have any effect until after Outlook is restarted, it will not be anything I can use here though unfortunately (as it would never get a chance to have any effect and wouldn't stop the operator irritation that I'm working to avoid). I'll post back when I've confirmed whether or not it can work (but that may be delayed as I'm all thunk out - I've been working solidly at this for a long while and need a break).
Sep 7 '11 #8
NeoPa
32,556 Expert Mod 16PB
It seems the Registry setting isn't even changed by Outlook until it closes either, which makes the whole approach unworkable. What a shame.

Don't think it wasn't a good idea to bring up though. It was the best I've had (received) yet. It was just very unfortunate that it led down a blind alley in the end.
Sep 7 '11 #9
NeoPa
32,556 Expert Mod 16PB
Jimmy Peña from LinkedIn has offered a suggestion of :
Expand|Select|Wrap|Line Numbers
  1. Inspector.CommandBars.FindControl(, 1860).Execute
It's probably the closest yet to what's required, but although it toggles the visibility, it doesn't set it related to what the user has chosen. Indeed there is no way I know of yet even to determine what the user has chosen for this.
Sep 12 '11 #10
Rabbit
12,516 Expert Mod 8TB
The registry value should be the setting the user has it at shouldn't it? Assuming you read that registry value before setting anything in code.
Sep 12 '11 #11
NeoPa
32,556 Expert Mod 16PB
The Registry setting reflects the value it was at when Outlook was started, but if I use that and the user decides to change their choice for that setting then their choice would be lost if they opened any new emails subsequent to that (because, as mentioned earlier, that change of setting is only saved away to the Registry when the Outlook application terminates).

Having said that, it's something to consider. It can at least approximate to what's required.

Having considered that some more, I believe the confusion that might ensue after a user tried to change the setting but their choice was lost, may well be more of a negative mark against the software even than not being able to make the choice at all. If I'm honest though, I'm still a little torn on this one.
Sep 12 '11 #12
NeoPa
32,556 Expert Mod 16PB
I have managed to get closer with the help of Malcolm Dixon from LinkedIn. It's particularly interesting as it provides insight into how to interface with the menu system within Outlook (and probably by extension into other Office apps too).

Unfortunately, I think I must admit defeat here :-( It seems that Microsoft have implemented an interface where doing this task, at the point where it needs to be done, is not possible (I'm always happy to be proven wrong of course).

The .State part of the code seems to be working fine. Where the problem lies is with switching the setting back once it's been set by the fact of setting a value in the BCC field (It doesn't like that until the MailItem is visible and it can't do that until after the event procedure has completed). I tried setting it by creating a new MailItem on the side just to change the setting with (As I could see that it had worked in Malcolm's code that way), but not only had the newly created item not registered that the State had changed in the first place, but changing it in the new item had no effect on the item that had just been created (The one that triggered the event) either.

The latest code I had is shown below. It's the closest I could get to a working version, but line #36 simply has no effect (It runs ok, but changes nothing).

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Private Const conBCC As String = "ValidMailAddress"
  4.  
  5. Private WithEvents VBAInspectors As Inspectors
  6.  
  7. Private blnBCCOK As Boolean
  8.  
  9. Private Sub Application_Startup()
  10.     Set VBAInspectors = Application.Inspectors
  11.  
  12.     'Determine if conBCC is usable for use later on
  13.     If conBCC = "" Then Exit Sub
  14.     With Application.CreateItem(olMailItem).Recipients.Add(conBCC)
  15.         blnBCCOK = .Resolve
  16.     End With
  17.     If Not blnBCCOK Then _
  18.         Call MsgBox(Prompt:="Unable to resolve '" & conBCC & "'.", _
  19.                     Buttons:=vbOKOnly, _
  20.                     Title:="Outlook Startup")
  21. End Sub
  22.  
  23. Private Sub VBAInspectors_NewInspector(ByVal Inspector As Inspector)
  24.     Dim blnBCCVis As Boolean
  25.     Dim cbcBCC As CommandBarControl
  26.  
  27.     With Inspector.CurrentItem
  28.         If .Class = olMail _
  29.         And blnBCCOK Then
  30.             Set cbcBCC = Inspector.CommandBars("View").Controls("BCC Field")
  31.             blnBCCVis = cbcBCC.State
  32.             With .Recipients.Add(conBCC)
  33.                 .Type = olBCC
  34.                 Call .Resolve
  35.             End With
  36.             If Not blnBCCVis Then Call cbcBCC.Execute
  37.         End If
  38.     End With
  39. End Sub
Oct 1 '11 #13
Rabbit
12,516 Expert Mod 8TB
Does it work if you make the mail item visible for the duration of the code?
Oct 1 '11 #14
NeoPa
32,556 Expert Mod 16PB
NeoPa:
(It doesn't like that until the MailItem is visible and it can't do that until after the event procedure has completed)
I may not have expressed that well in my previous post (#13) but it won't allow me to make the item visible (using Call .Display BTW) while the event is still active. There is also no timer event I can find to enable code to be run afterwards :-(
Oct 2 '11 #15
Rabbit
12,516 Expert Mod 8TB
Would it be possible to store the inspector for later use?
Oct 2 '11 #16
NeoPa
32,556 Expert Mod 16PB
I'm pretty sure I could manage that (in a global array of such items I expect), but I'm not aware of any way I could trigger processing it later. There appears to be no way of regaining control at an appropriate time. I'm guessing you have some thoughts on that though, perhaps?
Oct 2 '11 #17
Rabbit
12,516 Expert Mod 8TB
I think there's an on send event for e-mail?
Oct 3 '11 #18
NeoPa
32,556 Expert Mod 16PB
There are various events, which I will look into more deeply. Your post triggered me to find how I could look into events for these other objects (I'm still very green on the Outlook side. The code is structured so differently it seems to me.) and that is my next task. I should say, at this point, that I don't feel the Send event would be particularly helpful as running the code at that time would be more confusing to the operator, and it's the avoidance of such confusion and inconvenience that I'm trying to effect with this code.

Thanks for the trigger for looking deeper though. I feel I should have seen that myself, but somehow avoided realising all these events were available to me.
Oct 3 '11 #19
NeoPa
32,556 Expert Mod 16PB
I'm very happy as of right now. It's barely minutes before I need to leave for my appointment, but I've just got time to post the solution I managed to come up with (with no little input from you guys - as well as some from guys on LinkedIn). The solution I found, that I'm confident now does the trick, is :

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Private Const conBCC As String = "ValidEmailAddress"
  4.  
  5. Private WithEvents VBAInspectors As Inspectors
  6. Private WithEvents VBAInspector As Inspector
  7.  
  8. Private blnValidAddr As Boolean
  9.  
  10. Private Sub Application_Startup()
  11.     Set VBAInspectors = Application.Inspectors
  12.  
  13.     'Determine if conBCC is valid for use later on
  14.     If conBCC = "" Then Exit Sub
  15.     With Application.CreateItem(olMailItem)
  16.         With .Recipients.Add(conBCC)
  17.             blnValidAddr = .Resolve
  18.         End With
  19.         Call .Close(olDiscard)
  20.     End With
  21.     If Not blnValidAddr Then _
  22.         Call MsgBox(Prompt:="Unable to resolve '" & conBCC & "'.", _
  23.                     Buttons:=vbOKOnly, _
  24.                     Title:="Outlook Startup")
  25. End Sub
  26.  
  27. Private Sub VBAInspectors_NewInspector(ByVal Inspector As Inspector)
  28.     With Inspector.CurrentItem
  29.         If blnValidAddr _
  30.         And .Class = olMail Then _
  31.             Set VBAInspector = Inspector
  32.     End With
  33. End Sub
  34.  
  35. Private Sub VBAInspector_Activate()
  36.     Dim blnBCCVis As Boolean
  37.     Dim cbcBCC As CommandBarControl
  38.  
  39.     If VBAInspector Is Nothing Then Exit Sub
  40.     Set cbcBCC = VBAInspector.CommandBars("View").Controls("BCC Field")
  41.     blnBCCVis = cbcBCC.State
  42.     With VBAInspector.CurrentItem.Recipients.Add(conBCC)
  43.         .Type = olBCC
  44.         Call .Resolve
  45.     End With
  46.     If Not blnBCCVis Then Call cbcBCC.Execute
  47.     Set VBAInspector = Nothing
  48. End Sub
The Activate event of the Inspector occurs on various occasions, but that includes the one immediately after the Inspector has been created. The main benefit being that the item is displayed already by the time the Activate event is triggered. I ensure it is only run on this occasion by tying it to the valid content of the VBAInspector object.

I'm going to go ahead and set this as Best Answer. Not because I deserve the lion's share of the credit in this case, but simply because anyone searching for a similar answer should be able to find it by coming straight to this post.

Thanks again to all for your help and your 'Stick with me' attitude.
Oct 3 '11 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Hari | last post by:
Hi all! I am trying to send mail using outlook in my web application. i am using Outlook Express. Actually i am saving it in DRAFTS folder instead of sending it. My system are in network. I hard...
2
by: Say | last post by:
Hello all, Can this newsgrop (comp.lang.javascript) be accessed using outlook? thanks SaY
3
by: Prakash Wadhwani | last post by:
Hi !! I have been browsing around but have not been able to find a simple, lucid solution ... or maybe I'm just too confused. I know this has been asked before by many ... but pls bear with me. ...
3
by: Ricardo Corsi P. Cesar | last post by:
I have many textbox in my webform, but i want clear all of them with on single step. Someone have the code ? Thks
9
by: George McCullen | last post by:
I have an Outlook 2003 using Exchange Server 2003 Public Contacts Folder containing 20,000 Contacts. I am writing a VB .Net 2003 program that loops through all the contacts in a "for each oCt in...
2
by: Senthilkumar | last post by:
Hi, I would like to send mail using Outlook or Outlook Express my .net program which is written using vb.net . When ever a new record of particular type is added, i would like to send an...
5
by: handokowidjaja | last post by:
Hi All, I'm trying to automate sending an email with an attachment in our environment (access 97) using Outlook Express ( we dont have MS outlook or other fancy stuff). Does anybody knows how to...
1
by: crystalgal | last post by:
I have a form called CSR The data in the form is from a query. Within the form I have the following fields: ProjectName, Job Status, and CSR_Deliver which is a drop down list of names. I want to...
5
by: Bob Sanderson | last post by:
I need a JavaScript that will allow me to clear a field in a form when I click an image next to it. Any help will be greatly appreciated.
2
by: achinm | last post by:
Hi, I need to send a mail using Outlook with an attachment, using DOS / Command Line. The line of code i am using is \outlook.exe /c ipm.note /m enmail@sender.com /a C:\attachedFile.txt ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.