send activesheet bằng Outlook

Liên hệ QC

Tường_Vi

Thành viên tiêu biểu
Tham gia
19/4/10
Bài viết
482
Được thích
121
Điểm
0
Nơi ở
Hải Dương
Nghề nghiệp
Luôn tìm kiếm một vị trí tốt hơn
Giả sử ta có một file gồm nhiều sheet, mỗi sheet là tên một khách hàng. Và ta thỉnh thoảng cần gửi mail cho một khách hàng nào đó?
Bình thường muội vẫn phải
- Copy sheet đó
- Save thành một file mới, đặt tên
- Sau đó mới gửi file này đi

Thấy bất tiện, muội sưu tầm được code này từ
www.vbaexpress.com
và xin được chia sẻ

PHP:
Sub EmailWithOutlook()
     'Variable declaration
    Dim oApp As Object, _
    oMail As Object, _
    WB As Workbook, _
    FileName As String
     
     'Turn off screen updating
    Application.ScreenUpdating = False
     
     'Make a copy of the active sheet and save it to
     'a temporary file
    ActiveSheet.Copy
    Set WB = ActiveWorkbook
    FileName = ActiveSheet.Name & ".xls"
    On Error Resume Next
    Kill "C:\" & FileName
    On Error GoTo 0
    WB.SaveAs FileName:="C:\" & FileName
     
     'Create and show the outlook mail item
    Set oApp = CreateObject("Outlook.Application")
    Set oMail = oApp.CreateItem(0)
    With oMail
         'Uncomment the line below to hard code a recipient
         '.To = "someone@somedomain.com"
         'Uncomment the line below to hard code a subject
         '.Subject = "Look at my workbook!"
        .Attachments.Add WB.FullName
        .Display
    End With
     
     'Delete the temporary file
    WB.ChangeFileAccess Mode:=xlReadOnly
    Kill WB.FullName
    WB.Close SaveChanges:=False
     
     'Restore screen updating and release Outlook
    Application.ScreenUpdating = True
    Set oMail = Nothing
    Set oApp = Nothing
End Sub
 
Web KT
Back
Top