Solved

Script that copies content of Email to a text file

Posted on 2006-06-12
18
422 Views
Last Modified: 2012-05-05
I need a script, that will be used with an existing rule, that copies the content of an email to a text file.
0
Comment
Question by:myfleetman
  • 10
  • 8
18 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 16887862
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!
0
 

Author Comment

by:myfleetman
ID: 16888046
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
0
 
LVL 76

Expert Comment

by:David Lee
ID: 16889801
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

0
 

Author Comment

by:myfleetman
ID: 16889846
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.

0
 

Author Comment

by:myfleetman
ID: 16889945
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?


0
 
LVL 76

Expert Comment

by:David Lee
ID: 16890787
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.
0
 

Author Comment

by:myfleetman
ID: 16893705
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.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 16898078
> 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
0
 

Author Comment

by:myfleetman
ID: 16898484
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).
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 76

Accepted Solution

by:
David Lee earned 250 total points
ID: 16900945
Redemption would definitely work here.  Something like this:

Sub SaveAsTextFile(Item As MailItem)
    Dim strPath As String, _
        redItem As Object
    strPath = Replace(Item.Subject, "\", "")
    strPath = Replace(strPath, ":", "")
    Set redItem = CreateObject("Redemption.SafeMailItem")
    Set redItem.Item = Item
    redItem.SaveAs "C:\SomeFolder\" & strPath & ".txt", olTXT
    Set redItem = Nothing
End Sub

> without the "Security warning" if I have a certificate
With an digital certifcate created and signed by a separate authority, yes, I think so.  But maybe I'm not remembering correctly.  I thought the self cert would work for pretty much everything except sending.  I know it doesn't work for that.  

> Do you think I created it wrong
No.  More likely that I'm not remembering correctly.

0
 

Author Comment

by:myfleetman
ID: 16901990
BlueDevilFan:

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

0
 
LVL 76

Expert Comment

by:David Lee
ID: 16903436
myfleetman,

Thanks.  Glad I could help out.
0
 

Author Comment

by:myfleetman
ID: 16904110
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?
0
 

Author Comment

by:myfleetman
ID: 16904712
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?
0
 
LVL 76

Expert Comment

by:David Lee
ID: 16908163
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.
0
 

Author Comment

by:myfleetman
ID: 16910940
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.
0
 

Author Comment

by:myfleetman
ID: 16911186
OK...Success!  I updated my version of Redemption and reran the process.  It saved the entire email.  All is well.

Thanks again BlueDevilFan.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 16915470
You're welcome.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Set OWA language and time zone in Exchange for individuals, all users or per database.
Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now