我想在Outlook中选择邮件,然后将其复制到文本文件中,该文件的类别和接收时间为txt标题。例如,如果选择了三个消息,我想要三个文本文件。
Sub selectMSGToText()
Dim oMail As Outlook.MailItem
Set oMail = Application.ActiveExplorer.Selection.Item(1)
Dim sMail As Object
Set sMail = Application.ActiveExplorer.Selection
Dim categories As String: categories = oMail.categories
Dim rtimeAs String: rtime= oMail.ReceivedTime
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim path As String
path = "D:\test\"
Dim oFile As Object
For Each oMail In sMail
Set oFile = fso.CreateTextFile(path & categories & "|" & rtime & ".txt", True, True)
oFile.WriteLine "test"
oFile.Close
Set fso = Nothing
Set oFile = Nothing
Next oMail
End Sub
我希望所有选定的邮件都作为单独的txt文件放在文件夹D:\ test中,并具有各自的类别和接收时间作为标题。
答案 0 :(得分:0)
在Windows文件名中,|
是非法的,/
字符串中可能没有\
或:
或ReceivedTime
。
更改为:
Dim rtime As Date: rtime= oMail.ReceivedTime
并使用类似的内容:
Set oFile = fso.CreateTextFile(path & categories & "_" & Format$(rtime, "dd_mm_yy_hhmmss") & ".txt", True, True)
(这假设categories
绝不会包含非法字符)
您传递的第一个true
导致现有文件被覆盖,因此在同一秒内收到的多封电子邮件将丢失。
Set fso = Nothing
需要在循环之外,否则在第二次迭代中为Nothing
。