PDA

View Full Version : Problem reading Message Subject



ljefi
05-12-2015, 02:12 AM
Hi,

I have produced a macro attached to a button on the Outlook 2010 ribbon which adds "[OFFICIAL-SENSITIVE]" to the subject line when clicked by the users, allowing them to flag up sensitive emails.

My problem is that olMail.Subject doesn't seem able to read the value held in subject until the user has clicked out of the subject line box.

This causes two problems;



The user types their subject, then clicks the macro without moving the cursor from subject - their keyed in subject is overwritten with [OFFICIAL-SENSITIVE], using a message box proves that olMail.Subject is returning blank. If I type a subject, click out of the subject box and then click the macro the [OFFICIAL-SENSITIVE] label is appended to the subject as expected.
If the user accidentally removes/deletes the [OFFICIAL-SENSITIVE] label from their subject clicking the button again, the macro does not replace the label until they have clicked out of the subject box, this is because olMail.Subject is still returning the previous value which still includes [OFFICIAL-SENSITIVE]


I've tried using send keys to send a TAB character (and I assumed leave the subject box) but this made no difference.

Is there a way to explicitly give the message body the focus.

Any ideas?

Here's the code;


Dim strSubject As String
Dim strLabel As String


strSubject = ""
strLabel = "[OFFICIAL-SENSITIVE]"


Dim olMail As MailItem

Set olMail = ActiveInspector.CurrentItem

strSubject = olMail.Subject

'Check if the subject already includes the label
Contains = InStr(strSubject, strLabel)

'If not then add the label
If Contains = False Then
olMail.Subject = ""
olMail.Subject = strLabel & strSubject
End If



Any suggestions appreciated!

Regards
Lee

skatonni
05-12-2015, 12:46 PM
Try


olMail.Save


Option Explicit

Sub subj()

Dim strSubject As String
Dim strLabel As String
Dim contains As Boolean

strSubject = ""
strLabel = "[OFFICIAL-SENSITIVE]"

Dim olMail As MailItem

Set olMail = ActiveInspector.currentItem
Debug.Print "Subject: " & strSubject

olMail.Save

strSubject = olMail.Subject
Debug.Print "Subject: " & strSubject

'Check if the subject already includes the label
contains = InStr(strSubject, strLabel)

'If not then add the label
If contains = False Then
'olMail.Subject = ""
olMail.Subject = strLabel & strSubject
End If
End Sub

gmayor
05-12-2015, 10:32 PM
You can certainly give the focus to the message body (and edit the message body if you wish).
The following will add the label to the start of the subject if it is not already present and will put the cursor at the start of the message body.



Dim olMail As MailItem
Dim olInsp As Inspector
Dim wdDoc As Object
Dim oRng As Object
Dim strSubject As String
Const strLabel As String = "[OFFICIAL-SENSITIVE] "

Set olMail = ActiveInspector.CurrentItem
With olMail
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
Set oRng = wdDoc.Range(0, 0)
oRng.Select 'Put the cursor at the start of the message body
strSubject = .Subject

'Check if the subject already includes the label
If InStr(strSubject, strLabel) Then
.Subject = .Subject
Else
.Subject = strLabel & strSubject
End If
End With

ljefi
05-13-2015, 02:23 AM
Thanks both, I now have a working solution.

What I didn't consider is how I will deploy this to end users :crying:

It seems I could copy my VbaProject.OTM to the users "userprofile%\AppData\Roaming\Microsoft\Outlook" folder - I've exported the toolbar customisation to a file "Outlook Customizations (olkmailitem).exportedUI" but I'm not sure where I need to copy this to on the user's PC.

Any ideas?

I don't suppose there any third party tools which simplify this? I understand Visual Studio can be used but I don't have access to this.

Thanks
Lee

skatonni
05-13-2015, 05:31 PM
It seems I could copy my VbaProject.OTM to the users "userprofile%\AppData\Roaming\Microsoft\Outlook" folder

Those with an existing otm would not be happy about that.

May be getting out of date but see here for suggestions. http://www.outlookcode.com/article.aspx?id=28


I've exported the toolbar customisation to a file "Outlook Customizations (olkmailitem).exportedUI" but I'm not sure where I need to copy this to on the user's PC.

Export it to a place they can import from or see here for the file locations. http://www.msoutlook.info/question/482