- Local time
- Today, 12:07
- Joined
- Feb 28, 2001
- Messages
- 27,163
I'm trying to encrypt selected outbound E-mail, built by Access 2003 and sent through Outlook 2003.
The catch is, I can't quite get this to work. I've looked all over the net to get this far. Does anyone have insights that might help?
Here are the significant parts. I have removed some of the code that does not appear to be relevant, just some logging to an event table. An external routine builds the To, CC, Subject, and Mail Body as strings. They get passed in to my mail send routine correctly. The UID is a code number for the user internal ID, which is the PK of a lookup table of authorized users. It is used only in the logging routine that I don't show in the code. Formal parameter bEnc is TRUE if I need to encrypt the mail. If traps occur, I return a status code other than zero for the function value.
OK, here's my observation. It does not encrypt, but it DOES send.
In debugger, I single step the code to the point where I have set OLEnc and OLSgn to valid command bar buttons. The ugly little If-ladders are there so I can debug one step at a time.
In the locals window I can see that these buttons are enabled. Due to defaults set in my Outlook account, the code does not attempt to set the Digital Signature button because it is already set (state=msoButtonDown). But the Encryption button is NOT set by default in this case. So I check that I have instantiated the OLEnc button, which I have. Locals window shows me its content. I check that it is enabled. It is. I check that the button is currently up. It is. So I try to encrypt the message with that .Execute. It executes, no traps occur, ... but the resultant message is not encrypted. If I try to set the button by direct value assignment, I get an error trap because you can't do something like:
OLEnc.state = msoButtonDown
The Help says that intrinsic command bar buttons are read-only, though a user-added button could possibly be read/write. Well, these are intrinsic.
I've explored the possibility that I'm missing a library, but I have the MS Outlook 11.0 library referenced. The Encrypt button works manually if I am actually running Outlook, so I'm at a loss for what I've left out. Anyone have any ideas?
(In case anyone wants to try...) The constants you need are
Public Const DigitalSigCtrl = 719 'for outlook, the SIGN IT button
Public Const EncryptMsgCtrl = 718 'for outlook, the ENCRYPT IT button
The catch is, I can't quite get this to work. I've looked all over the net to get this far. Does anyone have insights that might help?
Here are the significant parts. I have removed some of the code that does not appear to be relevant, just some logging to an event table. An external routine builds the To, CC, Subject, and Mail Body as strings. They get passed in to my mail send routine correctly. The UID is a code number for the user internal ID, which is the PK of a lookup table of authorized users. It is used only in the logging routine that I don't show in the code. Formal parameter bEnc is TRUE if I need to encrypt the mail. If traps occur, I return a status code other than zero for the function value.
Code:
Public Function SendAMsg(UID As Long, OLSbj As String, OLTo As String, _
OLCC As String, OLBdy As String, bEnc As Boolean) As Long
Dim OLApp As Outlook.Application 'ACCESS construct to allow us to bit-twiddle Outlook
Dim OLMsg As Outlook.MailItem 'a mail message within Outlook
Dim OLNS As Outlook.NameSpace 'need this to get to the MAPI name space
Dim OLFld As Outlook.MAPIFolder 'need this for sending mail
Dim OLIns As Outlook.Inspector 'need this to set encryption
Dim OLBar As Office.CommandBar 'make this a command-bar object
Dim OLEnc As Office.CommandBarButton
Dim OLSgn As Office.CommandBarButton
...
Set OLApp = Nothing 'start with simplest assumption
On Error Resume Next 'block traps while we poke around
Set OLApp = GetObject(, "Outlook.Application") 'find existing instance of Outlook
On Error GoTo 0 'remove the trap block
...
OLNew = False 'assume we found one
If OLApp Is Nothing Then 'did we find an existing Outlook we could use?
OLNew = True 'no, have to create one, so ...
Set OLApp = CreateObject("Outlook.Application") 'create a private apps object
End If
...
Set OLNS = OLApp.GetNamespace("MAPI") 'get into the namespace
OLNS.Logon , , False, False 'we created or tested the app already
Set OLFld = OLNS.GetDefaultFolder(olFolderOutbox) 'find the outbox folder
Set OLMsg = OLFld.Items.Add(olMailItem) 'from which we will send a single mail message
...
OLMsg.Importance = olImportanceHigh 'start populating the mail message
OLMsg.To = OLTo 'fill in first set of recipients
OLMsg.cC = OLCC 'fill in second set of recipients
OLMsg.Subject = OLSbj 'fill in the message subject
OLMsg.Body = OLBdy 'fill in the message body
OLMsg.BodyFormat = olFormatPlain 'make it totally plaintext
' encrypt if requested to do so
If bEnc = True Then 'see if we even need to bother
Set OLIns = OLMsg.GetInspector 'get the inspector so we can open the toolbars
Set OLBar = OLIns.CommandBars("Standard") 'get to the standard toolbar
Set OLEnc = OLBar.FindControl(, EncryptMsgCtrl) 'which has ENCRYPT and SIGNDIGITAL
Set OLSgn = OLBar.FindControl(, DigitalSigCtrl)
If Not OLSgn Is Nothing Then 'if we found it, ...
If OLSgn.Enabled = True Then 'and if it is enabled, ...
If OLSgn.State = msoButtonUp Then 'and if it is not already set, ...
OLSgn.Execute
End If
End If
End If
If Not OLEnc Is Nothing Then
If OLEnc.Enabled = True Then
If OLEnc.State = msoButtonUp Then
OLEnc.Execute
End If
End If
End If
End If 'end - if we are going to encrypt the message
OLMsg.Send 'send it on its way
OK, here's my observation. It does not encrypt, but it DOES send.
In debugger, I single step the code to the point where I have set OLEnc and OLSgn to valid command bar buttons. The ugly little If-ladders are there so I can debug one step at a time.
In the locals window I can see that these buttons are enabled. Due to defaults set in my Outlook account, the code does not attempt to set the Digital Signature button because it is already set (state=msoButtonDown). But the Encryption button is NOT set by default in this case. So I check that I have instantiated the OLEnc button, which I have. Locals window shows me its content. I check that it is enabled. It is. I check that the button is currently up. It is. So I try to encrypt the message with that .Execute. It executes, no traps occur, ... but the resultant message is not encrypted. If I try to set the button by direct value assignment, I get an error trap because you can't do something like:
OLEnc.state = msoButtonDown
The Help says that intrinsic command bar buttons are read-only, though a user-added button could possibly be read/write. Well, these are intrinsic.
I've explored the possibility that I'm missing a library, but I have the MS Outlook 11.0 library referenced. The Encrypt button works manually if I am actually running Outlook, so I'm at a loss for what I've left out. Anyone have any ideas?
(In case anyone wants to try...) The constants you need are
Public Const DigitalSigCtrl = 719 'for outlook, the SIGN IT button
Public Const EncryptMsgCtrl = 718 'for outlook, the ENCRYPT IT button