Excel根据工作表名称发送电子邮件
我制作了一个宏,将主工作表拆分为不同的选项卡,并重命名这些选项卡。我想通过将标签名称与包含电子邮件地址的列表相匹配,将标签发送给不同的人 我现在得到的是:Excel根据工作表名称发送电子邮件,excel,email,outlook,tabs,worksheet,Excel,Email,Outlook,Tabs,Worksheet,我制作了一个宏,将主工作表拆分为不同的选项卡,并重命名这些选项卡。我想通过将标签名称与包含电子邮件地址的列表相匹配,将标签发送给不同的人 我现在得到的是: Sub Split_To_Workbook_and_Email_with_Body() 'Working in 2013/2016 Dim FileExtStr As String Dim FileFormatNum As Long Dim Sourcewb As Workbook Dim Destwb As
Sub Split_To_Workbook_and_Email_with_Body()
'Working in 2013/2016
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 myOutlook As Object
Dim myMailItem As Object
Dim mySubject As String
Dim myto As String
Dim myPath As String
With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
'Prompt for Email Subject
Set otlApp = CreateObject("Outlook.Application")
'mySubject = InputBox("Subject for Email")'this shows a dialog where you can enter the email subject (right now it is hard coded)
'myto = Application.VLOOKUP(SheetId, Sheet1!A3:B48, 2, FALSE)
'myto = Application.VLookup(SheetId, Sheet1!A3:B48, 2, range_lookup)
'Copy every sheet from the workbook with this macro
Set Sourcewb = ActiveWorkbook
'Create new folder to save the new files in
DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
FolderName = "Z:\user\report" & 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-2016
If Sourcewb.Name = .Name Then
MsgBox "Your answer is NO in the security dialog"
GoTo GoToNextSheet
Else
FileExtStr = ".xlsx": FileFormatNum = 51
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, email it, and close it
Set otlNewMail = otlApp.CreateItem(olMailItem)
With Destwb
.SaveAs FolderName _
& "\" & Destwb.Sheets(1).Name & FileExtStr, _
FileFormat:=FileFormatNum
End With
myPath = ActiveWorkbook.Path & "\" & ActiveWorkbook.Name
idkfile = "C:\Users\UniaHa\Desktop\Testing.txt"
With Destwb
.Close False
End With
With otlNewMail
'.Subject = mySubject
.to = test@test.ca
.Subject = "Diversion Report"
.Body = "Dear customer," & vbNewLine & vbNewLine & _
"This is your report please..... blah blah" & _
vbNewLine & vbNewLine & "Regards," & vbNewLine & vbNewLine & "Sender Name"
.Attachments.Add myPath
.Attachments.Add idkfile
.Display
End With
Set otlNewMail = Nothing
End If
GoToNextSheet:
Next sh
MsgBox "You can find the files in " & FolderName
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = xlCalculationAutomatic
End With
End Sub
Sub-Split_To_工作簿_和_Email_与_Body()
“2013/2016年工作
Dim FileExtStr作为字符串
Dim FileFormatNum尽可能长
将Sourcewb设置为工作簿
将WB设置为工作簿
将sh设置为工作表
将日期字符串设置为字符串
Dim FolderName作为字符串
看起来像物体的暗淡的肌肉
将myMailItem设置为对象
将我的主题设置为字符串
将myto变暗为字符串
将myPath设置为字符串
应用
.ScreenUpdate=False
.EnableEvents=False
.Calculation=xlCalculationManual
以
'提示输入电子邮件主题
设置otlApp=CreateObject(“Outlook.Application”)
'mySubject=InputBox(“电子邮件主题”)'显示一个对话框,您可以在其中输入电子邮件主题(现在它是硬编码的)
'myto=Application.VLOOKUP(SheetId,Sheet1!A3:B48,2,FALSE)
'myto=Application.VLookup(SheetId,Sheet1!A3:B48,2,范围查找)
'使用此宏复制工作簿中的每张工作表
设置Sourcewb=ActiveWorkbook
'创建新文件夹以在其中保存新文件
DateString=格式(现在为“yyyy-mm-dd-hh-mm-ss”)
FolderName=“Z:\user\report”&Sourcewb.Name&”“&DateString
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-2016
如果Sourcewb.Name=.Name,则
MsgBox“您的答案在安全对话框中为否”
转到转到下一页
其他的
FileExtStr=“.xlsx”:FileFormatNum=51
如果结束
如果结束
以
'如果需要,请将工作表中的所有单元格更改为值
如果Destwb.Sheets(1).ProtectContents=False,则
与Destwb.Sheets(1)一起使用
.细胞,复制
.Cells.paste特殊XLPaste值
.单元格(1)。选择
以
Application.CutCopyMode=False
如果结束
'保存新工作簿,发送电子邮件,然后关闭它
设置otlNewMail=otlApp.CreateItem(olMailItem)
用Destwb
.SaveAs FolderName_
&“\”&Destwb.Sheets(1).Name&FileExtStr_
FileFormat:=FileFormatNum
以
myPath=ActiveWorkbook.Path&“\”&ActiveWorkbook.Name
idkfile=“C:\Users\UniaHa\Desktop\Testing.txt”
用Destwb
.关闭错误
以
用otlNewMail
'.Subject=mySubject
.至=test@test.ca
.Subject=“转移报告”
.Body=“亲爱的客户,”&vbNewLine&vbNewLine&_
“这是你的报告……诸如此类”_
vbNewLine&vbNewLine&“问候,”&vbNewLine&vbNewLine&“发件人姓名”
.Attachments.Add myPath
.Attachments.Add idkfile
.展示
以
设置otlNewMail=Nothing
如果结束
GoToNextSheet:
下一个sh
MsgBox“您可以在”&FolderName中找到文件
应用
.ScreenUpdate=True
.EnableEvents=True
.Calculation=xlcalculation自动
以
端接头
对于您的vlookup,您可以尝试以下方法:
On Error Resume Next
for each sht in activeworkbook.worksheets
err.clear
sEmailTo = Application.worksheetfunction.VLOOKUP(sh.name, Sheet1!A3:B48, 2, FALSE)
if err<>0 and semailto like "*@*" Then
'send your email
end if
next sht
On Error goto 0
出错时继续下一步
对于activeworkbook.worksheets中的每个sht
清楚
sEmailTo=Application.worksheetfunction.VLOOKUP(sh.name,Sheet1!A3:B48,2,FALSE)
如果err0和semailto类似于“*@*”,那么
'发送您的电子邮件
如果结束
下一步
错误转到0
你在努力解决什么问题?它可以像在循环中拉取sEmailTo=Application.worksheetfunction.VLOOKUP(sh.name,Sheet1!A3:B48,2,FALSE)
一样简单。因此,一旦出现错误,它就会崩溃,您将如何忽略错误并继续?同样感谢您,这非常有效。您可以使用最小的错误处理,例如在错误恢复下一步之前<代码>和错误转到0之后<代码>等