【发布时间】: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