Excel VBA 将工作表复制到新工作簿并在 Outlook 中通过电子邮件发送新工作簿
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18904723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel VBA Copying Sheets to a New Workbook and emailing the New Workbook in Outlook
提问by Richard Pullman
I have a segment of code that attaches the current file to an e-mail addressed to our sales rep y is equal to the sales rep email. and to the orders email for our company.
我有一段代码将当前文件附加到发送给我们销售代表的电子邮件中 y 等于销售代表电子邮件。和我们公司的订单电子邮件。
Instead of attaching the whole document to this email I want to copy tabs from the document and paste them into a new document. Then send only the new document (thereby reducing file size and hopefully changing it from a .xlsm attachment to a .xls attachment).
我不想将整个文档附加到此电子邮件中,而是想从文档中复制选项卡并将它们粘贴到新文档中。然后仅发送新文档(从而减小文件大小并希望将其从 .xlsm 附件更改为 .xls 附件)。
If ShapesAfter > ShapesBefore Then
MsgBox "Please Repair Invalid Equipment Selection", , "Invalid Selections _
Have Been Made"
ElseIf ShapesAfter = ShapesBefore Then
Sheets("inputs").Select
Dim y As String
y = Cells(61, 5).Value
Sheets("config").Select
Application.Dialogs(xlDialogSendMail).Show "" & y & "; " & "[email protected]"
回答by Matus Morong
Above example is returning Run-time '9' error. Here is corrected code:
上面的示例返回运行时“9”错误。这是更正的代码:
Dim wb As Workbook
Set wb = Workbooks.Add
ThisWorkbook.Sheets("inputs").Copy After:=wb.Sheets(1)
ThisWorkbook.Sheets("config").Copy After:=wb.Sheets(1)
wb.Application.Dialogs(xlDialogSendMail).Show "" & y & "; " & "[email protected]"
Set wb = Nothing
"ThisWorkbook." has to be added there.
“这本工作簿。” 必须在那里添加。
回答by Sorceri
Create a new workbook object and copy the sheets to it. Example below.
创建一个新的工作簿对象并将工作表复制到其中。下面举例。
Dim wb As Workbook
Set wb = Workbooks.Add
Sheets("inputs").Copy After:=wb.Sheets(1)
Sheets("config").Copy After:=wb.Sheets(1)
wb.Application.Dialogs(xlDialogSendMail).Show "" & y & "; " & "[email protected]"
Set wb = Nothing