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
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