Consulting

Results 1 to 5 of 5

Thread: Problem reading Message Subject

  1. #1
    VBAX Newbie
    Joined
    May 2015
    Posts
    2
    Location

    Problem reading Message Subject

    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;


    1. 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.
    2. 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

  2. #2
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    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
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

  3. #3
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  4. #4
    VBAX Newbie
    Joined
    May 2015
    Posts
    2
    Location
    Thanks both, I now have a working solution.

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

    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

  5. #5
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    Quote Originally Posted by ljefi View Post
    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

    Quote Originally Posted by ljefi View Post
    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
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •