创建 Outlook 规则以根据主题行中的文本创建文件夹(如果需要)

发布于 2024-10-18 23:09:27 字数 1436 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

活泼老夫 2024-10-25 23:09:27

我刚刚为此编写了代码。我的宏在电子邮件中搜索特定字符串,然后获取此后的所有内容并使用该名称创建一个文件夹。您需要一些功能来:
1)检查文件夹是否已经存在
2)如果没有则创建它
3)将MailItem移动到新文件夹
4) 调用这些函数

注意:其中大部分是硬编码的,如果需要,可以更改以接受用户输入。此外,它不适用于子文件夹(您必须对其进行自定义)。

1) 检查文件夹:

Function CheckForFolder(strFolder As String) As Boolean

    Dim olApp As Outlook.Application
    Dim olNS As Outlook.NameSpace
    Dim olInbox As Outlook.MAPIFolder
    Dim FolderToCheck As Outlook.MAPIFolder


    Set olApp = Outlook.Application
    Set olNS = olApp.GetNamespace("MAPI")
    Set olInbox = olNS.GetDefaultFolder(olFolderInbox)

    On Error Resume Next
    Set FolderToCheck = olInbox.Folders(strFolder)
    On Error GoTo 0

    If Not FolderToCheck Is Nothing Then
        CheckForFolder = True
    End If

ExitProc:
    Set FolderToCheck = Nothing
    Set olInbox = Nothing
    Set olNS = Nothing
    Set olApp = Nothing
End Function

2) 创建:

Function CreateSubFolder(strFolder As String) As Outlook.MAPIFolder

    Dim olApp As Outlook.Application
    Dim olNS As Outlook.NameSpace
    Dim olInbox As Outlook.MAPIFolder

    Set olApp = Outlook.Application
    Set olNS = olApp.GetNamespace("MAPI")
    Set olInbox = olNS.GetDefaultFolder(olFolderInbox)

    Set CreateSubFolder = olInbox.Folders.Add(strFolder)

    ExitProc:
    Set olInbox = Nothing
    Set olNS = Nothing
    Set olApp = Nothing
End Function

3) 搜索和移动:

Function SearchAndMove(lookFor As String)

 Dim olApp As Outlook.Application
 Dim olNS As Outlook.NameSpace
 Dim olInbox As Outlook.MAPIFolder
 Dim FolderToCheck As Outlook.MAPIFolder
 Dim myItem As Object
 Dim MyFolder As Outlook.MAPIFolder
 Dim lookIn As String
 Dim newName As String
 Dim location As Integer


 Set olApp = Outlook.Application
 Set olNS = olApp.GetNamespace("MAPI")
 Set olInbox = olNS.GetDefaultFolder(olFolderInbox)
     For Each myItem In olInbox.Items
     lookIn = myItem.Subject
     If InStr(lookIn, lookFor) Then
         location = InStr(lookIn, lookFor)
                 newName = Mid(lookIn, location)
            If CheckForFolder(newName) = False Then
                Set MyFolder = CreateSubFolder(newName)
                myItem.Move MyFolder
                    Else
                Set MyFolder = olInbox.Folders(newName)
                myItem.Move MyFolder
            End If
        End If
    Next myItem
End Function

4) 调用函数:

Sub myMacro()
    Dim str as String
    str = "Thing to look for in the subjectline"
    SearchAndMove (str)

End Sub

I JUST wrote code for this. My macro searches emails for a specific string and then takes everything after that and creates a folder using that name. You'll need a few functions to:
1) Check to see if the folder already exists
2) Create it if it doesn't
3) Move the MailItem to the new folder
4) Call those functions

NOTE: Much of this is hard-coded and could be changed to take user input if desired. Also, it will not work for sub-folders (you'll have to customize that).

1) Check for folder:

Function CheckForFolder(strFolder As String) As Boolean

    Dim olApp As Outlook.Application
    Dim olNS As Outlook.NameSpace
    Dim olInbox As Outlook.MAPIFolder
    Dim FolderToCheck As Outlook.MAPIFolder


    Set olApp = Outlook.Application
    Set olNS = olApp.GetNamespace("MAPI")
    Set olInbox = olNS.GetDefaultFolder(olFolderInbox)

    On Error Resume Next
    Set FolderToCheck = olInbox.Folders(strFolder)
    On Error GoTo 0

    If Not FolderToCheck Is Nothing Then
        CheckForFolder = True
    End If

ExitProc:
    Set FolderToCheck = Nothing
    Set olInbox = Nothing
    Set olNS = Nothing
    Set olApp = Nothing
End Function

2) Create:

Function CreateSubFolder(strFolder As String) As Outlook.MAPIFolder

    Dim olApp As Outlook.Application
    Dim olNS As Outlook.NameSpace
    Dim olInbox As Outlook.MAPIFolder

    Set olApp = Outlook.Application
    Set olNS = olApp.GetNamespace("MAPI")
    Set olInbox = olNS.GetDefaultFolder(olFolderInbox)

    Set CreateSubFolder = olInbox.Folders.Add(strFolder)

    ExitProc:
    Set olInbox = Nothing
    Set olNS = Nothing
    Set olApp = Nothing
End Function

3) Search and move:

Function SearchAndMove(lookFor As String)

 Dim olApp As Outlook.Application
 Dim olNS As Outlook.NameSpace
 Dim olInbox As Outlook.MAPIFolder
 Dim FolderToCheck As Outlook.MAPIFolder
 Dim myItem As Object
 Dim MyFolder As Outlook.MAPIFolder
 Dim lookIn As String
 Dim newName As String
 Dim location As Integer


 Set olApp = Outlook.Application
 Set olNS = olApp.GetNamespace("MAPI")
 Set olInbox = olNS.GetDefaultFolder(olFolderInbox)
     For Each myItem In olInbox.Items
     lookIn = myItem.Subject
     If InStr(lookIn, lookFor) Then
         location = InStr(lookIn, lookFor)
                 newName = Mid(lookIn, location)
            If CheckForFolder(newName) = False Then
                Set MyFolder = CreateSubFolder(newName)
                myItem.Move MyFolder
                    Else
                Set MyFolder = olInbox.Folders(newName)
                myItem.Move MyFolder
            End If
        End If
    Next myItem
End Function

4) call function:

Sub myMacro()
    Dim str as String
    str = "Thing to look for in the subjectline"
    SearchAndMove (str)

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文