使用动态路径将电子邮件移动到公用文件夹

发布于 2024-12-06 22:58:43 字数 3433 浏览 0 评论 0原文

在我们的公司环境中,我们有一个包含许多子文件夹的邮箱(不是默认收件箱)。我们还有一个公共文件夹,它是邮箱文件夹结构的精确镜像。

我正在尝试检测选定电子邮件的路径并将该电子邮件移动到公共文件夹中的镜像文件夹。

我想说这段代码的 95% 是正确的,但我留下了 Outlook 错误消息“无法移动项目”。

该代码应该执行以下操作:
1. 检测所选电子邮件的当前文件夹
2. 将MAPIFolder转换为路径字符串
3.缩短字符串以删除根邮箱目录结构
4. 将剩余的字符串添加到公用文件夹的根目录结构中
5. 将结果路径转换回 MAPIFolder
6. 将选定的电子邮件移动到公共文件夹中的镜像文件夹

Sub PublicFolderAutoArchive()

    Dim olApp As Object
    Dim currentNameSpace As NameSpace
    Dim wipFolder As MAPIFolder
    Dim objFolder As MAPIFolder
    Dim pubFolder As String
    Dim wipFolderString As String
    Dim Messages As Selection
    Dim itm As Object
    Dim Msg As MailItem
    Dim Proceed As VbMsgBoxResult

    Set olApp = Application
    Set currentNameSpace = olApp.GetNamespace("MAPI")
    Set wipFolder = Application.ActiveExplorer.CurrentFolder
    Set Messages = ActiveExplorer.Selection

    ' Destination root directory'
    ' Tried with both "\\Public Folders" and "Public Folders" .. neither worked
    pubFolder = "\\Public Folders\All Public Folders\InboxMirror"

    ' wipFolder.FolderPath Could be any folder in our mailbox such as:  
    ' "\\Mailbox - Corporate Account\Inbox\SubFolder1\SubFolder2"
    ' however, the \\Mailbox - Corporate Account\Inbox\" part is 
    ' static and never changes so the variable below removes the static
    ' section, then the remainder of the path is added onto the root 
    ' of the public folder path which is an exact mirror of the inbox.
    ' This is to allow a dynamic Archive system where the destination 
    'path matches the source path except for the root directory.
    wipFolderString = Right(wipFolder.FolderPath, Len(wipFolder.FolderPath) - 35)

    ' tried with and without the & "\" ... neither worked
    Set objFolder = GetFolder(pubFolder & wipFolderString & "\")

    If Messages.Count = 0 Then
        Exit Sub
    End If

    For Each itm In Messages
        If itm.Class = olMail Then
            Proceed = MsgBox("Are you sure you want archive the message to the Public Folder?", _
            vbYesNo + vbQuestion, "Confirm Archive")
            If Proceed = vbYes Then
                Set Msg = itm
                Msg.Move objFolder
            End If
        End If
    Next
End Sub

Public Function GetFolder(strFolderPath As String) As MAPIFolder
  ' strFolderPath needs to be something like
  '   "Public Folders\All Public Folders\Company\Sales" or
  '   "Personal Folders\Inbox\My Folder"

  Dim objApp As Outlook.Application
  Dim objNS As Outlook.NameSpace
  Dim colFolders As Outlook.Folders
  Dim objFolder As Outlook.MAPIFolder
  Dim arrFolders() As String
  Dim I As Long
  On Error Resume Next

  strFolderPath = Replace(strFolderPath, "/", "\")
  arrFolders() = Split(strFolderPath, "\")
  Set objApp = Application
  Set objNS = objApp.GetNamespace("MAPI")
  Set objFolder = objNS.Folders.Item(arrFolders(0))
  If Not objFolder Is Nothing Then
    For I = 1 To UBound(arrFolders)
      Set colFolders = objFolder.Folders
      Set objFolder = Nothing
      Set objFolder = colFolders.Item(arrFolders(I))
      If objFolder Is Nothing Then
        Exit For
      End If
    Next
  End If

Set GetFolder = objFolder
Set colFolders = Nothing
Set objNS = Nothing
Set objApp = Nothing
End Function

注意:上面的邮箱只是一个示例,并非实际的邮箱名称。我使用 MsgBox 来确认路径字符串已与所有适当的反斜杠正确连接,并且 Right() 函数正在从源路径获取我需要的内容。

In our Corporate environment we have a Mailbox (not the default inbox) with many sub folders. We also have a Public Folder which is an exact mirror of the Mailbox folder structure.

I am trying to detect the path of a selected email and move that email to its mirrored folder in the Public Folders.

I would say 95% of this code is correct but I am left with an Outlook error message "Can't move the items."

The code is supposed to do the following:
1. detects the current folder of the selected email(s)
2. converts the MAPIFolder into a path string
3. shortens the string to remove the root Mailbox directory structure
4. adds the remaining string onto the root directory structure of the public folder
5. converts the resulting path back into a MAPIFolder
6. move the selected email(s) to the mirrored folder in the Public Folders

Sub PublicFolderAutoArchive()

    Dim olApp As Object
    Dim currentNameSpace As NameSpace
    Dim wipFolder As MAPIFolder
    Dim objFolder As MAPIFolder
    Dim pubFolder As String
    Dim wipFolderString As String
    Dim Messages As Selection
    Dim itm As Object
    Dim Msg As MailItem
    Dim Proceed As VbMsgBoxResult

    Set olApp = Application
    Set currentNameSpace = olApp.GetNamespace("MAPI")
    Set wipFolder = Application.ActiveExplorer.CurrentFolder
    Set Messages = ActiveExplorer.Selection

    ' Destination root directory'
    ' Tried with both "\\Public Folders" and "Public Folders" .. neither worked
    pubFolder = "\\Public Folders\All Public Folders\InboxMirror"

    ' wipFolder.FolderPath Could be any folder in our mailbox such as:  
    ' "\\Mailbox - Corporate Account\Inbox\SubFolder1\SubFolder2"
    ' however, the \\Mailbox - Corporate Account\Inbox\" part is 
    ' static and never changes so the variable below removes the static
    ' section, then the remainder of the path is added onto the root 
    ' of the public folder path which is an exact mirror of the inbox.
    ' This is to allow a dynamic Archive system where the destination 
    'path matches the source path except for the root directory.
    wipFolderString = Right(wipFolder.FolderPath, Len(wipFolder.FolderPath) - 35)

    ' tried with and without the & "\" ... neither worked
    Set objFolder = GetFolder(pubFolder & wipFolderString & "\")

    If Messages.Count = 0 Then
        Exit Sub
    End If

    For Each itm In Messages
        If itm.Class = olMail Then
            Proceed = MsgBox("Are you sure you want archive the message to the Public Folder?", _
            vbYesNo + vbQuestion, "Confirm Archive")
            If Proceed = vbYes Then
                Set Msg = itm
                Msg.Move objFolder
            End If
        End If
    Next
End Sub

Public Function GetFolder(strFolderPath As String) As MAPIFolder
  ' strFolderPath needs to be something like
  '   "Public Folders\All Public Folders\Company\Sales" or
  '   "Personal Folders\Inbox\My Folder"

  Dim objApp As Outlook.Application
  Dim objNS As Outlook.NameSpace
  Dim colFolders As Outlook.Folders
  Dim objFolder As Outlook.MAPIFolder
  Dim arrFolders() As String
  Dim I As Long
  On Error Resume Next

  strFolderPath = Replace(strFolderPath, "/", "\")
  arrFolders() = Split(strFolderPath, "\")
  Set objApp = Application
  Set objNS = objApp.GetNamespace("MAPI")
  Set objFolder = objNS.Folders.Item(arrFolders(0))
  If Not objFolder Is Nothing Then
    For I = 1 To UBound(arrFolders)
      Set colFolders = objFolder.Folders
      Set objFolder = Nothing
      Set objFolder = colFolders.Item(arrFolders(I))
      If objFolder Is Nothing Then
        Exit For
      End If
    Next
  End If

Set GetFolder = objFolder
Set colFolders = Nothing
Set objNS = Nothing
Set objApp = Nothing
End Function

Note: The mailbox above is just an example and is not the actual mailbox name. I used MsgBox to confirm the path string was being joined correctly with all appropriate back slashes and that the Right() function was getting what I needed from the source path.

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

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

发布评论

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

评论(2

相守太难 2024-12-13 22:58:43

我不确定,但应该是这样的?

set objApp = New Outlook.Application

而不是

    set objApp = Application

I'm not sure, but should be something like?

set objApp = New Outlook.Application

instead of

    set objApp = Application
旧人九事 2024-12-13 22:58:43

从代码上看,您的 GetFolder() 实现似乎不喜欢您在路径开头给出的双反斜杠。甚至在函数的开头有一条注释表明了这一点。尝试从 pubFolder 前面删除这两个字符。

或者,您可以更改 GetFolder 以允许它们。像这样的几行应该可以解决问题。

If Left(strFolderPath, 2) = "\\" Then
    strFolderPath = Right(strFolderPath, Len(strFolderPath) - 2)
End If

From glancing at the code, it appears that your GetFolder() implementation doesn't like the double-backslash you're giving at the start of the path. There's even a comment indicating this at the start of the function. Try removing those two chars from the front of pubFolder.

Alternatively, you could alter GetFolder to permit them. A few lines like this should do the trick.

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