Vba 如何将excel工作表复制到其他工作表并通过电子邮件发送
我正在进行一个项目,用户将填写信息,当点击发送按钮时,确切的文件将以新名称邮寄给其他人 这是我的代码,可以很好地发送电子邮件Vba 如何将excel工作表复制到其他工作表并通过电子邮件发送,vba,email,excel,Vba,Email,Excel,我正在进行一个项目,用户将填写信息,当点击发送按钮时,确切的文件将以新名称邮寄给其他人 这是我的代码,可以很好地发送电子邮件 Sub send_email_via_Gmail() Dim myMail As CDO.Message Set myMail = New CDO.Message myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = Tru
Sub send_email_via_Gmail()
Dim myMail As CDO.Message
Set myMail = New CDO.Message
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "example@gmail.com"
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
myMail.Configuration.Fields.Update
With myMail
.Subject = "Test Email from Dr. Takyar"
.From = "example@gmail.com"
.To = "example1@gmail.com"
.TextBody = "Good morning!"
End With
myMail.Send
MsgBox ("Mail has been sent")
Set myMail = Nothing
End Sub
这是一段代码,它将精确复制现有图纸
Sub Add_Sheet()
ActiveSheet.Copy After:=Worksheets(Worksheets.Count)
Worksheets(Worksheets.Count).Name = InputBox("New Name:")
End Sub
现在我的问题是如何制作新的工作簿而不是工作表,并通过电子邮件发送它们
这是我的密码
Sub Add_Sheet()
Dim wb As Workbook
Set wb = Workbooks.add
ThisWorkbook.Sheets("Sheet1").Copy before:=wb.Sheets(1)
ActiveWorkbook.SaveAs Filename:="C:\Data\SalesData1.xlsx"
Workbooks("SalesData1.xlsx").Close
End Sub
经过一整天的麻烦,我终于找到了解决办法 下面是克隆工作表并通过gmail和 正在完全使用excel-2007-2010-2013
Sub Copy_Every_Sheet_To_New_Workbook()
'Working in 97-2013
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim sh As Worksheet
Dim DateString As String
Dim FolderName As String
Dim Mail As New Message
Dim name As String
Dim Config As Configuration
Dim fileName As String
Set Config = Mail.Configuration
With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
'Copy every sheet from the workbook with this macro
Set Sourcewb = ThisWorkbook
'Create new folder to save the new files in
DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
FolderName = Sourcewb.Path & "\" & Sourcewb.name & " " & DateString
fileName = Sourcewb.name & " " & DateString
MkDir FolderName
'Copy every visible sheet to a new workbook
For Each sh In Sourcewb.Worksheets
'If the sheet is visible then copy it to a new workbook
If sh.Visible = -1 Then
sh.Copy
'Set Destwb to the new workbook
Set Destwb = ActiveWorkbook
'Determine the Excel version and file extension/format
With Destwb
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007-2013
If Sourcewb.name = .name Then
MsgBox "Your answer is NO in the security dialog"
GoTo GoToNextSheet
Else
Select Case Sourcewb.FileFormat
Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
Case 52:
If .HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Case 56: FileExtStr = ".xls": FileFormatNum = 56
Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
End Select
End If
End If
End With
'Change all cells in the worksheet to values if you want
If Destwb.Sheets(1).ProtectContents = False Then
With Destwb.Sheets(1).UsedRange
.Cells.Copy
.Cells.PasteSpecial xlPasteValues
.Cells(1).Select
End With
Application.CutCopyMode = False
End If
'Save the new workbook and close it
With Destwb
.SaveAs FolderName _
& "\" & fileName & FileExtStr, _
FileFormat:=FileFormatNum
.Close False
End With
End If
GoToNextSheet:
Next sh
MsgBox "You can find the files in " & FolderName
MsgBox FolderName & "\" & ActiveSheet.name & ".xlsm"
Config(cdoSendUsingMethod) = cdoSendUsingPort
Config(cdoSMTPServer) = "smtp.gmail.com"
Config(cdoSMTPServerPort) = 465
Config(cdoSMTPAuthenticate) = cdoBasic
Config(cdoSMTPUseSSL) = True
Config(cdoSendUserName) = "example@gmail.com"
Config(cdoSendPassword) = "password"
Config.Fields.Update
Mail.To = "example1@gmail.com"
Mail.From = Config(cdoSendUserName)
Mail.Subject = "hhhhhhhhhhhhh"
Mail.AddAttachment FolderName & "\" & fileName & ".xlsm"
Mail.HTMLBody = "fffff"
Mail.Send
MsgBox "sent "
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = xlCalculationAutomatic
End With
End Sub
将每个工作表复制到新工作簿()
“97-2013年工作
Dim FileExtStr作为字符串
Dim FileFormatNum尽可能长
将Sourcewb设置为工作簿
将WB设置为工作簿
将sh设置为工作表
将日期字符串设置为字符串
Dim FolderName作为字符串
将邮件设置为新邮件
将名称设置为字符串
Dim配置为配置
将文件名设置为字符串
Set Config=Mail.Configuration
应用
.ScreenUpdate=False
.EnableEvents=False
.Calculation=xlCalculationManual
以
'使用此宏复制工作簿中的每张工作表
设置Sourcewb=ThisWorkbook
'创建新文件夹以在其中保存新文件
DateString=格式(现在为“yyyy-mm-dd-hh-mm-ss”)
FolderName=Sourcewb.Path&“\”&Sourcewb.name&“&DateString
fileName=Sourcewb.name&“”日期字符串
MkDir FolderName
'将每个可见工作表复制到新工作簿
对于Sourcewb.工作表中的每个sh
'如果工作表可见,则将其复制到新工作簿
如果sh.Visible=-1,则
sh.副本
'将Destwb设置为新工作簿
设置Destwb=ActiveWorkbook
'确定Excel版本和文件扩展名/格式
用Destwb
如果Val(Application.Version)<12,则
“您使用的是Excel 97-2003
FileExtStr=“.xls”:FileFormatNum=-4143
其他的
“您使用的是Excel 2007-2013
如果Sourcewb.name=.name,则
MsgBox“您的答案在安全对话框中为否”
转到转到下一页
其他的
选择Case Sourcewb.FileFormat
案例51:FileExtStr=“.xlsx”:FileFormatNum=51
案例52:
如果.hasvb项目
FileExtStr=“.xlsm”:FileFormatNum=52
其他的
FileExtStr=“.xlsx”:FileFormatNum=51
如果结束
案例56:FileExtStr=“.xls”:FileFormatNum=56
其他情况:FileExtStr=“.xlsb”:FileFormatNum=50
结束选择
如果结束
如果结束
以
'如果需要,请将工作表中的所有单元格更改为值
如果Destwb.Sheets(1).ProtectContents=False,则
与Destwb.Sheets(1)一起使用
.细胞,复制
.Cells.paste特殊XLPaste值
.单元格(1)。选择
以
Application.CutCopyMode=False
如果结束
'保存新工作簿并关闭它
用Destwb
.SaveAs FolderName_
&“\”&fileName&FileExtStr_
FileFormat:=FileFormatNum
.关闭错误
以
如果结束
GoToNextSheet:
下一个sh
MsgBox“您可以在”&FolderName中找到文件
MsgBox FolderName&“\”&ActiveSheet.name&“.xlsm”
配置(cdoSendUsingMethod)=cdoSendUsingPort
配置(cdoSMTPServer)=“smtp.gmail.com”
配置(cdoSMTPServerPort)=465
配置(cdoSMTPAuthenticate)=cdoBasic
配置(cdoSMTPUseSSL)=真
配置(cdoSendUserName)=“example@gmail.com"
配置(cdoSendPassword)=“密码”
Config.Fields.Update
Mail.To=”example1@gmail.com"
Mail.From=Config(cdoSendUserName)
Mail.Subject=“hhhhhhhhh”
Mail.AddAttachment文件夹名&“\”文件名&“.xlsm”
Mail.HTMLBody=“fffff”
邮寄
MsgBox“已发送”
应用
.ScreenUpdate=True
.EnableEvents=True
.Calculation=xlcalculation自动
以
端接头
要将工作表复制到新工作簿中,只需使用ActiveSheet。复制时不使用后面的部分:…
即可。有关CDO邮件发送的更多信息,请参见:要添加附件,可以使用myMail.AddAttachment“C:\path…”
方法。