|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
仅供参考
Sub Mail_ActiveSheet()
'http://www.rondebruin.nl/mail/folder2/mail2.htm
'Working in 2000-2007
Dim FileExtStr As String
Dim TempFilePath As String
Dim TempFileName As String
Dim MTO As String
Dim MCC As String
Dim sql As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim OutApp As Object
Dim OutMail As Object
Dim conn As Object
With Application
.ScreenUpdating = False
.EnableEvents = False
.DisplayAlerts = False
End With
With Sheet2
For i = 4 To .[A65536].End(xlUp).Row
Debug.Print Sheet1.[A65536].End(xlUp).Row
Debug.Print Sheet1.Cells(5, 1)
Debug.Print Sheet2.Cells(i, 1)
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.Range("A1:I4").Copy ActiveSheet.Cells(1, 1) '复制表头
Set conn = CreateObject("adodb.connection")
conn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;hdr=no;';Data Source=" & ThisWorkbook.FullName
sql = "select * from [对账单$a5:i" & Sheet1.[A65536].End(xlUp).Row & "] where f1='" & .Cells(i, 1) & "'"
ActiveSheet.Cells(5, 1).CopyFromRecordset conn.Execute(sql) '筛选数据到新增工作表
conn.Close
Set conn = Nothing
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 |
|