Posted to microsoft.public.excel.programming
|
|
Opening MS Applications
Set olApp = CreateObject("Outlook.Application")
Should open Outlook.
--
Regards,
Tom Ogilvy
"gti_jobert" wrote:
Hi all, thanks for the advice!
I have the following code;
The probolem is that the do and for loops will NOT be executed as
Outlook doesn;t seem to be opened! If I open Outlook first then run
this Function then it works a treat! Any ideas bob....or anyone else?
TIA
Code:
--------------------
Sub SendAnEmailWithOutlook(CurrFile)
Dim olApp As Outlook.Application
Dim olMail As MailItem
Dim mailSent As Boolean, countMsg%
Dim olNamespace As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim lngRow As Long
Dim intAtt As Integer
Dim wbkTemp As Workbook
Dim strTempFile As String
Set olApp = CreateObject("Outlook.Application")
Set olMail = olApp.CreateItem(olMailItem)
Set olNamespace = olApp.GetNamespace("MAPI")
Set olFolder = olNamespace.GetDefaultFolder(olFolderSentMail)
countMsg = olFolder.Items.count
With olMail
.To = "
'.CC = "
.Subject = "Textron Schedule Agreements: " & Right(CurrFile, 13)
.Attachments.Add CurrFile & ".xls"
.Display
.OriginatorDeliveryReportRequested = True
.ReadReceiptRequested = True
End With
Do
Loop Until olFolder.Items.count = (countMsg + 1)
'go thru all mail in Sent Items
For Each olMail In olFolder.Items
' only check those with attachments
For intAtt = 1 To olMail.Attachments.count
' only those with xls files
If InStr(1, olMail.Attachments(intAtt).FileName, ".xls", vbTextCompare) 0 Then
' get folder and filename for xls file
strTempFile = ThisWorkbook.Path & Application.PathSeparator & olMail.Attachments(intAtt).FileName
' save it so we can open and read it
olMail.Attachments(intAtt).SaveAsFile strTempFile
Set wbkTemp = Workbooks.Open(strTempFile)
If Right(CurrFile, 13) & ".xls" = olMail.Attachments(intAtt).FileName Then
MsgBox "Mail Has Been Sent!!"
End If
' close and destroy temporary excel file
wbkTemp.Close False
Set wbkTemp = Nothing
Kill strTempFile
End If
Next
Next
Set olFolder = Nothing
Set olNamespace = Nothing
Set olMail = Nothing
Set olApp = Nothing
End Sub
--------------------
--
gti_jobert
------------------------------------------------------------------------
gti_jobert's Profile: http://www.excelforum.com/member.php...o&userid=30634
View this thread: http://www.excelforum.com/showthread...hreadid=545424
|