Reputation:
I am trying to automate a process where I get an email with an attached file 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:
Now the question is:
How to read the second line of txt before saving?
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
Upvotes: 2
Views: 7382
Reputation: 55682
You can use the FileScriptingObject
to grab the second line of the text file like so.
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
Upvotes: 3
Reputation: 49395
The Outlook object model doesn't provide anything for reading attached files on the fly. 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. It allows to open an attachment as a stream of bytes.
Upvotes: -1