【问题标题】:Macro to Send Sheet in email - NOT active sheet在电子邮件中发送工作表的宏 - 不是活动工作表
【发布时间】:2015-02-26 05:35:03
【问题描述】:

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

【问题讨论】:

  • 只要您对每张工作表都有明确的路径,就应该相当容易。将在一秒钟内提出解决方案

标签: excel vba


【解决方案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

【讨论】:

  • 找到了解释。根据您的电子邮件配置,您可能需要更改它:msdn.microsoft.com/en-us/library/ms526318(v=exchg.10).aspx
  • 这是用于发送 Outlook 电子邮件吗?抱歉,我应该指定的!
  • 在这种情况下没有区别。可以创建一个 Outlook 实例并从中发送电子邮件,但这种方式应该更快
  • 嗯——好吧,有些感觉有点多余,但这可能只是我的无知!我将按钮放在包含我要发送的工作表的工作簿中——我有按钮可以发送活动工作表,效果很好——但我想我想弄清楚如何发送不同的工作表...我没有正确调整我的宏...我可能会稍后发布它以寻求帮助。
  • 您需要发送的是同一工作簿中的工作表吗?如果是这样,您可以消除最初打开工作簿的需要,只需从复制工作表开始。其中一些可能对您的需求来说是多余的,但要进行更广泛的概述以尝试预测任何可能的需求
猜你喜欢
  • 2017-07-06
  • 1970-01-01
  • 1970-01-01
  • 2021-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多