【问题标题】:Is there a way to highlight failed cells in an excel VBA script?有没有办法在 excel VBA 脚本中突出显示失败的单元格?
【发布时间】:2023-03-27 10:35:02
【问题描述】:

有没有办法在 excel VBA 脚本中突出显示失败的单元格?它在 Outlook 中发送一封电子邮件,但是当它在 Outlook 通讯簿中的工作表中找不到相应的名称时,它只会跳过该行,并且手动检查不是一个真正的选项,因为我的 Excel 工作表已经包含超过 500 行。我能否以某种方式添加一行以突出显示地址不正确的这些行?

【问题讨论】:

  • 不分享您使用的代码,很难收到建议。否则,接收调整现有代码的建议并不复杂。我可以提供帮助,但仅限于这些情况......
  • 您的意思是要设置背景颜色,例如MyCell.Interior.Color = vbYellow? (假设 MyCell 是 VBA 处理中遇到的“失败”单元)。

标签: excel vba email outlook


【解决方案1】:

快速搜索给了我这个功能:

Function CheckAddressExists(eAddress As String) As Boolean
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olContactFolder As Outlook.MAPIFolder
Dim olContact As Outlook.ContactItem
Dim olItems As Outlook.Items
Dim bFound As Boolean, bStarted As Boolean
On Error Resume Next

Set olApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
    Set olApp = CreateObject("Outlook.Application")
    bStarted = True
End If

Set olNS = olApp.GetNamespace("MAPI")
Set olContactFolder = olNS.GetDefaultFolder(10)
Set olItems = olContactFolder.Items
For Each olContact In olItems
    bFound = False
    If eAddress = olContact.Email1Address Or _
       eAddress = olContact.Email2Address Or _
       eAddress = olContact.Email3Address Then
        bFound = True
        Exit For
    End If
    Err.Clear
Next olContact
If bFound Then
    CheckAddressExists = True
End If
CleanUp:
If bStarted = True Then
    olApp.Quit
End If
Set olItems = Nothing
Set olNS = Nothing
Set olContactFolder = Nothing
Set olContact = Nothing
End Function

假设在您发送邮件的循环中,您可以通过首先在此函数中运行它来检查地址是否存在,如果不存在,请突出显示该地址。

此代码未经我测试,因为我没有发送邮件的宏,如果您需要帮助将任何内容整合到您拥有的内容中,您需要分享您正在使用的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多