我終於有一些工作代碼,但我得到一個「類型不匹配」的錯誤,一旦Module2函數完成,我不知道爲什麼。類型不匹配VBA不能找到
如果我經過它,它逐步通過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([email protected])"
objMessage.Cc = "Department Name([email protected])"
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
在'Module1.GetData'中,您使用4個參數調用'Module2.Email',但是在它的定義中沒有參數?另外,爲什麼使用'Function'而不是'Sub'呢?你定義的函數會返回'Variant'對象,這是必要的嗎? – Ioannis
'函數電子郵件()'爲什麼它的函數,如果它不返回任何東西?看看這個[link](http://stackoverflow.com/questions/16964917/send-an-email-when-workbook-is-saved/16965103#16965103) – Santosh