Macro for mailing reports

CaseP

New Member
Joined
Jun 20, 2003
Messages
2
Hi all,
I've got 23 reports, all in their separate xl-files, each named with a number corresponding to a person, and each one mailed to the corresponding person.

The files are named 1.xls, 2.xls, etc. I have a table of mail-addresses corresponding to the report name/numbers:

A.. B
1 mrD@company.com
2 mrC@company.com
etc..

I would appreciate some ideas on how to write the macro to pick the reports one by one (so far it's still clear) from the same folder, find the corresponding email address, and mail the report. This is probably quite basic, but as I've never written a macro that uses e-mail features.. :oops: well, I'd sure appreciate some advice.

Thanks a lot,
CaseP
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Case,

Welcme to the Board!

Are you using Outlook? If so calling that procedure is fairly straightfoward. There are tons of posts on the board, so just do a search. (Unfortunately, I use Lotus Notes, so I can't help you there). Just record what you want to do with the macro recorder and you'll get what you need. (Make sure to send the "test" e-mail to yourself or you might confuse a colleague!)

If you use Lotus Notes let me know and I'll post the code for that.

As for your files and recipients, if the files and their respective recipients are always going to be the same then you can simply hard code the sendmail & attachment procedure for each one and just call it from a Forms or command button or use a Workbook_Open procedure.

Hope that helps,

Smitty
 
Upvote 0
Smitty,

Thanks for your advice. There's a ton of posts on e-mail/outlook topics and I'm sure to find help there.

Shoulda thought of making a search instead of making an unnecessary post... well, I'l remember next time.

Thanks again,
Case
 
Upvote 0
Jon,

Here you go. You'll just need to modify my code to your needs. FYI - This takes a current file, creates a copy, then sends the copy as an attachment.

I've gotta give recognition in advance to the boardmember(s) who originally posted the code, so I could modify it to fit my needs.

Code:
Sub Send_Confirmation()

ActiveWorkbook.Save
    
With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
End With

Sheets("Order Confirmation").Visible = True
    Application.Goto Reference:="Order_Confirmation"
        ActiveSheet.Copy
            
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = True
        .DisplayStatusBar = True
        .DisplayFormulaBar = True
        .MoveAfterReturn = True
    End With
    
    Dim x As CommandBar
        For Each x In Application.CommandBars
            x.Enabled = True
        Next
        
With ActiveWorkbook
    .SaveAs "c:\Dreams IO.xls", FileFormat:=xlNormal
End With
    
yesno = MsgBox(" This will generate an e-mail confirmation for the Dreams Coordinator" _
    & vbCrLf & " Do you wish to send the Confirmation?" _
        , vbYesNo + vbQuestion, "Confirmation Generation")

Select Case yesno
    Case vbNo
    Exit Sub
End Select
Select Case yesno
    Case vbYes

'   Declare Variables for file and macro setup

Dim UserName As String
    Dim MailDbName As String
        Dim Maildb As Object
            Dim MailDoc As Object
        Dim AttachME As Object
    Dim Session As Object
Dim EmbedObj1 As Object

'   Open and locate current LOTUS NOTES User

Set Session = CreateObject("Notes.NotesSession")
    UserName = Session.UserName
        MailDbName = Left$(UserName, 1) & Right$(UserName, (Len(UserName) - InStr(1, UserName, " "))) & ".nsf"
Set Maildb = Session.GETDATABASE("", MailDbName)
    If Maildb.IsOpen = True Then
        Else
            Maildb.OPENMAIL
    End If

'   Create New Mail and Address Title Handlers

Set MailDoc = Maildb.CREATEDOCUMENT

MailDoc.Form = "Memo"
    MailDoc.SendTo = "RECIPIENT@ANYCOMPANY.com"
        MailDoc.Subject = "New T&E Insertion Order"
    MailDoc.Body = _
"Attached is a new Insertion Order for the Dreams publication.  Please acknowledge receipt."

'   Select Workbook to Attach to E-Mail

MailDoc.savemessageonsend = True
    attachment1 = "c:\Dreams IO.xls" 'Required File Name

If attachment1 <> "" Then
    On Error Resume Next
        Set AttachME = MailDoc.CREATERICHTEXTITEM("attachment1")
        Set EmbedObj1 = AttachME.embedobject(1454, "attachment1", "c:\Dreams IO.xls", "")
    On Error Resume Next
End If

MailDoc.PostedDate = Now()
    On Error GoTo errorhandler1
MailDoc.SEND 0, Recipient

Set Maildb = Nothing
    Set MailDoc = Nothing
        Set AttachME = Nothing
    Set Session = Nothing
Set EmbedObj1 = Nothing

'   Routine to Generate a copy if required

Application.Run "Protect"

OnOff = MsgBox("Do you want to save a copy?", vbYesNo + vbInformation, "Save Copy?")

Select Case OnOff
    Case vbNo
        ActiveWorkbook.Close
    Exit Sub
End Select
    
Select Case OnOff
    Case vbYes
        Set NewBook = ActiveWorkbook
            Do
                fName = Application.GetSaveAsFilename
            Loop Until fName <> False
        NewBook.SaveAs Filename:=fName
    ActiveWorkbook.Close

End Select

 Exit Sub

errorhandler1:

Set Maildb = Nothing
    Set MailDoc = Nothing
        Set AttachME = Nothing
    Set Session = Nothing
Set EmbedObj1 = Nothing

End Select

End Sub

Hope that helps,

Smitty
 
Upvote 0
Smitty -

Thanks. I'd seen your post in the top of a search of the archives. I made my request, then found many more examples. I am compiling all into a single package, which I will inflict on my client to try out. Kind of a sequence of approaches, from the most basic (i.e., most likely to succeed), to the more adventurous, which will more totally automate the process, but has more risk of misbehaving.

I appreciate your help, and the help of everyone else who has posted on this topic in the past few years (I went back a ways).

Thanks,

- Jon
-------
Jon Peltier, Microsoft Excel MVP
http://www.geocities.com/jonpeltier/Excel/index.html
_______
 
Upvote 0
Jon,

I looked back a ways today too and found that NateO might just be responsible for a lot of this... :diablo:

Pretty sad since we're probably 50-60 years older than him! :cry:

Smitty

Wish we'd had computers in high school! (Now I have an aversion to pencils...)
 
Upvote 0
I did notice that Nate was posting the picture of his high school age son. (I'm not going to admit anyone is that much younger than I am!!! Sorry, Nate.)

My senior year in high school was the first year they offered computer science classes. The town's school department had gotten a new computer, probably a huge mainframe with less smarts than my digital watch has now, and they put all their records (grades, payroll, etc.) onto it. A friend in the class managed to hack into the system and delete everything. Backups? Nobody had ever heard of that. They instituted some stricter security after that, but my friend was never found out. I'd like to say he's a Senior VP at Microsoft, but I think he's programming for a small private company.

- Jon
 
Upvote 0
Sorry I missed this last night, I was busy attending driver's ed. :lol:

Jon Peltier said:
Kind of a sequence of approaches, from the most basic (i.e., most likely to succeed), to the more adventurous, which will more totally automate the process, but has more risk of misbehaving.
Well Jon, in my opinion, all Lotus Script automation is high-risk, this software behaves in ways that I simply don't understand, perhaps due to my lack of insight on the particular object model, it's fairly opaque for me... Not much you can do about it though. Nice to see yours is a cleaner version than my post from way back when Smitty, feels like I submitted that one in a previous life, surprising how functional it is though, should rewrite it, but new projects continually arise...

So they do have decent PC's in US high schools these days? As a freshman in Canada we had a cutting edge (for the time) dos-based LAN stacked with Zenith PCs boasting 40 mb hard-drives and equipped with the Corel suite and either basic or vb, can't recall, that was indeed an eye opener for me. I emigrated to the US junior year, and my new school, a top 100 US public school had three green-screen Macs that looked like they were built in 1980 (this was some time after 1980 :) ). It was awful, I took a two-year hiatus from computers at that point.
 
Upvote 0
The consensus here and on other forums is that LN is even more of a pain than Outlook. I'll do as much as I can using SendMail, so I can test it on my machine, and so it will be more portable.

The schools have good computers and facilities. My daughter's new high school (opened last fall) has a wireless network, Macs everywhere (although her architecture class used PCs!), several in each classromm plus banks of them in several locations. But the teachers aren't really up to speed. My kids (10th, 7th, and 5th grade) are actually teaching the teachers how to use the equipment.

- Jon
 
Upvote 0

Forum statistics

Threads
1,222,622
Messages
6,167,104
Members
452,094
Latest member
Roberto Saveru

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top