Sub 特定の送信者からのメールtxt保存()
Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim senderEmail As String
Dim savePath As String
Dim count As Integer
Dim specifiedDate As Date
Dim dateInput As String
Dim errorLogFile As String
' 特定の送信者のメールアドレスを設定
senderEmail = "example@example.com" ' ここに送信者のメールアドレスを入力
' 保存先のパスを設定
savePath = "C:\Your\Path\" ' ここに保存先のパスを入力
errorLogFile = savePath & "error_log.txt"
' ユーザーに日付を入力させる
dateInput = InputBox("メールの受信日以降のデータを取得する日付を入力してください (YYYY/MM/DD):", "日付入力")
' 入力された日付をDate型に変換
On Error Resume Next
specifiedDate = CDate(dateInput)
If Err.Number <> 0 Then
MsgBox "正しい日付形式ではありません。"
Exit Sub
End If
On Error GoTo 0
' Outlookアプリケーションを取得
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set olFolder = olNs.GetDefaultFolder(olFolderInbox) ' 受信トレイを指定
count = 0
' サブフォルダー以下のすべてのメールを検索
SearchEmails olFolder, senderEmail, specifiedDate, count, savePath, errorLogFile
MsgBox count & " 件のメールを保存しました。"
End Sub
Sub SearchEmails(ByVal olFolder As Outlook.MAPIFolder, ByVal senderEmail As String, _
ByVal specifiedDate As Date, ByRef count As Integer, _
ByVal savePath As String, ByVal errorLogFile As String)
Dim olItem As Object
Dim olMail As Outlook.MailItem
Dim fileName As String
Dim fileNum As Integer
Dim att As Attachment
Dim errorFileNum As Integer
' フォルダー内のメールをループ
For Each olItem In olFolder.Items
If TypeOf olItem Is Outlook.MailItem Then
Set olMail = olItem
If olMail.SenderEmailAddress = senderEmail And olMail.ReceivedTime >= specifiedDate Then
On Error Resume Next
count = count + 1
' 件名を短縮してファイル名に使用
fileName = savePath & ShortenFileName(olMail.Subject, 50) & "_" & count & ".txt"
' メールをテキストファイルとして保存
fileNum = FreeFile
Open fileName For Output As #fileNum
Print #fileNum, "件名: " & olMail.Subject
Print #fileNum, "受信者: " & olMail.To
Print #fileNum, "送信者: " & olMail.SenderName
Print #fileNum, "文面: " & olMail.Body
Close #fileNum
' 添付ファイルを処理
For Each att In olMail.Attachments
If LCase(Right(att.FileName, 3)) = "csv" Then
' CSVファイルの場合、Excel形式で保存
SaveAttachmentAsExcel att, savePath
Else
' その他のファイルは通常保存
att.SaveAsFile savePath & att.FileName
End If
Next att
If Err.Number <> 0 Then
' エラーが発生した場合
errorFileNum = FreeFile
Open errorLogFile For Append As #errorFileNum
Print #errorFileNum, "エラー: " & Err.Description & " (メール件名: " & olMail.Subject & ")"
Close errorFileNum
Err.Clear
End If
On Error GoTo 0
End If
End If
Next olItem
' サブフォルダーを再帰的に検索
Dim subFolder As Outlook.MAPIFolder
For Each subFolder In olFolder.Folders
SearchEmails subFolder, senderEmail, specifiedDate, count, savePath, errorLogFile
Next subFolder
End Sub
' CSVファイルをExcelとして保存する関数
Sub SaveAttachmentAsExcel(ByVal att As Attachment, ByVal savePath As String)
Dim tempFilePath As String
Dim xlApp As Object
Dim xlWorkbook As Object
Dim xlSheet As Object
Dim fileExtension As String
' 添付ファイルを一時的に保存するパス
tempFilePath = savePath & att.FileName
att.SaveAsFile tempFilePath
' Excelアプリケーションを起動
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False ' Excelを非表示にする
' CSVファイルを開く
Set xlWorkbook = xlApp.Workbooks.Open(tempFilePath)
Set xlSheet = xlWorkbook.Sheets(1)
' Excelとして保存する(拡張子を変更)
fileExtension = LCase(Right(att.FileName, 3))
If fileExtension = "csv" Then
xlWorkbook.SaveAs savePath & ShortenFileName(att.FileName, 50) & ".xlsx", 51 ' 51 = xlOpenXMLWorkbook (xlsx)
End If
' 後処理
xlWorkbook.Close False
xlApp.Quit
' 一時ファイルを削除
Kill tempFilePath
End Sub
' ファイル名を短縮する関数
Function ShortenFileName(s As String, maxLength As Integer) As String
If Len(s) > maxLength Then
ShortenFileName = Left(s, maxLength - 3) & "..." ' 最大長を超える場合、短縮
Else
ShortenFileName = s
End If
End Function