我的Excel宏需要以当前工作簿作为附件来启动新电子邮件。 (所有用户都有Outlook。)用户可以修改电子邮件中的任何内容,然后单击“发送”。
我在网上发现了两个不同的例子,但其中一个代码涉及的代码比另一个代码多得多。更简单的版本似乎有效,但我想知道是否有其他原因的复杂性。
简短版本:
Dim strRecipient, strSubject As String, booReturnReceipt As Boolean
strRecipient ="dummyEmail@example.com"
strSubject = "Enter Subject Here"
booReturnReceipt = True
Application.Dialogs(xlDialogSendMail).Show Arg1:=strRecipient, _
Arg2:=strSubject, Arg3:=booReturnReceipt
长版:
Sub Mail_ActiveSheet()
'Working in Excel 2000-2016
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Destwb As Workbook
Dim OutApp As Object
Dim OutMail As Object
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Destwb = ActiveWorkbook
'Determine the Excel version and file extension/format
With Destwb
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007-2016
Select Case Sourcewb.FileFormat
Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
Case 52:
If .HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Case 56: FileExtStr = ".xls": FileFormatNum = 56
Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
End Select
End If
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With Destwb
On Error Resume Next
With OutMail
.to = "dummyEmail@example.com"
.Subject = "Enter Subject Here"
.Body = "Hi there"
.Attachments.Add Destwb.FullName
.Display
End With
On Error GoTo 0
.Close savechanges:=False
End With
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
使用CreateObject(“Outlook.Application”)真的有优势吗? 附加文件类型时真的有用吗?
感谢。
编辑:基于前两个答案,不能清楚短版本的功能与长版本相同:不只是发送电子邮件,它打开撰写Outlook的电子邮件窗口(所有用户都有),已附加活动工作簿。在按“发送”之前,用户可以编辑,添加或更改收件人等。
答案 0 :(得分:1)
附加文件类型时真的有用吗?
除非您附加 Exe 文件,否则我不知道。
使用CreateObject(“Outlook.Application”)
确实有优势
对我来说,是。它易于使用(是迟到或早期绑定)。它是MS Office的一部分(取决于您采用的包)。大多数人都拥有它。
您的长版本可减少约50-60%。以下是使用延迟绑定的示例(未经测试)。
Sub Sample()
Dim OutApp As Object, OutMail As Object
Dim Destwb As Workbook
Set Destwb = ActiveWorkbook
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.to = "dummyEmail@example.com"
.Subject = "Enter Subject Here"
.Body = "Hi there"
.Attachments.Add Destwb.FullName
.Display ' .Send
End With
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
如果真的很重要,你可以减少几行:)
Sub Sample()
Dim OutApp As Object, OutMail As Object
Dim Destwb As Workbook
Set Destwb = ActiveWorkbook
With Application
.ScreenUpdating = False: .EnableEvents = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.to = "dummyEmail@example.com"
.Subject = "Enter Subject Here"
.Body = "Hi there"
.Attachments.Add Destwb.FullName
.Display ' .Send
End With
Set OutMail = Nothing: Set OutApp = Nothing
With Application
.ScreenUpdating = True: .EnableEvents = True
End With
End Sub
答案 1 :(得分:0)
Dim strRecipient, strSubject As String, booReturnReceipt As Boolean
这未被正确宣布。现在strRecipient被声明为变种。正确的方法是Dim strRecipient as String, strSubject as String, ...
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
这些行加快了程序。
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
Else
这些行表示此过程已准备好处理任何版本的Excel。此代码更易于移植。
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007-2016
Select Case Sourcewb.FileFormat
Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
Case 52:
If .HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Case 56: FileExtStr = ".xls": FileFormatNum = 56
Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
End Select
End If
如果在Excel中打开非Excel文件,则此代码非常有用 - 例如,如果使用Excel在分隔或固定宽度的文本文件上执行文本到列。通过为非Excel文件附加.xlsb
,可以保留通过在Excel中操作文件而添加的Excel功能。
使用CreateObject确实有优势(&#34; Outlook.Application&#34;)?
解释Sidd已经说过的话,可以。我知道当您使用Gmail的基本Send Mail
提示时,您会获得一个更加精简的应用程序。我不确定Outlook是否也是如此,但如果是这样,这将允许您使用功能齐全的Outlook进行邮件。例如,我不确定Send Mail
提示是否包含签名。
With OutMail
.to = "dummyEmail@example.com"
.Subject = "Enter Subject Here"
.Body = "Hi there"
.Attachments.Add Destwb.FullName
.Display
End With
最后,此方法允许您Send Mail
提示功能不会编辑电子邮件的正文等。
很抱歉格式不一致,StackExchange编辑器对于我想要完成的工作有点挑剔。
答案 2 :(得分:0)
我刚刚发现了一个显着的差异。短版本不允许您自动在邮件正文中放置任何内容(主要是表单字母的点)。
对于某些消息 - 例如,收件人每周都要求的报告 - 一个冗长的主题行就足够了。