Link to home
Start Free TrialLog in
Avatar of myfleetman
myfleetmanFlag for United States of America

asked on

Script that copies content of Email to a text file

I need a script, that will be used with an existing rule, that copies the content of an email to a text file.
Avatar of David Lee
David Lee
Flag of United States of America image

Hi myfleetman,

This will save the entire message to disk in text format:

Sub SaveAsTextFile(Item As MailItem)
    Item.SaveAs "C:\SomeFolder\" & Item.Subject & ".txt", olTXT
End Sub

Cheers!
Avatar of myfleetman

ASKER

OK...I inserted the script and ran it...nothing happened.  Could I have a problem with Macros being disabled or something like that?   My security setting is at medium.   Below is the script I'm trying to run:

Apply to message
from "Smith, Bob"
  and with "FW: Test" in the subject
  and on this machine only
run "Project1.SaveAsTextFile"


If I remove the "Project1.SaveAsTextFile" script and add  "move it to the 'Test' folder", it runs OK:

Apply to message
from "Smith, Bob"
  and with "FW: Test" in the subject
  and on this machine only
move it to the "Test" folder
myfleetman,

I see two possible causes.  One, did you adjust the save to path to a folder on your computer?  Two, using the Subject for the file name won't work if the subject is going to contain invalid characters.  

Sub SaveAsTextFile(Item As MailItem)
    Dim strPath As String
    strPath = Replace(Item.Subject, "\", "")
    strPath = Replace(strPath, ":", "")
    Item.SaveAs "C:\SomeFolder\" & strPath & ".txt", olTXT
End Sub

BlueDevilFan:

Here's the script I'm using:

Sub SaveAsTextFile(Item As MailItem)
    MsgBox ("hello")
    Item.SaveAs "C:\Test\" & Item.Subject & ".txt", olTXT
End Sub


The MsgBox is not firing either when I run it.

I'll try your suggestion and see if it changes anything.

OK.. I made the changes you (BlueDevilFan) suggested and it still is not firing the script.  I rewrote the script to include moving the email to a different folder (see below)

Apply to message
from "Smith, Bob"
  and with "Test" in the subject
  and on this machine only
move it to the "Test" folder
  and run "Project1.SaveAsTextFile

The script is as follows:

Sub SaveAsTextFile(Item As MailItem)
    MsgBox ("hello")
    Dim strPath As String
    strPath = Replace(Item.Subject, "\", "")
    strPath = Replace(strPath, ":", "")
    Item.SaveAs "C:\Test\" & Item.Subject & ".txt", olTXT
    'Item.SaveAs "C:\Test\TestFile.txt", olTXT
End Sub

I even tried ding a direct file name without success.

I don't think the script is firing, but everything else seems to work becuse the email is being move to the "Test" folder.  That's why I put nt the MsgBox.  

How do I verify the script is actually running?


The MsgBox should fire regardless of whether the rest of the code works or not.  It must not be firing at all.  Let's try a test.  Add this code to what you already have.

    Sub TestSaveAsTextFile()
        SaveAsTextFile Application.ActiveExplorer.Selection(1)
    End Sub

Now, minimize the VB editor window and select a message in your inbox.  Run this second macro (Tools->Macro->Macros).  Let me know what happens.
I created the Macro and when I went to run it, Outlook displayed a dialog box asking me if I wanted to enable or disable Macros.  I chose to enable Macros and ran the new Macro.  I then ran my script and it worked.  How did the Macros get disabled?  How can I assure that they're enabled all the time?

Also, when I run the script Outlook displayed a message stating "A program is trying to access data from Outlook that may include address book information.  Do you want to allow this?".  Is there a way to prevent this message from displaying?  It kind of defeats the purpose of automating the process.
> How did the Macros get disabled?
They may never have been enabled.  Adjusting the macro security level the first time doesn't seem to do it.  From what I've seen I gather that you aren't offered the opportunity to enable them until you try and run one.  Once enabled they should remain enabled or prompt you the first time a macro is run each Outlook session.  Once you've said you want to allow macros during that session it shouldn't be necessary to do anything else until you have closed this session and open a new one.

> Is there a way to prevent this message from displaying?
Yes, there are a few ways around this.  Before describing those I should explain why you're getting this.  Microsoft added security to Outlook to combat the many worms and viruses written to use it as a transport mechanism.  This warning is triggered when you access certain fields, perform certain actions, or attempt to send anything from code.  It is designed to warn you that a program is doing something you may not want it to do with your email and give you the opportunity to decide if it should be allowed to continue.  Yes, it does defeat writing your own automation code.  There are four ways around this.

1.  Write the code as an add-in.  Add-ins have different security constraints from macros.  Unfortunately, they are also more difficult to write.

2.  Sign the code with a digital signature.  A digital certificate is preferable, but you can "self-sign" code.  Here's a link to Microsoft instructions on how to do this: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnoxpta/html/odc_dsvba.asp

3.  Use Click-Yes.  This is a free third-party utility that sits in your system tray and watches for the security warning dialog-box.  When one pops up it clicks the Yes button for you.  In my opinion it's dangerous though.  Here's a link to it: http://www.contextmagic.com/express-clickyes 

4.  Outlook Redemption.  This too is a third-party utility that allows you to circumvent Outlook security.  It isn't risk free, but I believe it's safer.  It's not free though and is meant more for developers than a casual user.  Here's a link to it: http://www.dimastr.com/redemption
BlueDevilFan,

Thanks for the assistance.  I created a SelfCert certificate, however, it doesn't appear to work with Outlook.  With the certificate you can bypass the "Enable Macro" process, but the Outlook security warning still pops up.  I've used redemption in a few Access apps but not in a macro like this.  I'm not sure how it would apply here.

Do you think I should be able to run the Macro without the "Security warning" if I have a certificate?  Do you think I created it wrong (i did it twice to be sure).
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
BlueDevilFan:

Thanks for the assistance.  Everything is working now!  You da man!

myfleetman,

Thanks.  Glad I could help out.
BlueDevilFan:

Although the process is working I wasn't paying close attention to the actual file being saved.  It turns out that the file is incomplete (not all there).  Any thoughts as to why?
BlueDevilFan:

Although the process is working I wasn't paying close attention to the actual file being saved.  It turns out that the file is incomplete (not all there).  

The file size, when saved manually, is 38KB (606 lines), but is 33KB(530 lines) when saved via the script.

Any thoughts as to why?
What happens if you take Redemption back out and let Outlook save it?  I'm wondering if this is a body size limitation of Redemption.
I switched back to the non-Redemption code (where I have to answer the security request) and it saves the entire email to the file.  

Why would Redemption set a limit on the file size?  That seems odd.  

The version of Redemption I'm using is a couple of years old, so do you think if I used the latest version that it might work differently in this case?  

I guess the only way to really know is to try it.
OK...Success!  I updated my version of Redemption and reran the process.  It saved the entire email.  All is well.

Thanks again BlueDevilFan.
You're welcome.