如何运行需要参数"myitem As Outlook.mailItem"的宏?



我有这个:

Public Sub MoveMails (myitem As Outlook.mailItem)
...
End Sub

当我收到一封新电子邮件时,会根据规则自动运行。

我想在我想的时候推出这个。我试过用Call MoveMails,但我不知道myitem As Outlook.mailItem的论点。


我收到几封邮件。我的想法是移动所有人。我有几个条件:按主题筛选,按身体筛选。。。然后我把它们移到不同的文件夹。

现在我知道您希望同时在多个项目上运行此程序。

从子中删除参数(myitem As Outlook.MailItem),以便在文件夹或所选内容中的所有项目上运行。

复制自您发布的代码(我也更改了一些格式(;

Public Sub Corrected()
'==========================================
'Declare variables:
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.Folder
Dim myDestFolder As Outlook.Folder
'Set variables:
Set myNameSpace = Application.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myDestFolder = myInbox.Folders("CHECK")
'===========================================
'Declare the variable MyItem but don't set it:
Dim MyItem As Outlook.MailItem

'Create a loop for all items in a certain folder:
For Each MyItem In myInbox 'Or for a subfolder use '...In MyInbox.Folders("FolderName")'
'Or to check selected items you could use the below:
'For Each MyItem in Application.ActiveExplorer.Selection
'if body contains alarm then move
If InStr(MyItem.Body, "alarm") > 0 Then
MyItem.Move myDestFolder
End If

'if subject contains Urgent move
If InStr(MyItem.Subject, "Urgent") > 0 Then
MyItem.Move myDestFolder
End If

'if body contains MASTER then categorize
If InStr(MyItem.Body, "MASTER") > 0 Then
MyItem.Categories = "Boss"
MyItem.Save
End If
Next MyItem 'This loops onto the next item in the selection or folder,
'so it checks them all automatically.
End Sub

我在这里找到了一个用于选择当前项目的子项-它有在自己的窗口(检查器(或在Outlook主窗口(资源管理器(中打开项目的选项。

我建议您将该页面中的自定义函数GetCurrentItem()添加到模块中,并将其用作myitem参数。

因此,调用MoveMails子Call Movemails(GetCurrentItem())

Function GetCurrentItem() As Object
Dim objApp As Outlook.Application

Set objApp = Application
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
End Select

Set objApp = Nothing
End Function

最新更新