Microsoft VBScript运行时错误:类型不匹配:'cells'

时间:2014-06-24 08:14:58

标签: excel visual-studio vbscript cdo.message

我正在尝试修改VBS脚本,以便在excel文件的日期到达其到期日期时发送电子邮件。

长话短说,我们有一份包含到期日期,指定客户经理和电子邮件地址的服务合同的文件。

我需要做的是创建一个脚本,该脚本定期逐行读取文件,并在合同到期时将电子邮件发送到相应的地址。

我找到了一个可以通过CDO执行此操作的脚本,但我无法发送带有过期日期的行。

我当前的问题是,当我尝试运行vbs时,我得到:script.vbs(19,9)Microsoft VBScript运行时错误:类型不匹配:'cells'。

请帮助:)

    Set objExcel = CreateObject("Excel.Application")
Function getEmail()
    Dim iCol, iRow
    Dim sEmailBody
    Dim sEmailTo ' the recipient
    iCol = 1 ' column A
    iRow = 1 ' row 2
    Do
        sEmailTo = cells(irow, 1).text
        sEmailBody = sendData(iRow)
        iRow = iRow + 1
    Loop While Not Len(Trim(Cells(iRow, iCol))) = 0
End Function

Function sendData(ByVal iRow) 

    Dim iCol
    For iCol = 1 To 3 ' B=2, K=11
        sendData = sendData & vbCrLf & Cells(iRow, iCol).Text 
    Next
    MsgBox sendData
End Function

Set objExcel = CreateObject("Excel.Application")
Set objEmail = CreateObject("CDO.Message")
set objConf = CreateObject("CDO.Configuration")
Set objWorkbook = objExcel.Workbooks.Open _
   ("H:\CS\Contracte_suport_2014-06-13.xls")
Set objFlds = objConf.Fields
With objFlds
  .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "91.195.144.206"
  .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
  .Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
  .Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
  '.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoAnonymous
  .Update
End With

Set objEmail.Configuration = objConf
x = 2
Do Until objExcel.Cells(x, 2).Value = ""
    Set objEmail.Configuration = objConf

        objEmail.From = "in@datanets.ro"
    objEmail.To = objExcel.Cells(x, 2)
    objEmail.Subject = "Contracte Support - Notificare Expirare"
    objEmail.Textbody = sendData(2)
    objEmail.Send
    x = x + 1
    If Err Then
         WScript.Echo "SendMail Failed:" & Err.Description
    End If
Loop

objExcel.Quit
objExcel.DisplayAlerts = False

1 个答案:

答案 0 :(得分:0)

这是你的罪魁祸首:

Function sendData(ByVal iRow) 

    Dim iCol
    For iCol = 1 To 3 ' B=2, K=11
        sendData = sendData & vbCrLf & Cells(iRow, iCol).Text
    Next
    MsgBox sendData
End Function

在VBScript中,如果没有句柄,则无法访问Cells集合之类的Excel / VBA对象。改变这个:

sendData = sendData & vbCrLf & Cells(iRow, iCol).Text

进入这个:

sendData = sendData & vbCrLf & objExcel.Cells(iRow, iCol).Text

并且错误应该消失。