1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Outlook VBAのmoveメソッドで移動できないアイテム対応(エラーハンドリングで処理をスキップ)

Posted at

Outlook のクリーンアップツールを使ってメールのアーカイブをしようとしたところ、うまく保管出来ない(移動元のフォルダにメールが残ってしまう)事象が発生。
クリーンアップツールを何度試してもメールは移動せず、手動でやろうにも対象ユーザーのフォルダ構成が広大すぎたので、Outlook VBAで振り分けマクロを作成することに。

フォルダ内アイテムの移動にはMoveメソッドを利用したが、それで移動できないアイテム種別と対応について記載。

移動できないもの

検知した範囲では以下。

おそらく他にもあるが、途中でエラー全部をスキップするようにしたため、未調査。

対応

アイテムのタイプを調べて処理を飛ばす

対象アイテムのTypeNameがMailItemでない場合に、Gotoで処理を飛ばします。

sub
    For itemIndex = sourceFolder.Items.Count To 1 Step -1
        ' check the type of the item at the given index
        If TypeName(sourceFolder.Items.Item(itemIndex)) <> "MailItem" Then
            ' skip this item, since it is not an email
            GoTo Skipitem
        End If

        Set email = sourceFolder.Items.Item(itemIndex)
        email.Move destinationFolder
    Skipitem:
    Next

end sub

この方法で会議出席依頼は処理をスキップ出来たのですが、送信済メッセージの修正・取り消し通知は「このクラスはサポートされていません」のエラーで動作せず。
該当のアイテムはOutlook VBAで処理できないようです。
※エラーメッセージの詳細・情報ソースは失念しました。発掘出来たら追記。

エラーハンドリングを使って、エラーが出たら処理を飛ばす

上記のアイテムや、ユーザー環境の未知のエラーに対応するため、エラーが出たら全部処理を飛ばす方法で回避。
前述のコードに追記しているので、メールアイテムの種類を確認してスキップする処理入れてありますが、おそらく不要。

記述順序の通りの処理順序とならないので混乱しそうですが、エラーが出た際は以下のような動作。

  1. On Error GoTo ErrorHandlerでErrorHandler:に飛ぶ
  2. Resume Skipitem でエラーを無視してSkipitem:に飛ぶ
  3. NextでForに戻る
sub
    On Error GoTo ErrorHandler ' エラー発生時にErrorHandlerラベルにジャンプするように設定

    For itemIndex = sourceFolder.Items.Count To 1 Step -1
        ' check the type of the item at the given index
        If TypeName(sourceFolder.Items.Item(itemIndex)) <> "MailItem" Then
            ' skip this item, since it is not an email
            GoTo Skipitem
        End If

        Set email = sourceFolder.Items.Item(itemIndex)
        email.Move destinationFolder

    Skipitem:
    Next
    On Error GoTo 0 ' エラー処理を解除する

    ErrorHandler:
    If Err.Number <> 0 Then
        ' エラーが発生した場合に実行する処理を記述する
        Resume Skipitem ' エラーが発生した処理をスキップし、Skipitemラベルにジャンプする
    End If

end sub


そのほか

自身の環境ではなく、ユーザーの環境へ導入すると導入・実行・削除をこちらの手でやらないといけないのでめんどくさいです。
まずはクリーンアップツールや手動でも対応してもらい、それでも対応しきれない場合にやってもらうのがおすすめです。

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?