【问题标题】:Outlook VBA ignores original If argument when Else argument is added添加 Else 参数时,Outlook VBA 会忽略原始 If 参数
【发布时间】:2016-04-07 10:17:49
【问题描述】:

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

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 中是否有任何内容。如果它确实有一个链接,那么它将进入电子邮件的“收件人:”。如果没有,Outlook 将提示用户输入链接,保存联系信息并将链接添加到电子邮件的“收件人:”。就像我之前所说的,此代码适用于 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

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

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

但这带来了同样的结果。

有什么想法吗?

【问题讨论】:

    标签: vba if-statement outlook contacts


    【解决方案1】:

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

    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 个后联系人不存在。

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-24
      • 2018-02-19
      • 1970-01-01
      • 2021-07-31
      • 2019-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多