2017-03-17 48 views
0

我试图使用文本框中的文本(我将其命名为tx在Excel中)作为正文发送电子邮件。使用Excel文本框中的文本发送Outlook电子邮件 - 错误424:所需的对象

当我运行的代码,有上线的错误:

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

'tx'没有在你的子类中定义,它是一个公共变量吗?什么似乎是问题?请解释什么不如预期! – R3uK

+0

@ R3uK我认为这是工作表上的文本框的名称 – CallumDA

+0

这里有什么问题?当你运行这个程序时,你发送一封空白的电子邮件,是否出错,它什么都不做?如果您的安全设置限制了它,使用'.Send'可能会导致一些问题 - 使用'.Display'替换它以查看哪些内容不工作。 – CLR

回答

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 
+0

对不起,我没有看到你的答案。这里有一个错误:strbody = ThisWorkBook.Sheets(“Sheet's name”)。Shapes(“tx”).ControlFormat.Value(Error 438:Object does not support this property or method) – Francis

+1

@Francis:Ok,通过'DrawingObject.Text'控制'ControlFormat.Value' – R3uK

+0

哇,你很好。 Merci beaucoup! – Francis

0

你可以使用CDO?下面是一些快速的VBA代码,我把在Excel 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 = "[email protected]" 
     .CC = "" 
     .BCC = "" 
     .From = "[email protected]" 
     .Subject = "Test Email" 
     .TextBody = strbody 
     .Send 
    End With 

End Sub 
+1

这只是不回答这个问题...阅读评论请 – R3uK

+0

谢谢。但我认为我的问题在别处。 – Francis

+0

对不起。我误解了“但它不是正确的做法”,因为“我不想引用Outlook库来发送外发电子邮件”,而不是“这段代码是我想使用的,但它不起作用”。 – Kevin

相关问题