我正在尝试使用文本框中的文本(我在Excel中将其命名为tx)作为正文发送包含Outlook的电子邮件。
当我运行代码时,行上有一个错误:
strbody = tx.Text
错误424:需要对象
Sub SendMail()
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
strbody = tx.Text
'On Error Resume Next
With OutMail
.To = "..."
.CC = ""
.BCC = ""
.Subject = Cells(3, 2)
.Body = strbody
.Send
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
答案 0 :(得分:1)
将Sheet's name
替换为文本框所在的工作表的名称
在strbody = ThisWorkBook.Sheets("Sheet's name").Shapes("tx").ControlFormat.Value
Sub SendMail()
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
strbody = ThisWorkBook.Sheets("Sheet's name").Shapes("tx").ControlFormat.Value
'On Error Resume Next
With OutMail
.To = "..."
.CC = ""
.BCC = ""
.Subject = Cells(3, 2)
.Body = strbody
.Send
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
答案 1 :(得分:0)
你可以使用CDO吗?以下是我在Excel VBA中测试函数中添加的一些快速VBA代码(电子邮件地址和SMTP服务器地址已编辑):
Sub test()
Dim strbody As String
strbody = "Test Email" & vbNewLine & vbNewLine & "TEST EMAIL"
Dim iMsg As Object
Set iMsg = CreateObject("CDO.Message")
With iMsg
.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") _
= "whateverYourSMTPServerIs"
.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") _
= 25
.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") _
= 2 'Stands for sending using CDO
.Configuration.Fields.Update
.To = "someemail@someplace.com"
.CC = ""
.BCC = ""
.From = "someemail@someplace.com"
.Subject = "Test Email"
.TextBody = strbody
.Send
End With
End Sub