PDA

View Full Version : Having trouble renaming subjects



SpriteMonkey
12-08-2015, 09:11 PM
Having a problem with VBA scripting in Outlook. I want to set up a rule that takes all messages starting with XXXXXX and runs a script. That script will then check to see if the subject contains the string XXXXXX001 or XXXXXX002 etc and if it does, change the subject to YYYYYY001 or YYYYYY002 etc. I set up this code, which for my limited knowledge should work, but when I ran it, it instead blanked out all the subjects. So I have broken code, and I need a way to fix all the messages that have blanked out subjects which all need to be renamed YYYYYY001 (I only ran the script on a small batch of 50 XXXXXX001 emails to test). Can anyone point me in the right direction?



Sub EditSubject(Item As Outlook.MailItem)
If InStr(Item.Subject, "XXXXXX001") Then
Item.Subject = "YYYYYY001"
End If
If InStr(Item.Subject, "XXXXXX002") Then
Item.Subject = "YYYYYY002"
End If
Item.Save
End Sub

gmayor
12-08-2015, 11:31 PM
Sub EditSubject(Item As Outlook.MailItem)
If Item.Subject = "" Then Item.Subject = "YYYYYY001"
Item.Subject = Replace(Item.Subject, "XXXXXX", "YYYYYY")
Item.Save
End Sub

SpriteMonkey
12-14-2015, 10:21 AM
Thanks, gmayor! I finally had office time today to implement your script, and it solved the first part of my problem perfectly!!

The second part worked exactly the way you wrote it, but it wasn't the result I was looking for. That was an error on my part as I didn't do a good enough job explaining the situation well enough. If I re-explain it, do you think you could modify the script? I provided more detail below. Thank you so, so much for fixing the issue I created with my bad code, and thank you in advance for assistance with an update.

And an open ended question to anyone, what's a good resource to start learning more about the commands and syntax that are available for vba scripting? Thank you in advance!


So the emails that come in all have a generic subject header followed by a specific external part number. I want to run the script so it strips out all the junk and replaces the entire subject with just our internal part number. That was my original intent trying to use InStr to check if the string contains a specific value, the external part number, then replacing the whole subject with our internal part number. This script should examine the email subject, and if the subject contains XXXXXX001, then replace the entire subject with YYYYYY001. Here are some examples of the input and the result:

Incoming Subject: "Automated XYZ Company Order: Part Description (02246744)"
Resulting Subject: "YYYYYY001"
Incoming Subject: "Automated ABC Plant Order: Part Description (02293577)"
Resulting Subject: "YYYYYY002"
Incoming Subject: "New 123 B2B Handling Order: Part Description (02267241)"
Resulting Subject: "YYYYYY003"

SpriteMonkey
12-18-2015, 01:15 PM
Anyone else able to give me a hand? I've been searching myself but unable to find the function I'm looking for. :dunno