要在电子邮件中发送工作表的宏 - 非活动工作表

时间:2014-01-28 14:50:32

标签: excel excel-vba vba

我需要创建一个宏,将指定的工作表作为自己的工作簿发送到电子邮件地址。此工作表不是活动工作表。基本上,我需要一个按钮页面,然后编程每个按钮以发送工作簿中的特定工作表(选项卡,等等)作为电子邮件的附件。同样,正在发送的纸张是与包含按钮的纸张不同的纸张。请指教。

1 个答案:

答案 0 :(得分:1)

那么,它未经测试但至少应该指向正确的方向。如果其他人知道sendUsing位引用的内容,那么请刷新我的记忆! (如果没有,我会试着仔细查看并记住,因为大部分代码是直接从我一年前做的工作中拉出来的)

Sub copyAndEmail()
Dim sendUsing, smtpServer, smtpPort, pathToWB As String
Dim wbOpen, wbSend As Workbook

sendUsing = "" 'cant remember what this one is actually, but I have it on "2" - will look through some old notes and check
smtpServer = "" 'this is your mail server IP
smtpPort = "" 'and your mail server port

pathToWB = "c:\Your\Sheet\Location.xls"

Set wbOpen = Workbooks.Open(pathToWB)

wbOpen.Sheets("Your Sheet").Copy

getTemp = Environ$("tmp")
    If getTemp = vbNullString Then
        getTemp = Environ$("temp")
    End If

Set wbSend = ActiveSheet.SaveAs(getTemp & "\" & Format(Now(), "ddmmyy_hhmm") & ".xls")

Set objMessage = CreateObject("CDO.Message")
With objMessage
    .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = sendUsing
    .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtpServer
    .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = smtpPort
    .Configuration.Fields.Update
    .Subject = "" 'Insert Subject Here
    .From = "" 'insert "From" email address
    .To = "" 'insert "To" email address
    .TextBody = "" 'Insert Body Here
    .AddAttachment wbSend ' attachment
    .Send
End With
Kill wbSend

End Sub