创建字典。无效使用Me关键字时出错

时间:2016-11-02 00:51:12

标签: vba excel-vba excel

这是我的代码。我的列C具有重复的名称,列B具有唯一的ID我需要找到哪些唯一ID与名称匹配,并向名称发送电子邮件并将唯一ID粘贴到电子邮件中。我在第一个Me.Cells上收到错误。

Sub sendEmails()

Dim dict_emails As Scripting.dictionary
Set dict_emails = New Scripting.dictionary
Dim objOutlook As Object
Dim objMailMessage As Object
Set objOutlook = CreateObject("Outlook.Application")
Dim row As Range
Dim table As ListObject
Dim row_index As Long
Dim strEmail As String
Dim strExeptionID As String

ActiveWorkbook.Sheets("New 0-30").Select

Set table = ActiveSheet.ListObjects("New_030_Table")

For row_index = 1 To table.DataBodyRange.Rows.Count
    strEmail = table.DataBodyRange(row_index, 3).Value
    strExceptionID = table.DataBodyRange(row_index, 2).Value

    If Not dict_emails.Exists(strEmail) Then
        ' first time we have seen this name
        dict_emails.Add strEmail, strExceptionID
    Else
        dict_emails(strEmail) = dict_emails(strEmail) & vbCrLf & strExceptionID
    End If
Next

Dim var_key As Variant
For Each var_key In dict_emails.Keys
Set objMailMessage = objOutlook.CreateItem(0) ' create new mail
    With objMailMessage
        .To = "" & var_key
        .CC = ""
        .BCC = ""
        .Subject = "Exceptions Set to Expire in Less Than 30 Days"
        .Body = "You have the following exceptions set to expire: " & vbCrLf & dict_emails(var_key)
        .Save ' save as draft
    End With
Next

End Sub

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题而且我已经解决了! 但任何人都不会相信参考列表" Microsoft Scripting Runtime"优先考虑前三名。它将开始工作,错误将消失。

相关问题