简体   繁体   English

如何在 Outlook 中使用 VBA 打开和读取 txt 文件附件?

[英]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.

相关问题 使用 VBA 在 Outlook 中添加文件作为附件时,如何使打开的文件夹对话框窗口成为活动窗口? - When adding a file as attachment in Outlook using VBA how can I make the open folder dialog window the active window? 如何使用 VBA 打开 excel 文件的附件并在 Outlook 中确定的文件夹中发送消息之前验证文件内部? - How can I open with VBA a attachment of excel file and verify inside the file before seva the message in a determined folder in outlook? Access VBA 如何从附件字段中读取 txt 文件 - Access VBA how to read a txt file from an attachment field 使用 VBA 在 Outlook 2010 中打开 Excel 附件 - Open an Excel attachment in Outlook 2010 using VBA 在Outlook中使用VBA阅读带有附件的电子邮件正文 - Read body of email with attachment using VBA in Outlook 如何将文本附件从特定的Outlook文件夹保存到文件。 使用Excel VBA - How do i save a text attachment form a specific outlook folder to file. using Excel VBA 如何使用Excel VBA打开在特定时间范围内发送到特定Outlook文件夹的Outlook excel附件? - How to open an Outlook excel attachment using Excel VBA, sent in a particular time range to a specific Outlook folder? 使用VBA的Outlook附件 - outlook attachment using VBA 如何使用 Outlook VBA 浏览 Windows 文件夹并在其中保存 Outlook 附件 - How Do I Browse Windows Folders And Save An Outlook Attachment There Using Outlook VBA 使用 VBA 预览 Outlook 附件 - outlook attachment preview using VBA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM