Em chào các anh chị, sau khi mày mò chắp vá em cũng cho ra được code gửi mail đồng loạt với attacth file riêng biệt. Nhưng những file được copy ra có độ rộng cột và hàng không bằng file gốc, nhờ các anh chị sửa giúp em ạ. Em cảm ơn nhiều!
Em copy code lên cho anh chị dễ nhìn, cụ thể trong file đính kèm ạ.
Public Sub CommandButton1_Click()
Dim OutApp As Object
Dim OutMail As Object
Dim FileName As String
Dim Ws As Worksheet
Application.ScreenUpdating = 0
Application.DisplayAlerts = False
Set OutApp = CreateObject("Outlook.Application")
For Each Ws In Worksheets
If Ws.Name <> "Tong ket" Then
FileName = "PA tháng " & Month(Date - 15) & "_" & Ws.Name & ".xls"
Ws.[A1:H30].Copy
Workbooks.Add
ActiveSheet.Paste
With ActiveWorkbook
.SaveAs ThisWorkbook.Path & "\" & FileName, FileFormat:=xlNormal
.Close
End With
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = Ws.Cells(5, 8)
.Subject = "PA tháng " & Month(Date - 15)
.Body = "Dear all " & cell & vbNewLine & "Toi gui moi nguoi cham diem PA, neu khong co y kien thi in ky chuyen TBQA" & vbNewLine _
& "Tran Trong"
.Attachments.Add ThisWorkbook.Path & "\" & FileName
.Display
End With
Kill ThisWorkbook.Path & "\" & FileName
End If
Next
Application.ScreenUpdating = 1
Application.DisplayAlerts = 1
End Sub
Em copy code lên cho anh chị dễ nhìn, cụ thể trong file đính kèm ạ.
Public Sub CommandButton1_Click()
Dim OutApp As Object
Dim OutMail As Object
Dim FileName As String
Dim Ws As Worksheet
Application.ScreenUpdating = 0
Application.DisplayAlerts = False
Set OutApp = CreateObject("Outlook.Application")
For Each Ws In Worksheets
If Ws.Name <> "Tong ket" Then
FileName = "PA tháng " & Month(Date - 15) & "_" & Ws.Name & ".xls"
Ws.[A1:H30].Copy
Workbooks.Add
ActiveSheet.Paste
With ActiveWorkbook
.SaveAs ThisWorkbook.Path & "\" & FileName, FileFormat:=xlNormal
.Close
End With
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = Ws.Cells(5, 8)
.Subject = "PA tháng " & Month(Date - 15)
.Body = "Dear all " & cell & vbNewLine & "Toi gui moi nguoi cham diem PA, neu khong co y kien thi in ky chuyen TBQA" & vbNewLine _
& "Tran Trong"
.Attachments.Add ThisWorkbook.Path & "\" & FileName
.Display
End With
Kill ThisWorkbook.Path & "\" & FileName
End If
Next
Application.ScreenUpdating = 1
Application.DisplayAlerts = 1
End Sub