问题
I am working on a vba macro to extract the text contained in an email that is sent to me every day at a specified email address I want to generate a vba macro that triggers when this arrives. The text needs to be saved to a text file "c:\temp\email.txt"
I have collated various bits of code, but nothing really works.
Can anyone advise a simple vba method to do this?
回答1:
You may find my answer to How to copy Outlook mail message into excel using VBA or Macros gives you a start.
That answer shows how to run down the Inbox extracting selected information from every email including sender, subject and text body. With this as a start you could write a macro that searches the Inbox for the email of interest and exports it to a text file.
What you really want is an event routine that is triggered when an item is added to the Inbox but that is more advanced so get a macro that searches Inbox working first.
Outlook has a robust security system so you will find when you run the search macro that you have to give it permission to access your emails. You can self-certify your own macros but I have never managed the final step of suppressing the "A macro is accessing your emails" message. If you run the macro once a day, giving permission is not a problem but if you want to have an event routine you will have to confirm the permission every 10 minutes.
Visit the following sites for information or seek help elsewhere:
- Signing your own macros with SelfCert.exe: http://www.howto-outlook.com/howto/selfcert.htm
- Manage Trusted Root Certificates: http://technet.microsoft.com/en-us/library/cc754841.aspx
I can get the instructions in the first to work but not those in the second.
If you can crack the Manage Trusted Root Certificates problem, adding an event routine is not difficult. Within Outlook's Visual Basic Editor, The project explorer will look something like this:
- Project1(VbaProject.OTM)
+ Microsoft Office Outlook Objects
+ Forms
+ Modules
Click the pluses as necessary to get:
- Project1(VbaProject.OTM)
- Microsoft Office Outlook Objects
* ThisOutlookSession
+ Forms
+ Modules
Select ThisOutlookSession
. This is like a module but has extra privileges. Copy the code below to ThisOutlookSession
and you have the start of the event routine you seek.
Option Explicit
Public WithEvents MyNewItems As Outlook.Items
Private Sub Application_MAPILogonComplete()
Dim NS As NameSpace
Set NS = CreateObject("Outlook.Application").GetNamespace("MAPI")
With NS
Set MyNewItems = NS.GetDefaultFolder(olFolderInbox).Items
End With
End Sub
Private Sub myNewItems_ItemAdd(ByVal Item As Object)
Dim text As String
Debug.Print "--------------------"
Debug.Print "Item added to Inbox"
With Item
Debug.Print "Subject: " & .Subject
Debug.Print "Sender name: " & .SenderName
Debug.Print "Sender address: " & .SenderEmailAddress
text = Mid(.Body, 1, 100)
text = Replace(text, vbLf, "{lf}")
text = Replace(text, vbCr, "{cr}")
text = Replace(text, vbTab, "{tb}")
text = Replace(text, Chr$(160), "{nbsp}")
Debug.Print "Start of body: " & text
End With
来源:https://stackoverflow.com/questions/18796071/extract-body-text-from-outlook