Excel 需要使用CDO邮件以XLSX格式发送附件
Hi Iam使用以下代码作为示例,通过SMTP发送带有附件的邮件,但它发送的附件是XLSM格式的,我需要XLSX(非宏)格式。请帮我做这个Excel 需要使用CDO邮件以XLSX格式发送附件,excel,excel-2010,cdo.message,vba,Excel,Excel 2010,Cdo.message,Vba,Hi Iam使用以下代码作为示例,通过SMTP发送带有附件的邮件,但它发送的附件是XLSM格式的,我需要XLSX(非宏)格式。请帮我做这个 Option Explicit 'This procedure will mail the whole workbook 'You can 't send a Workbook that is open with CDO. 'That's why it use SaveCopyAs to save it with another name and send
Option Explicit
'This procedure will mail the whole workbook
'You can 't send a Workbook that is open with CDO.
'That's why it use SaveCopyAs to save it with another name and send that file.
Sub CDO_Mail_Workbook()
'Working in 2000-2007
Dim wb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim FileExtStr As String
Dim iMsg As Object
Dim iConf As Object
' Dim Flds As Variant
Set wb = ActiveWorkbook
If Val(Application.Version) >= 12 Then
If wb.FileFormat = 51 And wb.HasVBProject = True Then
MsgBox "There is VBA code in this xlsx file, there will be no VBA code in the file you send." & vbNewLine & _
"Save the file first as xlsm and then try the macro again.", vbInformation
Exit Sub
End If
End If
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Make a copy of the file/Mail it/Delete it
'If you want to change the file name then change only TempFileName
TempFilePath = Environ$("temp") & "\"
TempFileName = "Copy of " & wb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
FileExtStr = "." & LCase(Right(wb.Name, Len(wb.Name) - InStrRev(wb.Name, ".", , 1)))
wb.SaveCopyAs TempFilePath & TempFileName & FileExtStr
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
' iConf.Load -1 ' CDO Source Defaults
' Set Flds = iConf.Fields
' With Flds
' .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
' .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "Fill in your SMTP server here"
' .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
' .Update
' End With
With iMsg
Set .Configuration = iConf
.To = "ron@debruin.nl"
.CC = ""
.BCC = ""
.From = """Ron"" <ron@something.nl>"
.Subject = "This is a test"
.TextBody = "This is the body text"
.AddAttachment TempFilePath & TempFileName & FileExtStr
.Send
End With
'If you not want to delete the file you send delete this line
Kill TempFilePath & TempFileName & FileExtStr
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
选项显式
'此过程将邮寄整个工作簿
'无法发送使用CDO打开的工作簿。
'这就是为什么它使用SaveCopyAs以另一个名称保存它并发送该文件。
子CDO_邮件_工作簿()
“2000-2007年工作
将wb设置为工作簿
Dim TempFilePath作为字符串
将文件名设置为字符串
Dim FileExtStr作为字符串
作为对象的Dim iMsg
作为对象的Dim-iConf
“Dim FLD作为变体
设置wb=ActiveWorkbook
如果Val(Application.Version)>=12,则
如果wb.FileFormat=51且wb.HasVBProject=True,则
MsgBox“此xlsx文件中有VBA代码,您发送的文件中将没有VBA代码。”&vbNewLine&_
“请先将文件另存为xlsm,然后重试宏。”,vbInformation
出口接头
如果结束
如果结束
应用
.ScreenUpdate=False
.EnableEvents=False
以
'复制文件/邮寄/删除文件
'如果要更改文件名,请仅更改TempFileName
TempFilePath=Environ$(“temp”)和“\”
TempFileName=“复制”&wb.Name&&“格式(现在为“dd-mmm-yy h-mm-ss”)
FileExtStr=“.”和LCase(右(wb.Name,Len(wb.Name)-InStrRev(wb.Name,“.”,1)))
wb.SaveCopyAs TempFilePath&TempFileName&FileExtStr
设置iMsg=CreateObject(“CDO.Message”)
设置iConf=CreateObject(“CDO.Configuration”)
“iConf.Load-1”CDO源默认值
'设置Flds=iConf.Fields
“与FLD
“.项目(”http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
“.项目(”http://schemas.microsoft.com/cdo/configuration/smtpserver“”=“在此处填写您的SMTP服务器”
“.项目(”http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
更新:
"以
与iMsg
Set.Configuration=iConf
.To=”ron@debruin.nl"
.CC=“”
.BCC=“”
.From=“”罗恩“
.Subject=“这是一项测试”
.TextBody=“这是正文”
.AddAttachment TempFilePath&TempFileName&FileExtStr
邮寄
以
'如果不想删除发送的文件,请删除此行
终止TempFilePath&TempFileName&FileExtStr
应用
.ScreenUpdate=True
.EnableEvents=True
以
端接头
我认为您需要做的是将此代码驻留在外接程序中。这样,您就不会试图通过电子邮件发送带有代码的现有文件。您正在发送运行代码的工作簿,所以它必须是.xlsm,所以您可以发送它。您必须创建一份不带宏的工作簿副本,然后发送此副本,或者将宏移动到PERSONAL(假设您发布的宏是工作簿中包含的唯一代码)