
Outlook VBA - open Excel, read in two fields, send email with these, close excel
Hi
I'm using Outlook 2000.
I have an excel document that looks like this
subject1 company1
subject2 company2
subject3 company3
And I want to create an email for each of these three sets with the
first column date going in the subject field and the second column
data going in the body.
Basically all I need to do to get it working is
Open Excel (from outlook)
open a file
make this active
find the value of A1 (and use Excel's Format function)
find the value of B1 (same as above but less)
Pass these into another sub.
Close the excel document
Quit Excel.
Below I have Two sub's. The first works fine when I pass in the two
pieces of info. It just creates an email with some stuff in it and
sends it.
The second which gets the data from the excel and passes them into the
first doesn't work.
Public Sub Trafficfigures(currentCoyId As String, CurrentCompanyName
As String)
Dim startNewOutlookApp As New Outlook.Application
Dim newEmail As Outlook.MailItem
Set newEmail = startNewOutlookApp.CreateItem(olMailItem)
With newEmail
.Subject = currentCoyId
.Body = "Regarding" & CurrentCompanyName & vbCrLf & vbCrLf
.Body = .Body & "Thanks, Phil"
.Attachments.Add "D:/cofund/attach.xls"
.Send
End With
End Sub
'This sub should open excel and the file emaillist.xls.
'It should then loop through the first column and while there is data
there it
'should read in the A and B columns for the row its on and call the
above Sub
'passing these two values in.
Sub LoopThroughList()
Dim moreEmailsToSend As Boolean
Dim currentRowI As Variant
Dim currentRowS As String
Dim currentCoyId As String
Dim CurrentCompanyName As String
moreEmailsToSend = True
currentRowI = 1
Set ExcelApp = CreateObject("Excel.Sheet")
ExcelApp.Application.Workbooks.Open "D:/cofund/emaillist.xls"
While moreEmailsToSend
ExcelApp.Application.Windows("emaillist.xls").Activate
currentRowS = currentRowI
currentCoyId =
Format(ExcelApp.Application.Worksheets(1).Range("A" +
currentRowS).Value, "00000")
CurrentCompanyName =
ExcelApp.Application.Worksheets(1).Range("B" + currentRowS).Value
moreCoyIds = False
currentCoyId = ""
If Not (currentCoyId = "") Then
moreEmailsToSend = True
Trafficfigures currentCoyId, CurrentCompanyName
End If
currentRowI = currentRowI + 1
Wend
ExcelApp.Application.Windows("emaillist.xls").Activate
ExcelApp.Application.ActiveWindow.Close
ExcelApp.Quit
End Sub
This second sub comes from something I created in Excel and I know the
looping part works fine.
When I run this in outlook 2000 it says it can't find the
emaillist.xls file.
It is however there and I can send it using the first sub as an
attachment so it can find it.
I've tried adding FileName:= which I use in excel but that doesn't
help.
I have run this in Outlook XP(2002)(not what i'm designing for) and it
will do all this but won't close Excel, which uses 60% of the CPU, and
when I shut it down the macro says there was an error in the first
ExcelApp.Application.Worksheets(1).Range line. Though the messages do
get sent.
Any help would be greatly appreciated.
Many thanks,
Philip Hanna.