从 Excel VBA 创建新电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28526735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 06:14:24  来源:igfitidea点击:

New email create from Excel VBA

excelexcel-vbavba

提问by Hewage

I have email register in Excel. I need to create Command Button to open new email with last row details. i.e. last row, column C , column F . Please note there are hundreds of rows are existing with previously sent emails. How to write codes for that Command Button.

我在 Excel 中注册了电子邮件。我需要创建命令按钮来打开带有最后一行详细信息的新电子邮件。即最后一行,C 列,F 列。请注意,之前发送的电子邮件有数百行。如何为该命令按钮编写代码。

Thanks in advance

提前致谢

Hewage

海威奇

回答by Hewage

*It works with below

*它适用于以下

Private Sub CommandButton22_Click()
    Dim a As Integer
    Dim objOutlook As Object
    Dim objMail As Object
    Dim rngTo As Range
    Dim rngSubject As Range
    Dim rngBody As Range
    Dim rngAttach As Range

    Set objOutlook = CreateObject("Outlook.Application")
    Set objMail = objOutlook.CreateItem(0)

    a = ActiveCell.Row

    With ActiveSheet
        Set rngTo = .Cells(a, "C")
        Set rngSubject = .Cells(a, "E")
        'Set rngBody = .Range("B3")
        'Set rngAttach = .Range("B4")
    End With

    With objMail
        .To = rngTo.Value
        .Subject = rngSubject.Value
        '.Body = rngBody.Value
       '.Attachments.Add rngAttach.Value
        .Display 'Instead of .Display, you can use .Send to send the email _
                    or .Save to save a copy in the drafts folder
    End With

    Set objOutlook = Nothing
    Set objMail = Nothing
    Set rngTo = Nothing
    Set rngSubject = Nothing
    Set rngBody = Nothing
    Set rngAttach = Nothing
End Sub

Regards, Hewage

问候, Hewage

回答by Simon

set wbNew = application.workbooks.add
set wsNew = wbNew.sheets(1)

thisworkbook.rows(intLastRow).copy wsNew.rows(1)
wbnew.sendmail(stRecipients)

Something like that?

类似的东西?

Or are you saying the last row contains the details you want to email? "sendmail" gives you ability to specify recipients and subject, but you might wanna look at Outlook object instead (add through Tools/References) then you can do something like:

或者您是说最后一行包含您要通过电子邮件发送的详细信息?“sendmail”使您能够指定收件人和主题,但您可能想查看 Outlook 对象(通过工具/参考添加),然后您可以执行以下操作:

Set apOutlook = CreateObject("Outlook.Application")
apOutlook.Session.Logon

Set itEmail = apOutlook.CreateItem(olMailItem)

With itEmail
    .To = stRecipient
    .Subject = stSubject
    .Body = stBody
    .Display
End With

i.e. adds ability to specify "body" and will pop up an Outlook email object rather than the unsightly "An application is trying to send an email on your behalf" window that SendMail does...

即添加了指定“正文”的能力,并将弹出一个 Outlook 电子邮件对象,而不是 SendMail 所做的难看的“应用程序正在尝试代表您发送电子邮件”窗口...