【问题标题】:Moving Emails in Outlook Folders to Subfolder with VBA according to Datasets in Excel根据 Excel 中的数据集使用 VBA 将 Outlook 文件夹中的电子邮件移动到子文件夹
【发布时间】:2020-05-28 19:01:32
【问题描述】:

我发现了一个与我在这里类似的问题来解决这个问题;

How can I move Mails Items from Outlook Inbox with specific subject to specific folder/sub folder?

第一个模块- 此代码的第一部分 - 我已成功将所有电子邮件数据导出到电子表格。

第二个模块- 我想指示 Excel VBA 根据我在电子表格中输入的数据集将主文件夹中的电子邮件移动到子文件夹(它不会基于电子邮件本身的过滤器/标准,只是其独特的主题标题)。

在 (c) 列中,是电子邮件的主题(所有主题标题都是特定的/唯一的),在 (h) 列中,我已经详细说明了我想要的子文件夹的名称也动了。不幸的是,我在执行我创建的代码时出错了。

我是 Excel VBA 的初学者,没有最好的理解。我根据不同的来源对我的代码有所了解,如果不正确请告诉我,我们将不胜感激

谢谢。

  • 我尝试执行我在用户提出的问题中找到的与我类似的代码,但它没有工作

Sub MovingEmails_Invoices()

  'Declare your Variables
    Dim items As Outlook.items
    Dim subfolder As Outlook.Folder 'this will be the folder you want to move the Mail to

    'Set Outlook Inbox Reference
    Set OP = New Outlook.Application
    Set NS = OP.GetNamespace("MAPI")
    Set Mail = OP.CreateItem(olMailItem)

    'To loop through subfolder and its folders
    Set rootfol = NS.Folders("SYNTHES-JNJCZ-GBS.DE.AT.CH@ITS.JNJ.com")
    Set Folder = rootfol.Folders("Austria")


'The list for invoice number should be dynamic
Dim arraysearch(1 To 1000) As String
Dim i As Long
i = UBound(arraysearch)
arraysearch(i) = Range("C2").offset(i, 0).Value
If i = 0 Then
MsgBox "error"
Exit Sub
End If


'The list for folder type should be dynamic
Dim arraymove(1 To 1000) As String
i = UBound(arraymove)
arraymove(i) = Range("H2").offset(i, 0).Value
If i = 0 Then
MsgBox "error"
Exit Sub
End If

'specific folders for the mail to move to
Set subfolder = rootfol.Folders(arraymove(i))


For Each Mail In Folder.items.Restrict("[Subject] >= arraysearch(i)")

   If arraysearch(i) = arraymove(i) Then

   item.Move subfolder

   End If

   Next Mail

End Sub

【问题讨论】:

  • 光标在代码中,F8直到出现错误。编辑问题以识别该行。
  • 哦,对了,它停在 ;设置 item = items.Find(FilterText)
  • 相关信息应编辑到问题中。
  • 好的,谢谢!
  • 您今天添加的新代码没有任何意义。您创建一个具有 1000 个维度的数组,然后您只在这一千个维度上分配一个值。很难说你期望这会做什么。我认为您缺少迭代器“i”的循环。

标签: excel vba outlook


【解决方案1】:

在代码中,您将遍历文件夹中的所有项目:

  'Loop through the Items in the folder backwards
     'Setting Mail to counting backwards
    For lngCount = items.Count To 1 Step -1
    'setting object as Email item
        Set item = items.item(lngCount)

这确实不是一个好主意。特别是如果您以后使用Find 方法。

如果您需要查找符合您的条件的项目,我建议您使用 Items 类的 Find/FindNextRestrict 方法。在以下文章中详细了解这些方法:

另外,您可能会发现 Application 类的 AdvancedSearch 方法很有帮助。在 Outlook 中使用 AdvancedSearch 方法的主要好处是:

  • 搜索在另一个线程中执行。您无需手动运行另一个线程,因为 AdvancedSearch 方法会在后台自动运行它。
  • 可以在任何位置(即超出某个文件夹的范围)搜索任何项目类型:邮件、约会、日历、便笺等。 RestrictFind/FindNext 方法可以应用于特定的 Items 集合(请参阅 Outlook 中 Folder 类的 Items 属性)。
  • 完全支持 DASL 查询(自定义属性也可用于搜索)。您可以在 MSDN 中的 Filtering 文章中阅读有关此内容的更多信息。为了提高搜索性能,如果为商店启用了即时搜索,则可以使用即时搜索关键字(请参阅Store 类的IsInstantSearchEnabled 属性)。
  • 您可以随时使用Search 类的Stop 方法停止搜索过程。

【讨论】:

  • 您好,非常感谢您的建议,我确实研究了这两个选项,restrict 甚至 DASL 查询,但是对于我的代码,我使用了 restrict 方法。我现在重写了我的代码,并使用数组作为附加步骤,以使其更简洁。我的斗争是将电子邮件的“主题”与我也需要移动的文件夹类型相匹配。无论如何,你能不能帮帮我。我的新代码也有错误,调试向我展示了这段代码; set subfolder =rootfol.folders(arraymove(i))
【解决方案2】:

无需尝试查找该项目。

它已经被Set item = items.item(lngCount)标识了。

您可以检查主题,看看它是否是您想要的项目。

'Find Email using Subject found on Column C
'Set item = items.Find(FilterText)

'If the object is an Email
If item.Class = olMail Then

    If item.Subject = FilterText Then 

        'Find item under the main Folder subfolders
        Set subfolder = Folder.Folders(FolderMove)

        'Mark Item as Read
        item.UnRead = False

        'Move Item to folder type in Outlook
        item.Move subfolder
    End If

End If

【讨论】:

  • 你好!感谢您的评论,现在我决定整个 find 方法可能不是执行此功能的最佳方法。所以我重写了它并决定使用数组。不幸的是,我的新代码仍然存在错误; Set subfolder = rootfol.Folders(arraymove(i))如果您有任何建议做得更好,我将不胜感激,谢谢!
猜你喜欢
  • 2019-12-24
  • 1970-01-01
  • 2017-05-20
  • 1970-01-01
  • 2016-02-05
  • 2016-08-03
  • 1970-01-01
  • 2019-09-21
  • 1970-01-01
相关资源
最近更新 更多