Help needed opening instance of Outlook from Access and running code

Broker666

Registered User.
Local time
Today, 14:55
Joined
Dec 13, 2012
Messages
26
Hi,

I have the following code which downloads any email in a specific inbox sub-folder to a specified location and then deletes it. It works fine when I call the sub routine directly from Outlook, but when put the code into a module in Access it says there is a compile error and that the user-defined type is not defined, and it seems to relate to the below line of code.

Dim objMail As Outlook.Application

Any ideas? I assumed that as I was creating a new instance of Outlook this would run from a module in any Office application?


Sub SavePendingTxtfile()

If FileFolderExists("C:\OS\G_Reports\G_Pg.txt") Then
Kill "C:\OS\G_Reports\G_Pg.txt"
Else
End If

Dim objMail As Outlook.Application
Dim myOlapp As Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.MAPIFolder
Dim myItem As Outlook.MailItem
Dim myAttachment As Outlook.Attachment
Dim I As Long

Set myOlapp = CreateObject("Outlook.Application")
Set myNameSpace = myOlapp.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myFolder = myFolder.Folders("Pendingtrades")

'start of new code

'insert code to check that only one mail is in the folder

'end of new code

If myFolder.Items.Count <> 1 Then

MsgBox "Remove duplicate emails from Outlook 'Pendingtrade' folder and re-run", vbOKOnly

Exit Sub

Else

End If



For Each myItem In myFolder.Items
If myItem.Attachments.Count <> 0 Then
For Each myAttachment In myItem.Attachments
I = I + 1
myAttachment.SaveAsFile "C:\OS\G_Reports\G_Pg.txt"

Next
End If

Next


For Each myItem In myFolder.Items
If myItem.Attachments.Count <> 0 Then
For Each myAttachment In myItem.Attachments
myItem.Delete

Next
End If

Next



End Sub
 
Broker666, Make sure the Reference to Microsoft Outlook Library is set..
 
Now runs perfectly, thanks for your help.
 

Users who are viewing this thread

Back
Top Bottom