使用 VBA 将电子邮件消息写入 Outlook 中的平面文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/400941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing email messages to flat files in Outlook with VBA
提问by Craig
I have written a VBA app that opens a folder in outlook and then iterates through the messages. I need to write the message bodies (with some tweaking) to a single flat file. My code is as follows...
我编写了一个 VBA 应用程序,它在 Outlook 中打开一个文件夹,然后遍历消息。我需要将消息正文(经过一些调整)写入单个平面文件。我的代码如下...
Private Sub btnGo_Click()
Dim objOutlook As New Outlook.Application
Dim objNameSpace As Outlook.NameSpace
Dim objInbox As MAPIFolder
Dim objMail As mailItem
Dim count As Integer
Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set objInbox = objNameSpace.GetDefaultFolder(olFolderInbox)
count = 0
For Each objMail In objInbox.Items
lblStatus.Caption = "Count: " + CStr(count)
ProcessMailItem (objMail)
count = count + 1
Next objMail
End If
End Sub
The part in question is "ProcessMailItem". As I am not overly concerned with performance at this stage so the very inefficent "open, append, close" file methodology is fine for this example.
有问题的部分是“ProcessMailItem”。由于我并不太关心现阶段的性能,因此非常低效的“打开、附加、关闭”文件方法适用于本示例。
I know I could spend some time looking up the answer with google but I checked here first and there was no good answers for this. Being a fan of Stackoverflow I hope that putting this up here will help future developers looking for answers. Thanks for your patience.
我知道我可以花一些时间用谷歌查找答案,但我先在这里检查,没有很好的答案。作为 Stackoverflow 的粉丝,我希望把它放在这里能帮助未来的开发人员寻找答案。谢谢你的耐心。
回答by Jon Fournier
You can get away with writing to a file without using any objects, just using the built in VBA file tools:
您可以在不使用任何对象的情况下写入文件,只需使用内置的 VBA 文件工具:
Open "C:\file.txt" for append as 1
Print #1, SomeStringVar
Close #1
回答by Eric Ness
If you don't mind re-opening the output file each time you append some text, then this should work.
如果您不介意每次附加一些文本时重新打开输出文件,那么这应该可行。
Private Sub ProcessMailItem(objMail As MailItem)
Dim fso As New FileSystemObject
Dim ts As TextStream
Set ts = fso.OpenTextFile("C:\Outputfile.txt", ForAppending, True)
ts.Write(objMail.Body)
ts.Close()
Set ts = Nothing
Set fso = Nothing
End Sub
You'll also need to add a reference to the Microsoft Scripting Runtime library. This has FileSystemObject in it.
您还需要添加对 Microsoft Scripting Runtime 库的引用。这里面有 FileSystemObject。
回答by jim
You also have to take care of the security popup "trying to access email addresses" which is covered in Outlook "Object Model Guard" Security Issues for Developers
您还必须注意安全弹出窗口“试图访问电子邮件地址”,这在Outlook“对象模型保护”开发人员安全问题中有所介绍
Public Sub ProcessMailItem(objMail As MailItem)
Dim FSO As New FileSystemObject
Dim ts As TextStream
Dim loc As String
Dim subject As String
Dim strID As String
' per http://www.outlookcode.com/article.aspx?ID=52
Dim olNS As Outlook.NameSpace
Dim oMail As Outlook.MailItem
strID = MyMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set oMail = olNS.GetItemFromID(strID)
subject = oMail.subject
Set ts = FSO.OpenTextFile("C:\Documents and Settings\tempuser\My Documents\EMAILS\" + subject, ForAppending, True)
ts.Write (oMail.Body)
ts.Close
Set ts = Nothing
Set FSO = Nothing
Set oMail = Nothing
Set olNS = Nothing
End Sub
结束子