使用Excel文本框中的文本发送Outlook电子邮件 – 错误424:需要对象

我正在尝试使用文本框中的文本(我在Excel中将其命名为tx)作为正文发送电子邮件。

当我运行代码时,线上有一个错误:

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 

用您的文本框所在工作Sheet's namereplace工作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 

你可以使用CDO? 下面是我在Excel VBA中jointesting函数的一些快速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