Searching through emails

  • Thread starter news.btinternet.com
  • Start date
N

news.btinternet.com

I am trying to run a module which searches my inbox and saves the
attachments, but the code exits before it searches all emails. I think it
has something to do with my moving the email after I save the attachment but
I cant seem to sort out the code.
This is the code I have so far:
Public Sub AuditEmailExtract()
On Error GoTo Err_AuditEmailExtract

Dim varresponse As Integer
Dim oApp As Object
Dim olookApp As Outlook.Application
Dim objAttachments As Outlook.Attachments
Dim Attachment As Outlook.Attachment
Dim myNameSpace As NameSpace
Dim myfolder As Outlook.MAPIFolder
Dim myprofile As Recipient
Dim myItem As Outlook.MailItem
Dim strFile As String, strSaved As String
Dim myDestFolder
Dim myDestFolder1
Dim myDestFolder2
Dim myDestFolder3
Dim myDestFolder4
Dim myDestFolder5
Dim strName As String, intStartPosition As Integer

varresponse = MsgBox("Extract Site Audit Attachment", vbYesNo +
vbQuestion)
If varresponse = vbNo Then GoTo Exit_AuditEmailExtract_Click
If varresponse = vbYes Then
DoCmd.Hourglass True

' create the Outlook session.
Set olookApp = CreateObject("Outlook.Application")
Set myNameSpace = olookApp.GetNamespace("MAPI")
Set myprofile = myNameSpace.CreateRecipient("data msmc g")

Set myfolder = myNameSpace.GetSharedDefaultFolder(myprofile,
olFolderInbox)


For Each myItem In myfolder.Items
If myItem.Subject Like "*" & "Site Audit" & "*" Then




strName = myItem.Subject
intStartPosition = InStr(7, strName, " ") + 1
strName = Mid(strName, intStartPosition)

With myItem

Set objAttachments = myItem.Attachments
For Each Attachment In objAttachments

strFile = "N:\Customer Management
Centre\Warrington\Reach Team\Database\Site Audits\" & strName & ".xls"
Attachment.SaveAsFile (strFile)
strSaved = "Saved to " & strFile & " on
" & Now()
Next

If Len(Trim(strSaved)) > 0 Then
myItem.body = vbCrLf & vbCrLf &
strSaved & vbCrLf & vbCrLf & myItem.body
myItem.Save
End If

'Set myItem = myItem.CurrentItem
Set myDestFolder =
olookApp.Session.Folders("public folders")
Set myDestFolder1 =
myDestFolder.Folders("all public folders")
Set myDestFolder2 =
myDestFolder1.Folders("bt plc")
Set myDestFolder3 =
myDestFolder2.Folders("mobile")
Set myDestFolder4 =
myDestFolder3.Folders("bt reach services")
Set myDestFolder5 =
myDestFolder4.Folders("Site Audits")
myItem.Move (myDestFolder5)

Set olookApp = Nothing
Set myNameSpace = Nothing
Set myprofile = Nothing
Set myDestFolder = Nothing
Set myDestFolder1 = Nothing
Set myDestFolder2 = Nothing
Set myDestFolder3 = Nothing
Set myDestFolder4 = Nothing
Set myDestFolder5 = Nothing


Set oApp =
CreateObject("Excel.Application")
strFile = "N:\Customer Management
Centre\Warrington\Reach Team\Database\Site Audits\" & strName & ".xls"
oApp.Workbooks.Open strFile
oApp.Visible = True

End With
End If

Next


End If

DoCmd.Hourglass False

Exit_AuditEmailExtract_Click:
Exit Sub

Err_AuditEmailExtract:
MsgBox Err.Description
Resume Exit_AuditEmailExtract_Click


End Sub





Any help would be appreciated

Thanks
Dave
 
F

F. H. Muffman

news.btinternet.com said:
I am trying to run a module which searches my inbox and saves the
attachments, but the code exits before it searches all emails. I think it
has something to do with my moving the email after I save the attachment
but
I cant seem to sort out the code.


You'll probably have better luck either in
microsoft.public.outlook.program_vba or at the forums at
http://www.outlookcode.com, but let me make a couple of suggestions

If you think it has to do with your moving the email afer you save the
attachment, then why not, y'know, remove that part of the code and see if it
works? I mean, that'd be the first test I'd think to try before posting
over there, since that will give you more information.

Also, I would consider, rather than looping through the items, simply write
the code for a single email. Make that code the action on a rule. Heck, if
you do that, you could probably even put the Subject Line check into the
rule rather than the in the code. And you could even put the move into the
rule too. In fact, why do it this way at all? Why aren't you using a rule
with the Save Attachement code as the action?

And lastly, if you're going to post to the other places... what exactly does
happen? I mean, I get that not all messages are scanned. But, is it random
messages within the folder that don't get scanned, or is it just the last X
messages? Is anything happening to the last X messages (ie, are they being
moved but not saving the attachments)?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top