VBA代码通过电子邮件发送带有数据的Excel文档但收到空白时

时间:2017-01-26 12:53:41

标签: excel vba excel-vba

第一次海报(和初学者VBA编码器)所以请温柔:)

我已经在excel中创建了一个表单,以便完成并通过电子邮件发送到特定的发行版,但是当它作为电子邮件发送时,所有数据都将丢失。

这是我正在使用的代码。我找到了一个帖子,它告诉我我需要添加编码来告诉它保存用户数据,但是对于我的生活,我无法找到如何添加它。

非常感谢任何建议/帮助:)

    Sub Sendit()
Dim OutlookApp As Object
Dim Mess As Object, Recip As String
Dim wb1 As Workbook

    Set OutlookApp = CreateObject("Outlook.Application")
Set Mess = OutlookApp.CreateItem(olMailItem)
With Mess
    .To = "user@email.co.uk"
    .BCC = Range("I12").Value
    .Subject = "A new Bright Idea has been submitted"
    .Body = "A new Bright Idea is attached."
    .Attachments.Add ThisWorkbook.FullName
    .Send
    MsgBox "Thank you for sharing your Bright Idea. You will be contacted once our Bright Ideas meeting has taken place with an update on the progress so far"
End With

End Sub

1 个答案:

答案 0 :(得分:0)

要使用.Attachmets.Add函数,必须先将工作簿保存到文件系统。

您可以使用此类内容自动保存,无需用户交互/确认(需要在Outlook发送代码之前进行) -

Application.DisplayAlerts = False 'DISABLE ALERT PROMPT

Dim BookName 
BookName = Range("A1").Value 'for example

'SAVES FILE USING THE VARIABLE BookName AS FILENAME
ActiveWorkbook.SaveAs Filename:="C:\Data\" & BookName, FileFormat:= _
xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False

Application.DisplayAlerts = True 'RESETS DISPLAY ALERTS