【问题标题】:Reference subfolder of Inbox to move mail引用收件箱的子文件夹以移动邮件
【发布时间】:2017-04-21 22:45:36
【问题描述】:

我有 Outlook 2010。我收到的电子邮件具有相同的主题行,并带有要打开的 PDF。当 PDF 打开时,Adobe 询问我是否要将其添加到 Excel 响应文件中,我说是。

当 Adob​​e 询问是否要添加到响应文件时,我希望它以“好的”响应,但没有它我也能应付。在这一行:

Set SubFolder = Mailbox.Folders("Response File")

我收到一个错误:

尝试的操作失败。找不到对象。

未读电子邮件所在的子文件夹在我的收件箱下方称为“!响应文件”(不带引号)。打开 PDF 后,我想将电子邮件标记为已读,并移至另一个名为“已提取”(不带引号)的子文件夹(在收件箱下)。

Sub GetAttachments()
  On Error GoTo GetAttachments_err
  Dim ns As NameSpace
  Dim Inbox As MAPIFolder
  Dim SubFolder As MAPIFolder
  Dim Item As Object
  Dim Atmt As Attachment
  Dim FileName As String
  Dim i As Integer

  Set ns = GetNamespace("MAPI")
  Set Inbox = ns.GetDefaultFolder(olFolderInbox)
  Set Mailbox = Inbox.Parent
  Set SubFolder = Mailbox.Folders("!Response File")
  i = 0

  'check if there is any mail in the folder'
  If SubFolder.Items.Count = 0 Then
    MsgBox "There are no messages in the folder.", vbInformation, _
    "Nothing Found"
    Exit Sub
  End If

  'Check each message and save the attachment'
  If SubFolder.Items.Count > 0 Then
    For Each Item In SubFolder.Items
      If Item.UnRead = True Then
        For Each Atmt In Item.Attachments
          FileName = "C:\Users\abrupbac\Desktop\Response Emails\" & Atmt.FileName
          Atmt.SaveAsFile FileName 'saves each attachment'

          'this code opens each attachment'
          Set myShell = CreateObject("WScript.Shell")
          myShell.Run FileName

          'this sets the email as read'
          Item.UnRead = False
          'updates the counter'
          i = i + 1

        Next Atmt
      End If
    Next Item
  End If

  'Display results

  If i > 0 Then
    MsgBox "I found " & i & " attached files." _
     & vbCrLf & "They are saved on your desktop" _
     & vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
  Else
    MsgBox "I didn't find any attached files in your mail.", vbInformation, _
     "Finished!"
 End If

'Replenish Memory'
GetAttachments_exit:

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

  'function for sorting the excel attachment'

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
End Sub

【问题讨论】:

标签: vba pdf outlook


【解决方案1】:

欢迎使用 StackOverflow!

要回答您的具体问题,

我收到“尝试的操作失败。找不到对象。”错误在:
Set SubFolder = Mailbox.Folders("!Response File")

您收到此错误是因为“!响应文件”不在收件箱的父级中。按名称查找文件夹可能很棘手。 您可以改为通过 ID 访问该文件夹。 获取所需文件夹 ID 的一种方法是编写一个函数来执行此操作。

    Function GetInboxFolderID(FolderName As String) As String
    Dim nsp As Outlook.Folder
    Dim mpfSubFolder As Outlook.Folder
    Dim mpfSubFolder2 As Outlook.Folder
    Dim flds As Outlook.Folders
    Dim flds2 As Outlook.Folders

    Set nsp = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
    Set flds = nsp.Folders
    Set mpfSubFolder = flds.GetFirst
    Do While Not mpfSubFolder Is Nothing
        If mpfSubFolder.Name = FolderName Then
            GetInboxFolderID = mpfSubFolder.EntryID
            Exit Function
        End If
        Set flds2 = mpfSubFolder.Folders
        Set mpfSubFolder2 = flds2.GetFirst
        Do While Not mpfSubFolder2 Is Nothing
            If mpfSubFolder2.Name = FolderName Then
                GetInboxFolderID = mpfSubFolder2.EntryID
                Exit Function
            End If
            Set mpfSubFolder2 = flds2.GetNext
        Loop
        Set mpfSubFolder = flds.GetNext
    Loop
End Function

另外,这里有一个测试它的代码。

Sub testing()
Dim tv As String
tv = GetInboxFolderID("Response File")
  Set myNewFolder = Application.Session.GetFolderFromID(tv)
 myNewFolder.Display

End Sub 

此函数循环遍历您的主要用户文件夹集,然后检查每个文件夹中的文件夹名称中给出的字符串。如果函数找到它,则将 ID 返回到该文件夹​​。

测试子程序只是用于调试目的,当你运行它时,它应该打开你在函数中命名的文件夹,即“响应文件”

换行:

Set SubFolder = Mailbox.Folders("!Response File")

收件人:

Set SubFolder = Application.Session.GetFolderFromID(GetInboxFolderID("Response File"))

如果你实现了我的功能,应该可以解决你当前的错误。

此外,您可以使用 SendKeys 关闭“Okay”消息

Call AppActivate("Adobe Reader", True)
 DoEvents
 SendKeys "{Enter}"

希望这会有所帮助!

【讨论】:

  • 哇,JDB,感谢您的详细回复!我会试试你的建议(可能有点过头了),但我会试一试。非常感谢您的帮助!!
  • 我在需要放置这个函数例程的地方遇到了麻烦。同样,仍在学习 VBA。谢谢!
  • 在代码的底部,End Sub 行标志着子例程的结束。子例程和函数是两种主要的过程类型。要添加该功能,请将其添加到 End Sub 行之后。这会将其定义为单独的过程,并允许您单独调用它。让我知道这是否清楚,如果没有视觉辅助,可能很难解释。
  • 谢谢 - 我把函数例程放在最后(并注释掉 GoTo Error 以查看哪里出错了)。所以,我收到一个错误“无法打开该项目。再试一次。”在这一行:Set SubFolder = Application.Session.GetFolderFromID(GetFolderIDByName("!Response File")) 我需要在子程序的开头声明函数变量吗?对不起我的无知。谢谢。
  • 你试过我的测试子程序看看它是否正常工作吗?
猜你喜欢
  • 2020-03-10
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 2021-03-09
  • 1970-01-01
  • 2010-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多