0

私はVBを初めて使用し、電子メールの受信時に次のタスクを自動的に実行するVBAマクロを作成しようと苦労しています:

1) 電子メールが内部から発信されたのか、外部から発信されたのかを確認します。(外部無視の場合)
2) 電子メールに添付ファイルがあるかどうかを確認します。(添付ファイルがない場合は無視します)
3) 添付ファイル名をチェックします。「レポート」のようにする必要があります (フルネームは通常、「Report 12198 blah blah.pdf」です)。(添付ファイル名が「レポート」のようでない場合は無視します)
4) 添付ファイルを G:\Test に保存します
5) 電子メールを「完了」という名前の Outlook フォルダに移動します

添付ファイルを保存したり、電子メールをフォルダーに移動したりするためのコードを含む多くのサイトを見てきましたが、他の誰も私と同じ問題を抱えていないようです。この2つを組み合わせます。

私は当初、Outlook ルールを使用してこれを行うことができると考えていましたが、これまでに (添付ファイルを保存するための) コードをスクリプトとして表示することはできませんでした。

さらに、「移動」や「削除」などの操作を行うときに「For Each」ループを使用できないというサイトを読んだことがありますが (どのサイトか思い出せません)、私もそうではありません。以下のコードが使用可能かどうかを確認してください。

どんな助けでも大歓迎です。これは私が現時点で持っているコードです:

Sub GetAttachments()

On Error GoTo GetAttachments_err

Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim Item As Object
Dim Atmt As Attachment
Dim StringLength As Long
Dim FileName As String
Dim i As Integer

Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
i = 0

If Inbox.Items.Count = 0 Then
   MsgBox "There are no messages in the Inbox.", vbInformation, _
          "Nothing Found"
    Exit Sub
End If

For Each Item In Inbox.Items
   For Each Atmt In Item.Attachments
        If Left(Atmt.FileName, 6) Like "*REPORT*" Then
            StringLength = Len(Atmt.FileName)
            FileName = "G:\Test\" & Left(Atmt.FileName, (StringLength - 13)) & Format(Item.CreationTime, "ddmmmyyyy") & ".pdf"
      Atmt.SaveAsFile FileName
      i = i + 1
      End If
   Next Atmt
Next Item

If i > 0 Then
   MsgBox "I found " & i & " attached files." _
      & vbCrLf & "I have saved them into the Test Folder." _
      & vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
Else
   MsgBox "I didn't find any attached files in your mail.", vbInformation, _
   "Finished!"
End If

GetAttachments_exit:
    Set Atmt = Nothing
    Set Item = Nothing
    Set ns = Nothing
    Exit Sub

GetAttachments_err:
   MsgBox "An unexpected error has occurred." _
      & vbCrLf & "Please note and report the following information." _
      & vbCrLf & "Macro Name: GetAttachments" _
      & vbCrLf & "Error Number: " & Err.Number _
      & vbCrLf & "Error Description: " & Err.Description _
      , vbCritical, "Error!"
   Resume GetAttachments_exit

Exit Sub

End Sub
4

3 に答える 3

0

I think the problem is in the declaration part.
Try to change this:

Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim Atmt As Attachment
Dim Item as Object

with this:

Dim ns As outlook.NameSpace
Dim Inbox As outlook.MAPIFolder
Dim Atmt As outlook.Attachment
Dim Item as outlook.MailItem

Then in your code, you only check for the attachments?
I can't seem to find the checking for internal or external?

于 2013-11-06T09:21:44.203 に答える
0

ちなみに、スクリプトをルールとして使用する場合は、関数を次のように定義します

Sub GetAttachments(mItem As MailItem)

ルールを編集するときに関数のリストに表示されます

于 2013-11-07T09:41:23.653 に答える