我的VBA代码中的错误在哪里?

时间:2017-04-23 18:21:14

标签: vba excel-vba excel

我写了这个脚本来请求来自不同团队的名单,但代码不起作用..需要帮助来检查我错在哪里。

Sub RosterRequest()
Dim counter As Integer
Dim outapp As Object
Dim outmail As Object

Set outapp = CreateObject("Outlook.Application")
Set outmail = outapp.CreateItem(0)

Do While counter < 5
counter = counter + 1
c = Cells(counter, 1).Value
d = Cells(counter, 2).Value

    MsgBox ("loop number " & counter & " Name is " & c & " Email is " & d)
    With outmail
        .to = d
        .Subject = "Roster Request"
        .Body = "Hi " & c & " Please Share the roster for your team "
        .send
    End With

Loop

End Sub

2 个答案:

答案 0 :(得分:0)

  1. 检查是否已包含Microsoft Outlook 12.0对象库
  2. 在VBA编辑器中&gt;转到工具&gt;参考文献&gt;并选择Microsoft Outlook 12.0对象库

    1. 而不是使用c = Cells(counter,1).Value和d = Cells(counter,2)。值,尝试:
    2. 一个。 c = ThisWorkbook.Sheets(&#34; Shee1&#34;)。Cells(counter,1).Value

      湾d = ThisWorkbook.Sheets(&#34; Shee1&#34;)。Cells(counter,2).Value

      1. 在Do While Loop之前尝试counter = 0:
      2. counter = 0

        Do while counter&lt; 5

        ... ..

        1. 检查Outlook是否已配置。
        2. 一个。您已登录。

          湾要通过代码发送邮件,Outlook需要防病毒软件,请检查是否有防病毒软件。

          我。要检查转到文件&gt;选项&gt;信托中心&gt;信任中心设置&gt;编程访问并查找防病毒状态

答案 1 :(得分:0)

将createItem(0)指令放在do while循环中,如下所示

Do While counter < 5
 Set outmail = outapp.CreateItem(0)
counter = counter + 1
c = Cells(counter, 1).Value
d = Cells(counter, 2).Value

    MsgBox ("loop number " & counter & " Name is " & c & " Email is " & d)
    With outmail
        .to = d
        .Subject = "Roster Request"
        .Body = "Hi " & c & " Please Share the roster for your team "
        .send
    End With

Loop