2

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メール。

何か案は?

4

2 に答える 2

1

参考になる良い記事がここにあります: http://www.outlookcode.com/article.aspx?id=62

サンプル 1 を次のように調整しました。

Sub save_to_dir_test1(mymail As MailItem)
Dim strID As String
Dim objMail As Outlook.MailItem

strID = mymail.EntryID
Set objMail = Application.Session.GetItemFromID(strID)
mypath = "c:\temp\outlook\"
strdate = objMail.ReceivedTime
If objMail.Subject <> vbNullString Then
        strname = objMail.Subject
   Else
        strname = "No_Subject"
End If
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
objMail.SaveAs mypath & strname & "--" & strdate & ".msg", olMSG

Set objMail = Nothing
End Sub
于 2012-12-31T09:06:52.027 に答える
0

受信メールの Outlook で「ルール」を設定し、アクションで「スクリプトの実行」を選択し、以下のサブを選択できます。

そして、このoutlooksessionモジュールのモジュールに以下のサブを入れてください

Sub testing(MyMail As MailItem)
 MyMail.SaveAs ' your path here
end sub

それが役に立てば幸い

于 2012-12-27T02:38:54.760 に答える