Outlook 経由で着信メッセージをローカル ファイル システムに保存しようとしています。私がこれまでに持っているコードは次のとおりです。
Sub save_to_dir(Item As Outlook.MailItem)
'the mail we want to process
Dim objItem As Outlook.MailItem
'question for saving, use subject to save
Dim strPrompt As String, strname As String
'variables for the replacement of illegal characters
Dim sreplace As String, mychar As Variant, strdate As String
'put active mail in this object holder
Set objItem = Outlook.ActiveExplorer.Selection.Item(1)
'check if it's an email ... need to take a closer look cause
'gives an error when something else (contact, task) is selected
'because objItem is defined as a mailitem and code errors out
'saving does work, if you take care that a mailitem is selected
'before executing this code
mypath = "c:\temp\outlook\"
If objItem.Class = olMail Then
' check on subject
If objItem.Subject <> vbNullString Then
strname = objItem.Subject
Else
strname = "No_Subject"
End If
strdate = objItem.ReceivedTime
'define the character that will replace illegal characters
sreplace = "_"
'create an array to loop through illegal characters (saves lines)
For Each mychar In Array("/", "\", ":", "?", Chr(34), "<", ">", "¦")
'do the replacement for each character that's illegal
strname = Replace(strname, mychar, sreplace)
strdate = Replace(strdate, mychar, sreplace)
Next mychar
'Prompt the user for confirmation
'strPrompt = "Are you sure you want to save the item?"
'If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then
objItem.SaveAs mypath & strname & "--" & strdate & ".msg", olMSG
' Else
' MsgBox "You chose not to save."
'End If
End If
End Sub
このコードの問題は、電子メールが受信された瞬間に Outlook でアイテムが選択されている場合、受信メールの代わりに選択されたアイテムを使用することです。
受信メールを取得するにはどうすればよいですか?
ありがとう
編集
この行をデバッグした後、objItem = Outlook.ActiveExplorer.Selection.Item(1) を設定します。
Outlook.ActiveExplorer.Selection.Item(1) に現在の電子メールが含まれていることがわかりましたが、行を実行した後に objItem を調べると、objItem の値は現在 Outlook で選択されている電子メールであり、着信ではありません。 Eメール。
何か案は?