Results 1 to 8 of 8
  1. #1
    2 Star Lounger
    Join Date
    Oct 2001
    Location
    Not in KC anymore
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Rename Subject (2002)

    We receive several emails per day that need to have the subject changed from an auto-generated one to one based on content within the message.

    Is there a way to set up a rule or macro to do this for me?

    Here is the message text I have bolded the items that need to appear in the renamed subject:
    <hr>Event Title: EVENT TITLE
    Any Questions or Comments please Call: 1-888-555-1212

    Ticket Number: 12345

    Start Time: 07/13/2004 21:00 EDT
    Stop Time: 07/13/2004 23:00 EDT

    Location: ANYTOWN USA

    Impact: Impact Expected

    Maintenance Comments:
    Maintenance comments go here. <hr>

    Here is how I need the title to read:

    <hr>12345 07/13/2004 21:00 EDT ANYTOWN USA Maintenance comments go here.<hr>

    The message text is in the same order and spaced the same for every one.

    Thanks in advance.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Rename Subject (2002)

    How about a macro run by a rule? It's a complicated project because it requires parsing free text, but since you don't need to access any address fields or send a message, there's no security-related reason it can't be done. It's just hard work pulling out that information without error. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    Do you do VBA? If so, you can hook up your code following the method described in <post#=351149>post 351149</post#>. For more information about the interaction between rules and VBA (referred to misleadingly as script in the Rules Wizard), and the "pure VBA" alternatives, see <post#=293507>post 293507</post#>. If you don't write VBA, perhaps someone will volunteer to help.

  3. #3
    2 Star Lounger
    Join Date
    Oct 2001
    Location
    Not in KC anymore
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Rename Subject (2002)

    I have no idea what you just wrote. <img src=/S/confused3.gif border=0 alt=confused3 width=45 height=45>

    I'll check out the references you provided and see what I can figure out.

    Thanks for the help!

  4. #4
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Rename Subject (2002)

    As far as parsing text of message body to get subject line, that part is easy. You'll have to figure out how to define the rule - if these msg's have a standard subject line, or same "From" address, or whatever, that should not be hard. Sample code to change subject line:

    <code>Public Sub ParseMsgText()</code>
    <code></code>
    <code> Dim item As Object</code>
    <code> Dim fldr As MAPIFolder</code>
    <code> Dim strBody As String</code>
    <code> Dim strSubj As String</code>
    <code> Dim s() As String</code>
    <code> Dim n As Long</code>
    <code> </code>
    <code> Set fldr = ActiveExplorer.CurrentFolder</code>
    <code> </code>
    <code> For Each item In fldr.Items</code>
    <code> If item.Class = olMail Then</code>
    <code> ' Note comparison is case sensitive!</code>
    <code> If item.Subject = "Test Msg" Then</code>
    <code> strBody = item.Body & vbCrLf</code>
    <code> s = Split(strBody, vbCrLf, , vbBinaryCompare)</code>
    <code> </code>
    <code> ' Test only:</code>
    <code> For n = 0 To UBound(s) - 1</code>
    <code> Debug.Print n & " " & s(n)</code>
    <code> Next n</code>
    <code> </code>
    <code> ' Get new subject line:</code>
    <code> strSubj = Trim$(Mid$(s(3), InStr(1, s(3), ": ", 0) + 2)) & " " & _</code>
    <code> Trim$(Mid$(s(5), InStr(1, s(5), ": ", 0) + 2)) & " " & _</code>
    <code> Trim(Mid$(s(8), InStr(1, s(8), ": ", 0) + 2)) & " " & _</code>
    <code> Trim$(s(13))</code>
    <code> </code>
    <code> Debug.Print</code>
    <code> Debug.Print "Subj: " & strSubj</code>
    <code> item.Subject = strSubj</code>
    <code> item.Save</code>
    <code> End If</code>
    <code> End If</code>
    <code> Next item</code>
    <code> </code>
    <code> Set fldr = Nothing</code>
    <code> Set item = Nothing</code>
    <code></code>
    <code>End Sub</code>

    Note this assumes that the incoming msg's are formatted EXACTLY as in the example you posted. If not, code will probably need some modification. Sample output to Immediate window:

    0 Event Title: EVENT TITLE
    1 Any Questions or Comments please Call: 1-888-555-1212
    2
    3 Ticket Number: 12345
    4
    5 Start Time: 07/13/2004 21:00 EDT
    6 Stop Time: 07/13/2004 23:00 EDT
    7
    8 Location: ANYTOWN USA
    9
    10 Impact: Impact Expected
    11
    12 Maintenance Comments:
    13 Maintenance comments go here.

    Subj: 12345 07/13/2004 21:00 EDT ANYTOWN USA Maintenance comments go here.

    The Split function is used to populate an array with each line of msg body, using CR/LF as delimiter. Then simply parse each specified item in array & concatenate to get the new subject line. This worked OK in tests using Outlook 2000, as shown above. In actual use, recommend add error handling, etc. Debug.Print statements are for test purposes only, can be commented out or deleted.

    HTH

  5. #5
    2 Star Lounger
    Join Date
    Oct 2001
    Location
    Not in KC anymore
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Rename Subject (2002)

    I've been messing with the code a bit. I appreciate you doing the heavy lifting.

    The subject line is different each time, but only the first part is different. The subject line is like this:

    20f41:Scheduled Maintenance Message

    Is there a way to assign wild cards? Or if there is a way to apply this to all the messages in the current folder, that would work too.

    The sender is the same, but I don't see in the code where that is in your example.

    I'm fairly new and unfamiliar with this, and with the help of those who provide the help around here, I'm learning more. It is MUCH appreciated!

  6. #6
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Rename Subject (2002)

    If the subject line is always something like "20f41:Scheduled Maintenance Message" you could test for that using Like operator. Example:

    <code>Public Sub ChangeMsgSubjectAll()</code>
    <code> On Error GoTo Err_Handler</code>

    <code> Dim Item As Object</code>
    <code> Dim fldr As MAPIFolder</code>
    <code> Dim s() As String</code>
    <code> Dim n As Long</code>
    <code> Dim strMsg As String</code>
    <code> </code>
    <code> ' Test all items in current folder:</code>
    <code> Set fldr = ActiveExplorer.CurrentFolder</code>
    <code> </code>
    <code> For Each Item In fldr.Items</code>
    <code> ' If item.Class = olMail Then</code>
    <code> If TypeName(Item) = "MailItem" Then</code>
    <code> ' Note comparison is case sensitive by default!</code>
    <code> If Item.Subject Like "*" & "Scheduled Maintenance Message" Then</code>
    <code> ' And Item.SenderName Like "*" Then</code>
    <code> s = Split(Item.Body & vbCrLf, vbCrLf, , vbBinaryCompare)</code>
    <code> ' Generate new subject line:</code>
    <code> Item.Subject = Trim$(Mid$(s(3), InStr(1, s(3), ": ", 0) + 2)) & " " & _</code>
    <code> Trim$(Mid$(s(5), InStr(1, s(5), ": ", 0) + 2)) & " " & _</code>
    <code> Trim(Mid$(s(8), InStr(1, s(8), ": ", 0) + 2)) & " " & _</code>
    <code> Trim$(s(13))</code>
    <code> Item.Save</code>
    <code> End If</code>
    <code> End If</code>
    <code> Next Item</code>

    <code>Exit_Sub:</code>
    <code> Set fldr = Nothing</code>
    <code> Set Item = Nothing</code>
    <code> Exit Sub</code>
    <code>Err_Handler:</code>
    <code> strMsg = "Error No " & Err.Number & ": " & Err.Description</code>
    <code> MsgBox strMsg, vbExclamation, "CHANGE SUBJECT ERROR MESSAGE"</code>
    <code> Resume Exit_Sub</code>
    <code>End Sub</code>

    You could also test for SenderName property (commented out above) but I would avoid that if possible, because in Outlook 2000 (and probably in 2002) this will evoke a security warning dialog that you will have to respond to before code will run. I don't know if this is intended to run on your PC only, or for other users, or if you are using Exchange Server or not. In Outlook 2002 the Rules Wizard has a "Run A Script" option; I'm using 2000 so not familiar with this feature. In 2000 or 2002, you can use VBA to enable event-handlers that act as custom rules; see attached text file for examples. The code listed under "ThisOutlookSession" Class Module should be copied to the "ThisOutlookSession" module in VB Editor; code listed under Standard Code Module should be copied to a standard module. In example, Application Startup event initializes an event-handler for Inbox folder; whenever new item added to inbox, the ChangeMsgSubject sub will run if subject line matches criteria specified. Keep in mind, if you are on an Exchange server, this code will run only if Outlook is open & running on your pc (ie, it's client side only), as opposed to rules you create via Rules Wizard that are saved on server, such as "Out of Office" auto-replies, etc. Due to security issues, etc, where I work, having custom "scripts" run on server is totally out of the question. If the event-handler doesn't "capture" all incoming msg's, you can always copy the "Maintenance" msg's to a dedicated folder then run the ChangeMsgSubjectAll sub (recommend use dedicated folder for this, if you have a huge Inbox). There are numerous MSKB articles with more info & examples, for starters recommend see MSKB 324568:

    OL: Overview of Programming with Rules

    HTH

  7. #7
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Rename Subject (2002)

    Mark, I'm curious to know why you prefer

    If TypeName(Item) = "MailItem" Then ...

    to

    If Item.Class = olMail Then ...
    -John ... I float in liquid gardens
    UTC -7ąDS

  8. #8
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Rename Subject (2002)

    Actually, no preference in this case, I was just trying to illustrate two different methods available to determine what type of Outlook item is being processed by code. (Tested code with either option uncommented to make sure worked OK.) Though not applicable in this case, where using For... Next loop to step thru items in a collection, in some cases, if you try to get the Class property of an object variable that has not been set, Outlook will generate an error. Also not applicable here, but if dealing with some other types of items, such as Appointment or Task items, then the item's Class property may be preferred method, as, for example, the Class property can distinguish between an appointment (olAppointment) and a meeting request (olMeetingRequest), etc.; the TypeName function returns "AppointmentItem" for both. But there may be cases where you need to identify all appointment items, regardless of specific class, in which case the TypeName function would be preferred method. The same would apply to other types of items (such as Tasks) that have more than one Class defined. Hope this makes sense.

Posting Permissions

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