|
Sub Mail_ActiveSheet()
'http://www.rondebruin.nl/mail/folder2/mail2.htm
'Working in 2000-2007
Dim FileExtStr As String, TempFilePath As String, TempFileName As String
Dim MTO As String, MCC As String, Ad As String, Sql As String
Dim Addr As Integer
Dim FileFormatNum As Long
Dim Sourcewb As Workbook, Destwb As Workbook
Dim OutApp As Object, OutMail As Object, Conn As Object
With Application
.ScreenUpdating = False
.EnableEvents = False
.DisplayAlerts = False
End With
With Sheet2
For i = 4 To .[A65536].End(xlUp).Row
Set c = Sheet1.Range("A4:A" & Sheet1.[A65536].End(xlUp).Row).Find(.Cells(i, 1), , , 1)
If Not c Is Nothing Then
For Each sh In Sheets
If sh.Index > 2 Then sh.Delete '刪除"公司"工作表
Next sh
Sheets.Add After:=Sheets(Sheets.Count) '新增筛选工作表
ActiveSheet.Name = .Cells(i, 1) '新工作表命名
Sheet1.Rows("1:4").Copy ActiveSheet.Cells(1, 1) '复制表头
Addr = Sheet1.[a4].End(xlToRight).Column
Ad = Split(Cells(1, Addr).Address, "$")(1)
Set Conn = CreateObject("adodb.connection")
If Val(Application.Version) < 12 Then
'You use Excel 2000-2003
FileExtStr = ".xls": FileFormatNum = -4143
Conn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;hdr=no;';Data Source=" & ThisWorkbook.FullName
Else
'You use Excel 2007-2010
FileExtStr = ".xlsx": FileFormatNum = 51
Conn.Open "provider=Microsoft.ACE.OLEDB.12.0;extended properties='excel 12.0;hdr=no';data source=" & ThisWorkbook.FullName
End If
Sql = "select * from [对账单$A5:" & Ad & Sheet1.[A65536].End(xlUp).Row & "] where f1='" & .Cells(i, 1) & "'"
ActiveSheet.Cells(5, 1).CopyFromRecordset Conn.Execute(Sql) '筛选数据到新增工作表
Conn.Close
Set Conn = Nothing
ActiveSheet.UsedRange.Borders.LineStyle = xlContinuous '加表格框线
Set Sourcewb = ActiveWorkbook
'Copy the sheet to a new workbook
ActiveSheet.Copy
'ActiveSheet.Delete
Set Destwb = ActiveWorkbook
'Determine the Excel version and file extension/format
With Destwb
If Val(Application.Version) < 12 Then
'You use Excel 2000-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007, we exit the sub when your answer is
'NO in the security dialog that you only see when you copy
'an sheet from a xlsm file with macro's disabled.
If Sourcewb.Name = .Name Then
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
MsgBox "Your answer is NO in the security dialog"
Exit Sub
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
' With Destwb.Sheets(1).UsedRange
' .Cells.Copy
' .Cells.PasteSpecial xlPasteValues
' .Cells(1).Select
' End With
' Application.CutCopyMode = False
'Save the new workbook/Mail it/Delete it
TempFilePath = Environ$("temp") & "\"
TempFileName = .Cells(i, 1) & "公司对账单 " _
& Format(Now, "dd-mm-yyyy") '附档档案名称
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
Destwb.SaveAs TempFilePath & TempFileName & FileExtStr, _
FileFormat:=FileFormatNum
On Error Resume Next
MTO = "": MCC = ""
For j = 2 To 4
If .Cells(i, j) <> "" Then MTO = MTO & ";" & .Cells(i, j) '收件人
If .Cells(i, j + 3) <> "" Then MCC = MCC & ";" & .Cells(i, j + 3) '抄送
Next j
OutMail.TO = Mid(MTO, 2) '收件人
OutMail.CC = Mid(MCC, 2) '抄送
'.BCC = ""
OutMail.Subject = .Cells(i, 1) & "公司对账单"
'.Body = "Hi there"
StrMail = "<H3>Dear " & .Cells(i, 1) & "</H3>" & _
"附件是 貴公司的公司对账单,如对发出的内容有不明之处,请与本人联系。<BR><BR>" & _
"谢谢!</FONT>"
OutMail.HTMLBody = StrMail
OutMail.Attachments.Add Destwb.FullName '对账单附档
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
OutMail.Send 'or use OutMail.Display
On Error GoTo 0
Destwb.Close SaveChanges:=False
'Delete the file you have send
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
End If
Next i
For Each sh In Sheets
If sh.Index > 2 Then sh.Delete '刪除"对账单","联系人邮箱"以外工作表
Next sh
End With
With Application
.ScreenUpdating = True
.EnableEvents = True
.DisplayAlerts = True
End With
End Sub |
|