Gửi email tự động bằng Excel.

Liên hệ QC

vietnam123

Thành viên mới
Tham gia
4/9/07
Bài viết
12
Được thích
9
Mình tìm được đoạn mã này khá hay, đã test thành công với Outlook và Gmail. Rất tiện cho việc gửi data khi xử lý xong.
Mail a different file(s) to each person in a range Index (Only working when you use Excel-Outlook 2000 -2007)
Ron de Bruin ( Last update 28 Oct 2006)

Make a list in Sheets("Sheet1") withIn
column A : Names of the peopleIn
column B : E-mail addressesIn
column C:Z : Filenames like this C:\Data\Book2.xls (don't have to be Excel files)

The Macro will loop through each row in Sheet1 and if there is a E-mail address and file names that exist in that row it will create a mail with this information and send it.

Mã:
Sub Send_Files()
'Working in 2000-2007
Dim OutApp As Object
Dim OutMail As Object
Dim sh As Worksheet
Dim cell As Range, FileCell As Range, rng As Range
With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With
Set sh = Sheets("Sheet1")
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)
    'Enter the file names in the C:Z column in each row
    Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")
    If cell.Value Like "?*@?*.?*" And Application.WorksheetFunction.CountA(rng) > 0 Then
        Set OutMail = OutApp.CreateItem(0)
        With OutMail
            .To = cell.Value
            .Subject = "Testfile"
            .Body = "Hi " & cell.Offset(0, -1).Value
            For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                If Trim(FileCell) <> "" Then
                    If Dir(FileCell.Value) <> "" Then
                        .Attachments.Add FileCell.Value
                    End If
                End If
            Next FileCell
            .Send
            'Or use Display
        End With
        Set OutMail = Nothing
    End If
Next cell
Set OutApp = Nothing
With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With
End Sub
Xem thêm ở đây http://www.rondebruin.nl/sendmail.htm
 
Chỉnh sửa lần cuối bởi điều hành viên:
ongtrungducmx25 đã viết:
không hiểu lắm bạn hướng dẫnkir hơn 1 chút nhé
Mình đã Test thử, đoạn code làm việc tốt đấy chứ.
Cách sử dụng:
Mở Excel. Vào VBA, rồi Insert đoạn code trên.
Trở về Excel.
Cột A: Đánh tên người nhận
Cột B: Địa chỉ Email người nhận
Cột C đến cột Z: Là đường dẫn đầy đủ (Path) các file Excel (*.xls). Mỗi File trong 1 cột.
Cuối cùng là chạy Macro Send_Files.

Tuy nhiên muốn gửi được files thì bạn phải sử dụng thêm Outlook, và có 1 hòm thư hỗ trợ POP3 (Gmail chẳng hạn).
Cách sử dụng Outlook để gửi và nhận thư Gmail các bạn xem bài viết này!

P/S: Nếu đã sử dụng cả Outlook nữa thì có thể sử dụng luôn tính năng gửi Email có sẵn trong Excel ...
(Excel 2003: Có ngay trên thanh công cụ, Excel 2007: Trên thanh Quick Access Toolbar)
 
Upvote 0
Khi gởi email em thường bị trường hợp đó là Outlook ngăn không gởi email mà phải đợi 5 giây cho mỗi cái email gởi ra từ Excel,
Có ai biết xin chỉ giúp cách tránh được chuyện này.
 
Upvote 0
Đã khắc phục được chuyện đợi 5s của Outlook.

Sub Mailer()
Sheets("BB Email Data").Select
pathname = [b11].value 'defines attachment
dname = [b14].value 'defines date for subject
Dim objol As New outlook.Application
Dim objmail As MailItem
Set objol = New outlook.Application
Set objmail = objol.createitem(olmailitem)
With objmail
.To = "whoever" 'enter in here the email address
.cc = "whoever" 'enter in here the email address
.Subject = "Daily test email for " & dname
.Body = "Please find attached the teste email" & _
vbCrLf & "If you have any queries can you please let me know" & vbCrLf
.NoAging = True
.Attachments.Add pathname 'adds attachment to email
.display
End With
Set objmail = Nothing
Set objol = Nothing
SendKeys "%{s}", True 'send the email without prompts

End Sub

Xem thêm tại đây: http://www.tek-tips.com/faqs.cfm?fid=4334
 
Upvote 0
Mình có viết đoạn code này có thể send mail mà k cần Outlook,
nhưng code đang bị lỗi như sau
- chỉ g
ưi được 1-10 mail
-mỗi vòng lại tăng số file đính kèm

Private Sub CommandButton1_Click()

Dim iMsg As Object
Dim iConf As Object
Dim strbody As String
Dim Flds As Variant

Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
'----
Dim sh As Worksheet
Dim cell As Range, FileCell As Range, rng As Range
Set sh = Sheets("Sheet1")
'----
iConf.Load -1 ' CDO Source Defaults
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "trantrongluongwvi@gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "lengoctan"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"

.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Update
End With
'------

For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)

Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")

If cell.Value Like "?*@?*.?*" And Application.WorksheetFunction.CountA(rng) > 0 Then

With iMsg
Set .Configuration = iConf
.To = Cells(1, 2).Value
.From = """tran trong long-a"" <trantrongluongwvi@gmail.com>"
.CC = ""
.BCC = ""
.Subject = "PD for " & cell.Offset(0, -1).Value
.TextBody = "Dears College, find attached PD of " & cell.Offset(0, -1).Value & _
vbCrLf & "If you have any queries can you please let me know " & _
vbCrLf & "Please ! do not reply this email address ! used sending out PD only "
For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
If Trim(FileCell) <> "" Then
If Dir(FileCell.Value) <> "" Then
.AddAttachment FileCell.Value
End If
End If
Next FileCell
.Send
'MsgBox "scess"
End With

End If
Next cell


End Sub


nhờ các bạn xem hộ( fải cài CDO theo dc này mới chạy đợc
http://www.rondebruin.nl/mail/templates.htm




 
Upvote 0
Đợi bao nhiêu là do bạn !

Mình sưu tầm đc 1 code cho phép gởi và thời gian chờ là do ban quyết đinh
Hãy tạo 1 Form soạn mail roi dán code này vào thử nhé

Code in module :

Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long

Public Sub SendEMail(Optional Msg As String = "")
Dim Email As String, Subj As String
Dim URL As String
Dim r As Integer, X As Double

' Get the email address
Email = "Goi den"

' Message subject
Subj = "Tua de"

' Compose the message
'Msg = "Tuy"

URL = "mailto:" & Email & "?subject=" & Subj & "&body=" & Msg
' Execute the URL (start the email client)
ShellExecute 0&, vbNullString, URL, vbNullString, vbNullString, vbNormalFocus

' Wait two seconds before sending keystrokes
Application.Wait (Now + TimeValue("0:00:01"))

'Application.SendKeys "{Tab}{Tab}{Tab}{Tab}{Tab}^{End}{Return}{Return}^v"
'Application.SendKeys "%s"

End Sub

code Of Form :

Private Sub CommandButton1_Click()
prepareAndSendEmail
End Sub

Private Sub prepareAndSendEmail()
Dim Msg As String
If me.textbox1 ="" then
msgbox(..) do ban
textbox1.setfocut
Else
Me.Hide
Msg = me.textbox1' Noi dung thu
SendEMail Msg
End If
End Sub


Thử rồi cho ý kiến nhé
 
Lần chỉnh sửa cuối:
Upvote 0
Mình sưu tầm đc 1 code cho phép gởi và thời gian chờ là do ban quyết đinh
Hãy tạo 1 Form soạn mail roi dán code này vào thử nhé

Code in module :

Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long

Public Sub SendEMail(Optional Msg As String = "")
Dim Email As String, Subj As String
Dim URL As String
Dim r As Integer, X As Double

' Get the email address
Email = "Goi den"

' Message subject
Subj = "Tua de"

' Compose the message
'Msg = "Tuy"

URL = "mailto:" & Email & "?subject=" & Subj & "&body=" & Msg
' Execute the URL (start the email client)
ShellExecute 0&, vbNullString, URL, vbNullString, vbNullString, vbNormalFocus

' Wait two seconds before sending keystrokes
Application.Wait (Now + TimeValue("0:00:01"))

'Application.SendKeys "{Tab}{Tab}{Tab}{Tab}{Tab}^{End}{Return}{Return}^v"
'Application.SendKeys "%s"

End Sub

code Of Form :

Private Sub CommandButton1_Click()
prepareAndSendEmail
End Sub

Private Sub prepareAndSendEmail()
Dim Msg As String
If me.textbox1 ="" then
msgbox(..) do ban
textbox1.setfocut
Else
Me.Hide
Msg = me.textbox1' Noi dung thu
SendEMail Msg
End If
End Sub


Thử rồi cho ý kiến nhé



Mình đã thử cách này rồi mà vẫn không thể gửi email tự động được. Mỗi lần gửi đều hiện ra bảng thông báo, phải bấm nút Yes mới gửi được.
Xin các bác chỉ giáo giùm.
Many thanks
 
Upvote 0
Chào các anh chị em,

Cho mình hỏi một chút, nội dung email sẽ gửi cho danh sách người nhận sẽ soạn trên Word hay Excel nhỉ?

Xin cảm ơn.
 
Upvote 0
Gửi mail tự động

Mình tìm được đoạn mã này khá hay, đã test thành công với Outlook và Gmail. Rất tiện cho việc gửi data khi xử lý xong.
Mail a different file(s) to each person in a range Index (Only working when you use Excel-Outlook 2000 -2007)
Ron de Bruin ( Last update 28 Oct 2006)

Make a list in Sheets("Sheet1") withIn
column A : Names of the peopleIn
column B : E-mail addressesIn
column C:Z : Filenames like this C:\Data\Book2.xls (don't have to be Excel files)

The Macro will loop through each row in Sheet1 and if there is a E-mail address and file names that exist in that row it will create a mail with this information and send it.

Mã:
Sub Send_Files()
'Working in 2000-2007
Dim OutApp As Object
Dim OutMail As Object
Dim sh As Worksheet
Dim cell As Range, FileCell As Range, rng As Range
With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With
Set sh = Sheets("Sheet1")
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)
    'Enter the file names in the C:Z column in each row
    Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")
    If cell.Value Like "?*@?*.?*" And Application.WorksheetFunction.CountA(rng) > 0 Then
        Set OutMail = OutApp.CreateItem(0)
        With OutMail
            .To = cell.Value
            .Subject = "Testfile"
            .Body = "Hi " & cell.Offset(0, -1).Value
            For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                If Trim(FileCell) <> "" Then
                    If Dir(FileCell.Value) <> "" Then
                        .Attachments.Add FileCell.Value
                    End If
                End If
            Next FileCell
            .Send
            'Or use Display
        End With
        Set OutMail = Nothing
    End If
Next cell
Set OutApp = Nothing
With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With
End Sub
Xem thêm ở đây http://www.rondebruin.nl/sendmail.htm
Các anh này, Em có thử code trên nhưng khi gửi cứ phải click YES mới gửi được chứ. Khắc phục được không các anh?
 
Upvote 0
Make a list in Sheets("Sheet1") withIn
column A : Names of the peopleIn
column B : E-mail addressesIn
column C:Z : Filenames like this C:\Data\Book2.xls (don't have to be Excel files)

The Macro will loop through each row in Sheet1 and if there is a E-mail address and file names that exist in that row it will create a mail with this information and send it.

Mã:
Sub Send_Files()
'Working in 2000-2007
Dim OutApp As Object
Dim OutMail As Object
Dim sh As Worksheet
Dim cell As Range, FileCell As Range, rng As Range
With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With
Set sh = Sheets("Sheet1")
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)
    'Enter the file names in the C:Z column in each row
    Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")
    If cell.Value Like "?*@?*.?*" And Application.WorksheetFunction.CountA(rng) > 0 Then
        Set OutMail = OutApp.CreateItem(0)
        With OutMail
            .To = cell.Value
            .Subject = "Testfile"
            .Body = "Hi " & cell.Offset(0, -1).Value
            For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                If Trim(FileCell) <> "" Then
                    If Dir(FileCell.Value) <> "" Then
                        .Attachments.Add FileCell.Value
                    End If
                End If
            Next FileCell
            .Send
            'Or use Display
        End With
        Set OutMail = Nothing
    End If
Next cell
Set OutApp = Nothing
With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With
End Sub
Xem thêm ở đây http://www.rondebruin.nl/sendmail.htm[/QUOTE]
Mình đã Test thử, đoạn code làm việc tốt đấy chứ.
Cách sử dụng:
Mở Excel. Vào VBA, rồi Insert đoạn code trên.
Trở về Excel.
Cột A: Đánh tên người nhận
Cột B: Địa chỉ Email người nhận
Cột C đến cột Z: Là đường dẫn đầy đủ (Path) các file Excel (*.xls). Mỗi File trong 1 cột.
Cuối cùng là chạy Macro Send_Files.

Tuy nhiên muốn gửi được files thì bạn phải sử dụng thêm Outlook, và có 1 hòm thư hỗ trợ POP3 (Gmail chẳng hạn).
Cách sử dụng Outlook để gửi và nhận thư Gmail các bạn xem bài viết này!

P/S: Nếu đã sử dụng cả Outlook nữa thì có thể sử dụng luôn tính năng gửi Email có sẵn trong Excel ...
(Excel 2003: Có ngay trên thanh công cụ, Excel 2007: Trên thanh Quick Access Toolbar)

Mình đã làm được file gửi mail tự động, nhưng gặp vấn đề sau:
+ Chỉ gửi được cho các mail nội bộ VD:ABC@mycompany.com, còn những mail của tên miền khác kô gửi đi được. Cty mình xài Exchange Server. Có cách nào không cần dùng mail hỗ trợ POP3 mà vẫn có thể gửi được cho các mail tên miền khác không vậy
+ Khi gửi hiện lên thông báo, phải bấm nút YES mới chịu gửi, mỗi cuối tháng gửi cả ngàn email phải click từng cái YES hơi phê.....
Vui lòng giúp mình nhé. Các code macro của bài trên mình làm nhưng không chạy được.
Thanks so much!!!
 
Lần chỉnh sửa cuối:
Upvote 0
Mình làm theo như đoạn code trên web http://www.rondebruin.nl/sendmail.htm
Nhưng khi bấm gửi thì lại bị lỗi như hình đính kèm.
Hoibai_GPE.jpg

Gửi thư trong outlook thì vẫn bình thường. Mong mọi người giúp đỡ
 
Upvote 0
Chào yeudoi,

Nhưng mã lỗi và thông báo lỗi như thế nào?

Vbavn
 
Upvote 0
Mã:
Sub Send_Files()
'Working in 2000-2007
Dim OutApp As Object
Dim OutMail As Object
Dim sh As Worksheet
Dim cell As Range, FileCell As Range, rng As Range
With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With
Set sh = Sheets("Sheet1")
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants) [B][COLOR="#ff0000"]----> Không thực hiện được nếu trong ô đó ta để công thức[/COLOR][/B]
    'Enter the file names in the C:Z column in each row
    Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")
    If cell.Value Like "?*@?*.?*" And Application.WorksheetFunction.CountA(rng) > 0 Then
        Set OutMail = OutApp.CreateItem(0)
        With OutMail
            .To = cell.Value
            .Subject = "Testfile"
            .Body = "Hi " & cell.Offset(0, -1).Value
            For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                If Trim(FileCell) <> "" Then
                    If Dir(FileCell.Value) <> "" Then
                        .Attachments.Add FileCell.Value
                    End If
                End If
            Next FileCell
            .Send
            'Or use Display
        End With
        Set OutMail = Nothing
    End If
Next cell
Set OutApp = Nothing
With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With
End Sub
Tôi sừ dụng code này thấy hiểu quả nhưng gặp 1 lỗi. Nếu trong ô B (địa chỉ email) là 1 công thức thì code trên không thực hiện được. Có bác nào giúp em được không ạ? Thanks so much!!!
 
Upvote 0
Các bác vui lòng bỏ chút thời gian giúp mình và mọi người với nhé!
Thanks so much!
 
Upvote 0
Mã:
Sub Send_Files()
'Working in 2000-2007
Dim OutApp As Object
Dim OutMail As Object
Dim sh As Worksheet
Dim cell As Range, FileCell As Range, rng As Range
With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With
Set sh = Sheets("Sheet1")
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants) [B][COLOR="#ff0000"]----> Không thực hiện được nếu trong ô đó ta để công thức[/COLOR][/B]
    'Enter the file names in the C:Z column in each row
    Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")
    If cell.Value Like "?*@?*.?*" And Application.WorksheetFunction.CountA(rng) > 0 Then
        Set OutMail = OutApp.CreateItem(0)
        With OutMail
            .To = cell.Value
            .Subject = "Testfile"
            .Body = "Hi " & cell.Offset(0, -1).Value
            For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                If Trim(FileCell) <> "" Then
                    If Dir(FileCell.Value) <> "" Then
                        .Attachments.Add FileCell.Value
                    End If
                End If
            Next FileCell
            .Send
            'Or use Display
        End With
        Set OutMail = Nothing
    End If
Next cell
Set OutApp = Nothing
With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With
End Sub
Tôi sừ dụng code này thấy hiểu quả nhưng gặp 1 lỗi. Nếu trong ô B (địa chỉ email) là 1 công thức thì code trên không thực hiện được. Có bác nào giúp em được không ạ? Thanks so much!!!

Khúc mắc của bạn là năm ở xlCellTypeConstants bạn có thể tìm hiểu bài này là biết thay thế nó bằng cái gì là biết ngay thôi
http://www.giaiphapexcel.com/forum/...hi-chép-về-phương-thức-SpecialCells
 
Upvote 0
Mình thấy có rất nhiều ý tưởng và code gửi mail tự động để tiện lợi cho công việc. Vậy có thầy nào có code gửi mail tự động trên excel theo 1 thời gian cho trước không? (gửi theo giờ cố định)

Private Sub Workbook_Open()
Application.OnTime TimeValue("thời gian cần gửi"), "Macro mail"
End Sub
 
Lần chỉnh sửa cuối:
Upvote 0
Cho mình hỏi chút:
- Mình đã thực hiện được gửi mail bằng excel nhờ đoạn Code này.
- Nhưng có cách nào mình chỉnh được Format trong đoạn text gửi kèm mail được không.

Đoạn text gửi kèm mail là code này đúng không.
Bây giờ mình muốn Chữ "Hi" in đậm, còn Tên người dùng (cell.Offset(0, -1).Value) in nghiêng thì làm thế nào
.Body = "Hi " & cell.Offset(0, -1).Value
For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
If Trim(FileCell) <> "" Then
If Dir(FileCell.Value) <> "" Then
.Attachments.Add FileCell.Value
End If
End If

Rất mong các Pro giúp đỡ
 
Upvote 0
Cảm ơn mọi người rất nhiều
nhưng em vẫn ko biết làm thế nào, bác nào có số điện thoại cho em xin để em nhờ giúp đỡ ko a.
0916.075.605 rất mong phản hồi của các bác
 
Upvote 0
Web KT
Back
Top Bottom