和号(&)错误与.Find

时间:2015-09-09 16:07:33

标签: vba outlook ampersand

我创建了一个程序,可以在我的Outlook电子邮件中搜索特定主题行。它工作得很好,但每当我要搜索的一个字符串包含“&”时,我都会收到错误在它的象征。以下是我正在使用的代码示例。

Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim olFldr As MAPIFolder
Dim olMi As MailItem
Dim olItms As Items

Dim subj As String

Set olApp = GetObject(, "Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
Set olFldr = olNs.GetDefaultFolder(olFolderInbox)
Set olItms = olFldr.Items

subj = "Life&Health Insurance"

Set olMi = olItms.Find("[Subject] = " & subj) 

当遇到与&符号的任何内容时,我得到“条件无效”错误。请帮忙。

1 个答案:

答案 0 :(得分:0)

尝试使用Chr(38)语句声明一个字符串变量:

Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim olFldr As MAPIFolder
Dim olMi As MailItem
Dim olItms As Items
Dim subj As String

Set olApp = GetObject(, "Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
Set olFldr = olNs.GetDefaultFolder(olFolderInbox)
Set olItms = olFldr.Items
Set subj = "Life" & Chr(38) & "Health Insurance"

Set olMi = olItms.Find("[Subject] = " & Chr(34) & subj & Chr(34)) 

此外,在过滤文本字段时,您可以使用一对单引号(')或一对双引号(")来分隔属于过滤器的值。请参阅MSDN中描述的Items类的Find方法。