0

我终于得到了一些工作代码,但是一旦 Module2 函数完成,我就会收到“类型不匹配”错误,我不知道为什么。

如果我单步执行它,它会单步执行 module2 上的“结束功能”,然后我会收到类型不匹配,但它确实会发送电子邮件。任何帮助都会很棒

此 VBA 代码分为 3 个部分。

1个子

  Sub Workbook_open()

  Call Module1.GetData

  End Sub

2 模块 1

  Public EmailAddress As String
  Public CompanyNumber As String
  Public Name As String
  Public GroupComp As String

  Function GetData()

  Dim LastRow As String
  Dim rng As Range


  LastRow = Cells(Rows.Count, "K").End(xlUp).Row

  For Each rng In Range("K2:K" + LastRow)

       If Not rng.Value = vbNullString Then
           Select Case rng.Value
               Case 1
                  Case Is = "True"
                    Let EmailAddress = ActiveCell.Offset(0, -5).Value
                    Let CompanyNumber = ActiveCell.Offset(0, -9).Value
                    Let Name = ActiveCell.Offset(0, -8).Value
                    Let GroupComp = ActiveCell.Offset(0, -7).Value
                    Call Module2.Email(EmailAddress, CompanyNumber, Name, GroupsComp)
               Case 2
                  Case Is = "False"
           End Select

        End If
    Next
   End Function

3 模块 2

      Function Email()
     'MsgBox (EmailAddress)
     Set objMessage = CreateObject("CDO.Message")
     objMessage.Subject = "Stuffl " & (GroupComp)
     objMessage.From = "Department Name(Department@Email.com)"
     objMessage.Cc = "Department Name(Department@Email.com)"
     objMessage.To = (EmailAddress)
     MsgBox (EmailAddress)
     objMessage.TextBody = "TEST"

      objMessage.Configuration.Fields.Item _
      ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

      objMessage.Configuration.Fields.Item _
      ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "x.x.x.x"

      objMessage.Configuration.Fields.Item _
      ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

      objMessage.Configuration.Fields.Update

      objMessage.Send

    End Function
4

2 回答 2

1

Function Email()改成_

Function Email(emailaddress As String, companynumber As String, name As String, groupscomp As String)

你实际上并没有调用 Mail 函数,这就是我认为的问题

于 2013-09-27T12:38:49.893 回答
1

“我不明白为什么我必须在邮件中再次指定变量”

每当您将值传递给 Sub 或 Function 时,您必须定义该 Sub 或 Function 以便它期望将值传递给它。所以这行不通:

Sub Foo()
    Dim i as Integer
    i = 5
    Call Bar(i)
End Sub

Sub Bar()
    i = i + 2
End Sub

因为 Bar() 不希望有任何东西传递给它。这将起作用:

Sub Foo()
    Dim i as Integer
    i = 5
    Call Bar(i)
End Sub

Sub Bar(i as Integer)
    i = i + 2
End Sub

因为您现在已经告诉 Bar 期望将一个整数传递给它。

希望有帮助。

于 2013-09-27T13:07:06.140 回答