我正在尝试将我的工作簿作为.csv或.xls文档发送为电子邮件的附件。由于我使用.xlsm文档来运行我的宏,因此附件将是.xlsm文档。我的工作簿应该输入一个Web应用程序,我需要它是.xls或.csv。
我可以在下面的代码中添加任何行,以便在发送之前将其创建为.csv或.xls文档吗?我不想在我的计算机上保存额外的副本,只是想让它更改附件中的文档类型。
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = ""
.CC = ""
.BCC = ""
.Subject = ""
.Body = ""
.Attachments.Add ActiveWorkbook.FullName
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
答案 0 :(得分:3)
您需要在某处实际创建工作簿的副本,以便更改文件类型,附加并发送。您可以使用以下功能执行此操作:
Function GetCSVCopy() As String
Set wb = ActiveWorkbook
Set tempSheet = ActiveSheet.Copy
tempName$ = Environ$("TEMP") & "\" & Replace$(wb.Name, ".xlsm", ".csv")
tempSheet.SaveAs tempName, xlCSV
tempSheet.Close False
wb.Activate
GetCSVCopy = tempName
End Function
像这样使用:
With OutMail
.To = ""
.CC = ""
.BCC = ""
.Subject = ""
.Body = ""
attName$ = GetCSVCopy
.Attachments.Add attName
.Send
End With
Kill attName '// Remove file after use.