[英]How can I Open and read a txt file attachment using VBA in Outlook?
I am trying to automate a process where I get an email with an attached file txt我正在尝试自动化一个过程,在该过程中我收到一封带有附加文件 txt 的电子邮件
I open and I check the date on the second line and saved to a folder with that date in a particular location.我打开并检查第二行上的日期,并将该日期保存到特定位置的文件夹中。
With this code and a message rule in Outlook I saved the file in the folder where you wish:使用此代码和 Outlook 中的消息规则,我将文件保存在您希望的文件夹中:
Now the question is:现在的问题是:
How to read the second line of txt before saving?如何在保存前阅读txt的第二行?
Public Sub SalvarAnexo(Item As MailItem)
Dim Atmt As Attachment
Dim FileName As String
MsgBox "Mensagem Recebida de " & Item.Sender & "!"
For Each Atmt In Item.Attachments
If Right(Atmt.FileName, 3) = "txt" Then
FileName = "C:\temp\" & Format(Item.CreationTime, "yyyymmdd_hhnnss_") & Atmt.FileName
Atmt.SaveAsFile FileName
End If
Next Atmt
End Sub
You can use the FileScriptingObject
to grab the second line of the text file like so.您可以使用FileScriptingObject
像这样抓取文本文件的第二行。
Public Sub SalvarAnexo(Item)
Dim Atmt As Attachment
Dim FileName As String
Dim objFSO As Object
Dim objFile As Object
Dim strTest As String
MsgBox "Mensagem Recebida de " & Item.Sender & "!"
For Each Atmt In Item.Attachments
If Right$(Atmt.FileName, 3) = "txt" Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
FileName = "C:\temp\" & Format(Item.CreationTime, "yyyymmdd_hhnnss_") & Atmt.FileName
Atmt.SaveAsFile FileName
Set objFile = objFSO.OpenTextFile(FileName, 1)
strTest = objFile.ReadLine
strTest = objFile.ReadLine
objFile.Close
MsgBox "Your date is " & strTest
End If
Next Atmt
End Sub
The Outlook object model doesn't provide anything for reading attached files on the fly. Outlook 对象模型不提供任何用于即时读取附加文件的内容。 You need to save the attachment on the disk as a file and then open it for reading the second line.您需要将附件作为文件保存在磁盘上,然后打开它以阅读第二行。 Also you may consider using the low-level code (or any third-party wrapper) - Extended MAPI.您也可以考虑使用低级代码(或任何第三方包装器) - 扩展 MAPI。 It allows to open an attachment as a stream of bytes.它允许以字节流的形式打开附件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.