Outlook VBA 在添加 Else 参数时忽略原始 If 参数

Outlook VBA ignores original If argument when Else argument is added

这是按预期工作的原始代码:

Dim ContactsFolder As Folder
Set ContactsFolder = Session.GetDefaultFolder(olFolderContacts)
Dim SearchContactName As String
Set myItem = Outlook.CreateItem(olMailItem)
Dim NewContactEmail As String
Dim NewMail As Outlook.MailItem
Set NewMail = Application.ActiveInspector.currentItem

' Ask user for inputs
SearchContactName = InputBox("What is the name of your contact you want to comment to on Trello?")

Dim Contact As ContactItem

'search contacts'
For Each Contact In ContactsFolder.Items
    If Contact.FullName = SearchContactName Then
        If Contact.IMAddress = "" Then 'add trello card link'
            Contact.IMAddress = InputBox("What is the Trello card e-mail of this contact?")
            Contact.Save
            myItem.To = Contact.IMAddress
        Else
            myItem.To = Contact.IMAddress
        End If
    End If
Next

myItem.Body = NewMail.Body
myItem.Subject = NewMail.Subject

myItem.Display

我这样做的目的是当它找到一个联系人时,它会查看它的 IMaddress 中是否有任何内容。如果它确实有 link,那么它将进入电子邮件的 "To:"。如果没有,outlook 将提示用户输入 link,保存联系信息并将 link 添加到电子邮件的 "To:"。正如我之前所说,此代码适用于 outlook 中的任何联系人,无论有无 IMaddress。

我 运行 遇到的麻烦是没有保存联系信息。这是我可以复制错误的精简代码:

Sub LeaveAComment()
Dim ContactsFolder As Folder
Set ContactsFolder = Session.GetDefaultFolder(olFolderContacts)
Dim SearchContactName As String
Set myItem = Outlook.CreateItem(olMailItem)
Dim NewContactEmail As String
Dim NewMail As Outlook.MailItem
Set NewMail = Application.ActiveInspector.currentItem

' Ask user for inputs
SearchContactName = InputBox("What is the name of your contact you want to comment to on Trello?")

Dim Contact As ContactItem

'search contacts'
For Each Contact In ContactsFolder.Items
    If Contact.FullName = SearchContactName Then
        If Contact.IMAddress = "" Then 'add trello card link'
            Contact.IMAddress = InputBox("What is the Trello card e-mail of this contact?")
            Contact.Save
            myItem.To = Contact.IMAddress
        Else
            myItem.To = Contact.IMAddress
        End If
    Else
        If MsgBox("Contact does not exist. Would you like to add one?", vbYesNo, "Confirm") = vbYes Then
            End
        Else
            End
        End If
    End If
Next

myItem.Body = NewMail.Body
myItem.Subject = NewMail.Subject

myItem.Display

当我添加

Else
        If MsgBox("Contact does not exist. Would you like to add one?", vbYesNo, "Confirm") = vbYes Then
            End
        Else
            End
        End If
End If

那我运行成问题了。现在当我 运行 宏时,无论联系人是否已经存在,消息框都会触发。由于某种原因,原始的 If Contact.FullName = SearchContactName 被 Else 覆盖,即使不应触发 Else(至少在我的思考过程中)。我试过颠倒逻辑并做:

For Each Contact In ContactsFolder.Items
    If Contact.FullName <> SearchContactName Then

但这带来了同样的结果。

有什么想法吗?

您不能将以下代码部分放入每个循环中:

Else
    If MsgBox("Contact does not exist. Would you like to add one?", vbYesNo, "Confirm") = vbYes Then
        End
    Else
        End
    End If
End If

假设您有 100 个联系人并且想要找到一个名为 "Charlie" 的联系人。上面的部分代码给出了消息框,说联系人不存在,只检查了100个联系人中的1个。

您必须将消息框放在循环之外。您可以使用布尔值来检查是否找到 "Charlie" 个联系人:

Dim contactFound as boolean
For Each Contact In ContactsFolder.Items
   If Contact.FullName = SearchContactName Then
      contactFound = true

然后就在循环之后:

  If contactFound = false then
      If MsgBox("Contact does not exist. Would you like to add one?", vbYesNo, "Confirm") = vbYes Then
         End
      Else
         End
      End If
  End if