【发布时间】:2016-10-26 16:29:02
【问题描述】:
基本上,我在 Excel 电子表格中填充了 5000 个字符串的列表。我希望 VBA 浏览 Outlook 收件箱中的附件,如果找到字符串匹配,我希望标记特定的电子邮件。这是我到目前为止的代码
Sub attachsearch()
On Error GoTo bigerror
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
Dim varresponse As VbMsgBoxResult
Dim workbk As Workbook
Dim SearchString As String
Set ns = GetNamespace("MAPI")
Set inbox = ns.GetDefaultFolder(olFolderInbox)
Set subfolder = inbox.Folders("test")
Set workbk = Workbooks.Open("C:\Users\John.Doe\Desktop\10 25 2016 Pricing Team Macro.xlsm")
i = 0
If subfolder.Items.Count = 0 Then
MsgBox "There are no emails to look at. Please stop wasting my time.", vbInformation, "Folder is Empty"
Exit Sub
End If
For Each item In subfolder.Items
For Each atmt In item.Attachments
For rwindex = 1 To 5000
SearchString = Workbooks("10 25 2016 Pricing Team Macro").Sheets("NDC Sort").Cells(rwindex, 1).Value
下面是问题代码,这里没有正确使用index proberty,但是我不确定用什么。我知道 Microsoft 会为附件中的单词编制索引,因为当我在 Outlook 中手动输入搜索字符串时,即使该字符串仅存在于附件中,它也会提取电子邮件。所以最终,我的问题是,如何在 VBA 中利用该附件索引?
If atmt.Index Like "*" & Workbooks("10 25 2016 Pricing Team Macro").Sheets("NDC Sort").Cells(rwindex, 1).Value & "*" Then
i = i + 1
With item
.FlagRequest = "Follow up"
.Save
End With
End If
Next rwindex
Next atmt
Next item
If i > 0 Then
MsgBox "I found " & i & " attached files with a specific name."
Else
MsgBox "I didn't find any files"
End If
Set atmt = Nothing
Set item = Nothing
Set ns = Nothing
workbk.Close savechanges:=False
Exit Sub
bigerror:
MsgBox "something went wrong"
End Sub
任何帮助将不胜感激,在此先感谢!
【问题讨论】:
-
你在搜索什么?附件名称或附件文件中的字符串?
-
附件文件内
-
为了在附件中搜索,您必须先保存它,它们是什么类型的文件?电子表格?单词?
-
好的。我现在正在编写它来为 xls、pdf 和 doc 文件运行不同的进程。完成后我会发布该代码,因为这可能是最好的答案。如果其他人有见识,我将不胜感激。