Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

automatically opening an e-mail in Outlook?

Status
Not open for further replies.

tekvb1977

Technical User
Nov 16, 2005
46
US
Is there a way in VBA that would open up the most recent
e-mail from a particular sender in outlook. The following code works fine but it only works if I have the e-mail open (Set myItem = myOlApp.ActiveInspector.CurrentItem).

' ##########
Set myOlApp = CreateObject("Outlook.Application")
Set myItem = myOlApp.ActiveInspector.CurrentItem
Set myAttachments = myItem.Attachments
myAttachments.Item(1).SaveAsFile "C:\My Documents\" & _
myAttachments.Item(1).DisplayName
' ################

I like to enhance this code such that the code opens up the outlook application, looks for the most recent e-mail from the particular sender, eeport the attachment from the e-mail to my personal folder.

I have been struggling with this quite a bit. Any ideas/help will be highly appreciated. Thanks

 
I'm not quite THAT familiar with the Outlook Object model.

Personally, I import ALL attachments in a given public folder and then move the items to a sub-folder called import. I hope the code below is helpful and I got the line continuation right.

Function moveMailItemDestination(ByVal OutDestFolder As Outlook.MAPIFolder, ByVal Outmailitem As Object) As Boolean
On Error GoTo moveMailItemDestination_err
moveMailItemDestination = False
Outmailitem.Move OutDestFolder
moveMailItemDestination = True

moveMailItemDestination_exit:
Exit Function
moveMailItemDestination_err:
MsgBox "Error " & Err.NUMBER & ": " & Err.Description, vbCritical, "moveMailItemDestination"
Resume moveMailItemDestination_exit
End Function

Some stuff from another function:

Dim OutApp As Outlook.Application
Dim OutNameSpace As Outlook.NameSpace
Dim OutFolder As Outlook.MAPIFolder
Dim OutDestinationFolder As Outlook.MAPIFolder
Dim OutItems As Items
Dim Outmailitem As Object
Dim OutAttachments As Outlook.Attachments
Dim OutAttachment As Outlook.Attachment

Set OutApp = CreateObject("Outlook.Application")
Set OutNameSpace = OutApp.GetNamespace("MAPI")
Set OutFolder = OutNameSpace.Folders("Public Folders").Folders("All Public Folders")_
.Folders("Folder Name").Folders("Another Folder Name")
Set OutDestinationFolder = OutFolder.Folders("Imported")
Set OutNotForImportFolder = OutFolder.Folders("Files That Are Not Flat")
Set OutItems = OutFolder.Items

'do stuff with mailitems / attachments

'Move mailitem... note it returns a boolean indicating success of failure.
moveMailItemDestination(OutDestinationFolder, Outmailitem)



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top