Saving emails from within access

A

amiga1200

I have a problem with the code listed below, my access database has Outllook
EntityID's stored in it & I would like to now save the actual emails to a
certain directory, the problem is that the EntityID's are from different
computers, I need to be able to skip messages that cause an error.

Code:
Private Sub Command74_Click()
On Error GoTo Err_Command74_Click:
Dim rs As Object
rs.MoveFirst
Do
Set myolApp = CreateObject("Outlook.Application")
Set myNameSpace = myolApp.GetNamespace("MAPI")
If Me.Category = "SENT" Then
Set myFolder = myNameSpace.GetDefaultFolder(5)
ElseIf Me.Category = "RECEIVED" Then
Set myFolder = myNameSpace.GetDefaultFolder(6)
End If
Set myMail = myNameSpace.GetItemFromID(Me.OutlookID)
myMail.SaveAs "c:\eMail\" & Me.OutlookID & ".msg" & olMSG
rs.MoveNext
Loop While Not rs.EOF
Exit_Command74_Click:
Exit Sub
Err_Command74_Click:
Resume Exit_Command74_Click:
End Sub
 
A

amiga1200

Altering it to the follwoing worked:

Private Sub ExtractEmails_Click()
On Error GoTo Err_ExtractEmails_Click:
Dim Cnt As Integer
Me.Recordset.MoveFirst
Do While Cnt < 1
Call SaveEmail(Me.OutlookID, Me.Category)
Me.Recordset.MoveNext
If Me.Recordset.EOF Then Cnt = 1
Loop
Exit_ExtractEmails_Click:
Exit Sub
Err_ExtractEmails_Click:
Resume Exit_ExtractEmails_Click:
End Sub

Public Sub SaveEmail(ID As String, Cat As String):
On Error GoTo Err_SaveEmail:
Set myolApp = CreateObject("Outlook.Application")
Set myNameSpace = myolApp.GetNamespace("MAPI")
If Cat = "SENT" Then
Set myFolder = myNameSpace.GetDefaultFolder(5)
ElseIf Cat = "RECEIVED" Then
Set myFolder = myNameSpace.GetDefaultFolder(6)
End If
Set myMail = myNameSpace.GetItemFromID(ID)
myMail.SaveAs "\\Main\c\Emails\" & ID & ".msg" & olMSG
Exit_SaveEmail:
Exit Sub
Err_SaveEmail:
Resume Exit_SaveEmail:
End Sub
 
J

John Nurick

That the Outlook EntryID can be quite long - longer, AFAIR, than the 255
character limit on Windows paths. That means that this line

myMail.SaveAs "\\Main\c\Emails\" & ID & ".msg" & olMSG

is not safe.
 

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