VbaProject: Changing email format

Status
Not open for further replies.

divan

Member
Outlook version
Email Account
POP3
Hi

I need a script that can do the following:

Original Email
Email Subject:

Movement Alarm 555
Email Body:
Unit: 0 Version:03.091
Continuous Movement
Back Office
Zone 3
2015/01/20 @ 07h15
___________________________________________
Altered email after script was processed:
Email Subject:

Movement Alarm 555
Unit: 0 Version:03.091
Continuous Movement
Back Office
Zone 3
2015/01/20 @ 07h15
Email Body:
“Phone Number”
___________________________________________
My Script does it half way, it lacks in the following areas:
1. When I add the item.body to item.subject it replaces the whole subject (I only want to add it)
2. When It whould replace the body with mu “Phone Number” it adds a lot of gibberish which I don’t want

Example of what my email outcome is:
Original Email
Email Subject:

Movement Alarm 555
Email Body:
Unit: 0 Version:03.091
Continuous Movement
Back Office
Zone 3
2015/01/20 @ 07h15
___________________________________________
Altered email after script was processed:
Email Subject:

Unit: 0 Version:03.091
Continuous Movement
Back Office
Zone 3
2015/01/20 @ 07h15
Email Body:
Date: Mon, 19 Jan 2015 08:42:28 +0200
Message-ID: <000001d033b3$1a2ec660$4e8c5320$@co.za>
MIME-Version: 1.0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit
X-Mailer: Microsoft Office Outlook 12.0
Thread-Index: AdAzsgvdJDRuShGcSiaP+wcwbJIN/QAAQvAw
Content-Language: en-za

“Phone Number”
___________________________________________

This is my script:
Sub ChangeSubjectForward(Item As Outlook.MailItem)
Item.Subject = Item.Body
Item.Save
Set myForward = Item.Forward
myForward.Recipients.Add "sms@fakeserver.com"
myForward.Subject = Item.Body
myForward.Body = "Phone Number"
myForward.Send

End Sub
___________________________________________
 
1. When I add the item.body to item.subject it replaces the whole subject (I only want to add it)

concatenate the fields - myForward.subject = myForward.subject & myForward.body

Because the subject doesn't handles lines breaks well, you should replace them with commas or another character (I'm assuming the body is 5 lines as you've posted here.)

strSubject = replace(item.body, vbcrlf, ",")
myForward.subject = myForward.subject & strSubject

2. When It whould replace the body with mu “Phone Number” it adds a lot of gibberish which I don’t want

Where is the phone number coming from? If it's plain text, it should work just fine with the following code.
myForward.Body = "Phone Number"

what do you get with this:
myForward.htmlBody = "Phone Number"

or try this:
myForward.BodyFormat = olFormatplaintext
myForward.Body = "Phone Number"
 
concatenate the fields - myForward.subject = myForward.subject & myForward.body

Because the subject doesn't handles lines breaks well, you should replace them with commas or another character (I'm assuming the body is 5 lines as you've posted here.)

strSubject = replace(item.body, vbcrlf, ",")
myForward.subject = myForward.subject & strSubject



Where is the phone number coming from? If it's plain text, it should work just fine with the following code.
myForward.Body = "Phone Number"

what do you get with this:
myForward.htmlBody = "Phone Number"

or try this:
myForward.BodyFormat = olFormatplaintext
myForward.Body = "Phone Number"


I have tried the code you have given , but can't get it to work.

Not really sure as to where I should put in the code:

Sub ChangeSubjectForward(Item As Outlook.MailItem)
Item.Subject = myForward.Subject & myForward.Body <---This line gives me an error
Item.Save

Set myForward = Item.Forward
myForward.Recipients.Add "fake.email@gmail.com"
strSubject = Replace(Item.Body, vbCrLf, ",")
myForward.Subject = myForward.Subject & strSubject
myForward.HTMLBody = "Phone number" <---This makes no difference
myForward.Send

End Sub
 
I have played around with the script and found that this works almost 100 except for the concatenate function in the subject

Sub ChangeSubjectForward(Item As Outlook.MailItem)
Item.Subject = Item.Body
Item.Save
Set myforward = Item.Forward
myforward.Recipients.Add "fake.email@server.com"
strSubject = Replace(Item.Body, vbCrLf, ", ") <-----This is so cool, thanks!
myforward.Subject = strSubject
myforward.Body = "Phone Number"
myforward.Send
End Sub

If I where to change this:
myforward.Subject = strSubject
to this:
myforward.Subject = Item.Body & strSubject

Then there is no change in the email and only gets forwarded as it arrived
 
I have tried the code you have given , but can't get it to work.

Not really sure as to where I should put in the code:

Sub ChangeSubjectForward(Item As Outlook.MailItem)
Item.Subject = myForward.Subject & myForward.Body <---This line gives me an error
Item.Save

Set myForward = Item.Forward

The line errors because you are referencing myForward before you told Outlook what myForward is.




myForward.HTMLBody = "Phone number" <---This makes no difference

This should add the words "phone number" to the body. Nothing more, nothing less.
 
strSubject = Replace(Item.Body, vbCrLf, ", ") <-----This is so cool, thanks!

<snip>

If I where to change this:
myforward.Subject = strSubject
to this:
myforward.Subject = Item.Body & strSubject

Then there is no change in the email and only gets forwarded as it arrived

The first line strips the line feeds from the body to make one long line. in your two versions of myforward.subject, the first one adds the body then adds it again, with the line feeds stripped.

if you want to combine the original subject and the body, you need to hook them together.
myforward.Subject = Item.Subject & "," & strSubject
 
Hi Diane.

Thanks for all your help so far!
I finally got the script to work 100%....now I need to duplicate the script to have other recipients.

Example of how this is working now:
Our office (Site-1) has a CCTV system that sends out a email to Outlook that then gets forwarded to Linux box that SMS it out.
Now I need to add the CCTV system from home (Site-2) to send email to Outlook and also the forward it to the same Linux box.

What I want to accomplish:
Site-1 and 2 each have their own email address. Outlook determines the source of email (By using outlook Rules) and then tell it to run a scrip which is my Project1 (VBAProject.OTM)

But now I have two scripts each the same except for :
Site 1 = myforward.Body = "Phone Number-1"
Site 2 = myforward.Body = "Phone Number-2"

My rules does not seem to work when I have duplicated the script.
When I select my script to run in the rule it does show both of Project1.ChangeSubjectForward
bud does not do anything doesn't matter which one I choose.

The scrip only works if there is only one copy and one rule attached to the one scrip.

Please help!

Thanks in advance

Divan

This is what I have in VBA:
_____________________________________________
Sub ChangeSubjectForward(Item As Outlook.MailItem)
strSubject = Replace(Item.Body, vbCrLf, ", ")
Item.Subject = Item.Subject & " " & strSubject
Item.Body = "Phone number-1"
Item.Save

Set myforward = Item.Forward
myforward.Recipients.Add "Linux_box@domain.com"
myforward.Subject = "Alarm Detection" & " " & Item.Subject
myforward.Body = "Phone Number-1"
myforward.Send

End Sub
_____________________________________________

Sub ChangeSubjectForward(Item As Outlook.MailItem)
strSubject = Replace(Item.Body, vbCrLf, ", ")
Item.Subject = Item.Subject & " " & strSubject
Item.Body = "Phone number-2"
Item.Save

Set myforward = Item.Forward
myforward.Recipients.Add "Linux_box@domain.com"
myforward.Subject = "Alarm Detection" & " " & Item.Subject
myforward.Body = "Phone Number-2"
myforward.Send

End Sub
_____________________________________________
 
There are two ways to do this - duplicate the script with a new name or use 3 scripts - one to set the phone in a global variable and a shared one to do the work. The rule would call the one that set the # and that script would call the shared script.

Because the macro is just a few lines, I'd probably use the full macro and change the name:
Sub Site1Forward(Item As Outlook.MailItem)
Sub Site2Forward(Item As Outlook.MailItem)

if you needed to change more things or it was a long macro, you'd do something like this - but since it's about the same amount of text either way, it doesn't really matter.

===========================
dim strPhone as string

Sub Site1Forward(Item As Outlook.MailItem)
strphone = "site1 phone"
ChangeSubjectForward
end sub

Sub Site2Forward(Item As Outlook.MailItem)
strphone = "site2 phone"
ChangeSubjectForward
end sub

Sub ChangeSubjectForward()
strSubject = Replace(Item.Body, vbCrLf, ", ")
Item.Subject = Item.Subject & " " & strSubject
Item.Body = strphone
Item.Save

Set myforward = Item.Forward
myforward.Recipients.Add "Linux_box@domain.com"
myforward.Subject = "Alarm Detection" & " " & Item.Subject
myforward.Body = strphone
myforward.Send

End Sub
=============================
 
It makes sense and is a very good idea to have a shared script.
Just tried it now...
I went into rules and created the following rules:
------------------------------
Apply this rules after message arrives
from site-1@domain.com
and on this machine only
run Project1.Site1Forward
------------------------------
------------------------------
Apply this rules after message arrives
from site-2@domain.com
and on this machine only
run Project1.Site2Forward
------------------------------


If I receive a mail from either site I get an Run-time error '424'
Object Required

When I select either rule and try and run them on folders where email it does nothing

Any Ideas?
 
There are two ways to do this - duplicate the script with a new name or use 3 scripts - one to set the phone in a global variable and a shared one to do the work. The rule would call the one that set the # and that script would call the shared script.

Because the macro is just a few lines, I'd probably use the full macro and change the name:
Sub Site1Forward(Item As Outlook.MailItem)
Sub Site2Forward(Item As Outlook.MailItem)

if you needed to change more things or it was a long macro, you'd do something like this - but since it's about the same amount of text either way, it doesn't really matter.

===========================
dim strPhone as string

Sub Site1Forward(Item As Outlook.MailItem)
strphone = "site1 phone"
ChangeSubjectForward
end sub

Sub Site2Forward(Item As Outlook.MailItem)
strphone = "site2 phone"
ChangeSubjectForward
end sub

Sub ChangeSubjectForward()
strSubject = Replace(Item.Body, vbCrLf, ", ")
Item.Subject = Item.Subject & " " & strSubject
Item.Body = strphone
Item.Save

Set myforward = Item.Forward
myforward.Recipients.Add "Linux_box@domain.com"
myforward.Subject = "Alarm Detection" & " " & Item.Subject
myforward.Body = strphone
myforward.Send

End Sub
=============================

The run-time error refers to
strSubject = Replace(Item.Body, vbCrLf, ", ")
 
Let me see what i forgot.... ah... to set the item.


Code:
Dim strPhone As String

Sub Site1Forward(Item As Outlook.MailItem)
strPhone = "site1 phone"
ChangeSubjectForward Item
End Sub

Sub Site2Forward(Item As Outlook.MailItem)
strPhone = "site2 phone"
ChangeSubjectForward Item
End Sub

Sub ChangeSubjectForward(Item As Outlook.MailItem)
strSubject = Replace(Item.Body, vbCrLf, ", ")
Item.Subject = Item.Subject & " " & strSubject
Item.Body = strPhone
Item.Save

Set myforward = Item.Forward
myforward.Recipients.Add "Linux_box@domain.com"
myforward.Subject = "Alarm Detection" & " " & Item.Subject
myforward.Body = strPhone
myforward.Send

End Sub
 
Thanks so much Diane!
Works like a charm!

What would the max sites be that I can add to the script?
 
AFAIK, the only limitation would be in the number of rules allowed in Outlook - with POP3, it's virtually unlimited, or as many as you can manage without going nuts. The bigger problem would be multiple messages coming in at the same time, trying to be processed by the same script. If that happens often, it might help to use individual scripts for each number, not the shared script, but its also possible that the rules will just not process the messages.
 
Thanks for all your help so far Diane
This would hopefully be my last question to you:
I altered the script to give each Site a username and password which must be unique. The way I assigned it was the same as for the phone number but I think I slipped up somewhere.
Somehow it does not enter the given strUser = "UsernameX Password " info in my script.
What am I missing?

This is my whole VBA Script:

Dim strPhone As String
________________________________________
Sub Site001Forward(Item As Outlook.MailItem)
strPhone = "Site001 Phone"
strUser = "Username1 Password "
ChangeSubjectForward Item
End Sub
________________________________________
Sub Site002Forward(Item As Outlook.MailItem)
strPhone = "Site002 Phone"
strUser = "Username2 Password "
ChangeSubjectForward Item
End Sub
________________________________________
Sub Site003Forward(Item As Outlook.MailItem)
strPhone = "Site003 Phone"
strUser = "Username3 Password "
ChangeSubjectForward Item
End Sub
________________________________________
Sub ChangeSubjectForward(Item As Outlook.MailItem)
strSubject = Replace(Item.Body, vbCrLf, ", ")
Item.Subject = Item.Subject & " " & strSubject
Item.Body = strPhone
Item.Save
Set myforward = Item.Forward
myforward.Recipients.Add "Linux_box@domain.com"
myforward.Subject = strUser & Item.Subject
myforward.Body = strPhone
myforward.Send
End Sub
 
Variables that are shared/reused need to be dimmed outside of the macros - like strPhone is. Add a line for strUser and it should work. :)
Dim strUser As String
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
Hornblower409 Automatically or Manually Backup Multiple Versions of VbaProject.OTM Outlook VBA and Custom Forms 1
G Updating VbaProject.OTM on several users Outlook VBA and Custom Forms 3
A VBAProject.OTM Not Found Outlook VBA and Custom Forms 2
N Open & Save VBAProject.Otm using VBA Code Outlook VBA and Custom Forms 1
Peter R Hawkes Can I sync VBAProject.otm? Outlook VBA and Custom Forms 1
R how do I eliminate allow access dialog box using VBAproject in Out Outlook VBA and Custom Forms 1
P userform in VBAProject.otm not working Outlook VBA and Custom Forms 1
J Calendar events created on iPhone automatically changing default alert from 'None' to 'Time of Event' Using Outlook.com accounts in Outlook 0
F Auto changing email subject line in bulk Using Outlook 2
K Changing the Deleted Items location in Outlook 2019 Using Outlook 2
MattC Changing the font of an email with VBA Outlook VBA and Custom Forms 1
V Outlook 2021 Can anyone explain why my Outlook views keep changing?! Using Outlook 2
wayneame Changing the Form Used by Existing Task Items in a Folder Outlook VBA and Custom Forms 4
S Changing Message Class Outlook VBA and Custom Forms 4
C Pop Server Changing Verizon/Aol to Yahoo Using Outlook 6
P Outlook tasks keeps changing (updating) dates that I type Using Outlook 2
e_a_g_l_e_p_i Changing where data .pst is saved to Using Outlook 3
P Changing the font that the task view shows Using Outlook 5
S Changing colors of today's appointments, but not recurring ones Using Outlook 33
T Changing Sent Items location in Outlook 2019 Using Outlook 0
E Outlook view grouping keeps changing Using Outlook 3
B BCC issues after changing root folder path for gmail Using Outlook 1
M Changing the preferred order for "Put this entry in" list for adding new contacts to the Address Book Using Outlook 1
J Outlook 2010 Changing events in Outlook calendar via opening file, importing CSV Using Outlook 0
A .restrict results changing after moving to Exchange online Outlook VBA and Custom Forms 0
T Outlook Contacts ... Changing Font Size, Style, Bold, etc. Using Outlook 2
N Rule for "on behalf of" - with changing names Using Outlook 2
O Save attachments using hotkey without changing attributes Outlook VBA and Custom Forms 1
M Outlook 2016: Changing default font for Notes and Reading Pane Using Outlook 4
V Changing default date for task follow-up buttons Using Outlook 2
Gary Hile Outlook 2016 changing editor options Using Outlook 6
J Outlook Rules - Changing auto-submit address in multiple rules, according to rule name Outlook VBA and Custom Forms 0
S Problems syncing emails with webmail after changing to Outlook 2016 Using Outlook 1
T Changing default Mail Account in Outlook 2016 - POP3 Using Outlook 1
S Changing notification sound for new incoming messages in Outlook 365/2016 Using Outlook 1
Stephen Weinberg Changing the mailing address checkbox Using Outlook 0
D Outlook 2013 changing iCloud reminder time? Using Outlook 0
C Changing the name of Outlook Messages saved to a folder Using Outlook 1
A Outlook.com changing appointments Using Outlook 8
B Changing CC list to .add Outlook VBA and Custom Forms 2
Diane Poremsky Changing the Message Size in Exchange Server Using Outlook 0
R changing FW: on forward Outlook VBA and Custom Forms 3
B changing Win7 default backup schedule for Previous Versions Using Outlook 0
Diane Poremsky Changing the default *.pst and *.ost sizes Using Outlook 0
P Message Class keeps changing back to IPM.Contact Outlook VBA and Custom Forms 2
C Macro to send email after changing from address and adding signature Outlook VBA and Custom Forms 1
Diane Poremsky Changing Outlook.com color schemes Using Outlook 0
R Outlook calendar appointments Free/Busy time is changing from "Busy" to "Free" Using Outlook 2
W Changing looks of emails in Outlook 2003 Using Outlook 0
L Office 365 Outlook changing default contact folder Using Outlook 0

Similar threads

Back
Top