473,382 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Outlook / Email

Can anyone help? I am importing Emails from Outlook using the following
code:

Public Function ScanInbox(SubjectLine As String)
Dim TempRst As Recordset
Dim OlApp As Outlook.Application
Dim Inbox As Outlook.MAPIFolder
Dim InboxItems As Outlook.Items
Dim Mailobject As Object
Set OlApp = CreateObject("Outlook.Application")
Set Inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFold erInbox)
Set TempRst = CurrentDb.OpenRecordset("tbl_OutlookTemp")
If SubjectLine <"" Then
Set InboxItems = Inbox.Items.Restrict("[Subject] = """ & SubjectLine
& """")
Else
Set InboxItems = Inbox.Items
End If
For Each Mailobject In InboxItems
With TempRst
On Error Resume Next
.AddNew
!Subject = Mailobject.Subject
!From = Mailobject.SenderName
!To = Mailobject.To
!Body = Mailobject.Body
!DateSent = Mailobject.SentOn
!Attachment = Mailobject.Attachment
.Update
'Mailobject.Delete
Mailobject.Read
End With
Next

Set OlApp = Nothing
Set Inbox = Nothing
Set InboxItems = Nothing
Set Mailobject = Nothing
Set TempRst = Nothing
End Function
The emails which I would like to import contain an attachment. Does anyone
know how I can either save the attachement in Access as a memo or OLE
object? or even save a link to open the attachement?

I have tried adding !Attachment = Mailobject.Attachment but nothing is added
to the temporary table?

Thank you for your help.

Dean Spencer.
Dec 11 '06 #1
7 6766
Dean Spencer wrote:
Can anyone help? I am importing Emails from Outlook using the following
code:

Public Function ScanInbox(SubjectLine As String)
Dim TempRst As Recordset
Dim OlApp As Outlook.Application
Dim Inbox As Outlook.MAPIFolder
Dim InboxItems As Outlook.Items
Dim Mailobject As Object
Set OlApp = CreateObject("Outlook.Application")
Set Inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFold erInbox)
Set TempRst = CurrentDb.OpenRecordset("tbl_OutlookTemp")
If SubjectLine <"" Then
Set InboxItems = Inbox.Items.Restrict("[Subject] = """ & SubjectLine
& """")
Else
Set InboxItems = Inbox.Items
End If
For Each Mailobject In InboxItems
With TempRst
On Error Resume Next
.AddNew
!Subject = Mailobject.Subject
!From = Mailobject.SenderName
!To = Mailobject.To
!Body = Mailobject.Body
!DateSent = Mailobject.SentOn
!Attachment = Mailobject.Attachment
.Update
'Mailobject.Delete
Mailobject.Read
End With
Next

Set OlApp = Nothing
Set Inbox = Nothing
Set InboxItems = Nothing
Set Mailobject = Nothing
Set TempRst = Nothing
End Function
The emails which I would like to import contain an attachment. Does anyone
know how I can either save the attachement in Access as a memo or OLE
object? or even save a link to open the attachement?

I have tried adding !Attachment = Mailobject.Attachment but nothing is added
to the temporary table?

Thank you for your help.

Dean Spencer.
Hi, Dean.

Your question is really about the Outlook object model and not about
Access per se. That said, if you check the Object Browser you will see
that there is no "Attachment" property for the MailItem object.
However, there _is_ an "Attachments" collection through which you can
access the attachment(s) for the MailItem. (A single mail message can
have more than one attachment.)

So, your reference to the ".Attachment" property won't work. You can
either arbitrarily select the first attachment, something like this
(warning - air code):
Dim oMail As Outlook.MailItem, oAttach As Outlook.Attachment

' ...omitted code to set up object references, open recordset, etc.

If oMail.Attachments.Count 0 Then
Set oAttach = oMail.Attachments(0)
' do something useful
Set oAttach = Nothing
End If
or you could iterate through the Attachments collection, as in
Dim oMail As Outlook.MailItem, oAttach As Outlook.Attachment

' ...omitted code to set up object references, open recordset, etc.

For Each oAttach In oMail.Attachments
' do something useful
Next
Set oAttach = Nothing

Dec 11 '06 #2
Hi Gord,

there is always one attachment with the emails we are processing. The
attachement is a html report which i would either like to open or view from
Access.

Dean

"Gord" <gd*@kingston.netwrote in message
news:11**********************@73g2000cwn.googlegro ups.com...
Dean Spencer wrote:
>Can anyone help? I am importing Emails from Outlook using the following
code:

Public Function ScanInbox(SubjectLine As String)
Dim TempRst As Recordset
Dim OlApp As Outlook.Application
Dim Inbox As Outlook.MAPIFolder
Dim InboxItems As Outlook.Items
Dim Mailobject As Object
Set OlApp = CreateObject("Outlook.Application")
Set Inbox =
OlApp.GetNamespace("Mapi").GetDefaultFolder(olFol derInbox)
Set TempRst = CurrentDb.OpenRecordset("tbl_OutlookTemp")
If SubjectLine <"" Then
Set InboxItems = Inbox.Items.Restrict("[Subject] = """ &
SubjectLine
& """")
Else
Set InboxItems = Inbox.Items
End If
For Each Mailobject In InboxItems
With TempRst
On Error Resume Next
.AddNew
!Subject = Mailobject.Subject
!From = Mailobject.SenderName
!To = Mailobject.To
!Body = Mailobject.Body
!DateSent = Mailobject.SentOn
!Attachment = Mailobject.Attachment
.Update
'Mailobject.Delete
Mailobject.Read
End With
Next

Set OlApp = Nothing
Set Inbox = Nothing
Set InboxItems = Nothing
Set Mailobject = Nothing
Set TempRst = Nothing
End Function
The emails which I would like to import contain an attachment. Does
anyone
know how I can either save the attachement in Access as a memo or OLE
object? or even save a link to open the attachement?

I have tried adding !Attachment = Mailobject.Attachment but nothing is
added
to the temporary table?

Thank you for your help.

Dean Spencer.

Hi, Dean.

Your question is really about the Outlook object model and not about
Access per se. That said, if you check the Object Browser you will see
that there is no "Attachment" property for the MailItem object.
However, there _is_ an "Attachments" collection through which you can
access the attachment(s) for the MailItem. (A single mail message can
have more than one attachment.)

So, your reference to the ".Attachment" property won't work. You can
either arbitrarily select the first attachment, something like this
(warning - air code):
Dim oMail As Outlook.MailItem, oAttach As Outlook.Attachment

' ...omitted code to set up object references, open recordset, etc.

If oMail.Attachments.Count 0 Then
Set oAttach = oMail.Attachments(0)
' do something useful
Set oAttach = Nothing
End If
or you could iterate through the Attachments collection, as in
Dim oMail As Outlook.MailItem, oAttach As Outlook.Attachment

' ...omitted code to set up object references, open recordset, etc.

For Each oAttach In oMail.Attachments
' do something useful
Next
Set oAttach = Nothing

Dec 12 '06 #3

Dean Spencer wrote:
Hi Gord,

there is always one attachment with the emails we are processing. The
attachement is a html report which i would either like to open or view from
Access.

Dean
The following will write the HTML attachment contents (text) to the
memo field called [Attachment]. To view it, you would write come code
to write it back to a temp file and then use FollowHyperlink to open
it.
Public Function ScanInbox(SubjectLine As String)

Const strTempFileSpec = "C:\__tmp\AttachTemp.txt"

Dim TempRst As DAO.Recordset
Dim OlApp As Outlook.Application
Dim Inbox As Outlook.MAPIFolder
Dim InboxItems As Outlook.Items
Dim OlMessage As Outlook.MailItem
Dim OlAttachment As Outlook.Attachment

Dim strmTemp As ADODB.Stream

Set OlApp = New Outlook.Application
Set Inbox =
OlApp.GetNamespace("Mapi").GetDefaultFolder(olFold erInbox)
Set TempRst = CurrentDb.OpenRecordset("tbl_OutlookTemp")

If SubjectLine <"" Then
Set InboxItems = Inbox.Items.Restrict("[Subject] = """ &
SubjectLine & """")
Else
Set InboxItems = Inbox.Items
End If

For Each OlMessage In InboxItems
With TempRst
.AddNew
!Subject = OlMessage.Subject
!From = OlMessage.SenderName
!To = OlMessage.To
!Body = OlMessage.Body
!DateSent = OlMessage.SentOn
If OlMessage.Attachments.Count 0 Then
' collection index starts at 1, not 0
Set OlAttachment = OlMessage.Attachments(1)
OlAttachment.SaveAsFile strTempFileSpec
Set OlAttachment = Nothing
Set strmTemp = New ADODB.Stream
' HTML attachment is text
strmTemp.Type = adTypeText
strmTemp.Charset = "us-ascii"
strmTemp.Open
strmTemp.LoadFromFile strTempFileSpec
strmTemp.Position = 0
' save to memo field in table
!Attachment = strmTemp.ReadText
strmTemp.Close
Set strmTemp = Nothing
Kill strTempFileSpec
End If
.Update
End With
Next

Set OlApp = Nothing
Set Inbox = Nothing
Set InboxItems = Nothing
Set OlMessage = Nothing
Set TempRst = Nothing

MsgBox "Done."
End Function

"Gord" <gd*@kingston.netwrote in message
news:11**********************@73g2000cwn.googlegro ups.com...
Dean Spencer wrote:
Can anyone help? I am importing Emails from Outlook using the following
code:

Public Function ScanInbox(SubjectLine As String)
Dim TempRst As Recordset
Dim OlApp As Outlook.Application
Dim Inbox As Outlook.MAPIFolder
Dim InboxItems As Outlook.Items
Dim Mailobject As Object
Set OlApp = CreateObject("Outlook.Application")
Set Inbox =
OlApp.GetNamespace("Mapi").GetDefaultFolder(olFold erInbox)
Set TempRst = CurrentDb.OpenRecordset("tbl_OutlookTemp")
If SubjectLine <"" Then
Set InboxItems = Inbox.Items.Restrict("[Subject] = """ &
SubjectLine
& """")
Else
Set InboxItems = Inbox.Items
End If
For Each Mailobject In InboxItems
With TempRst
On Error Resume Next
.AddNew
!Subject = Mailobject.Subject
!From = Mailobject.SenderName
!To = Mailobject.To
!Body = Mailobject.Body
!DateSent = Mailobject.SentOn
!Attachment = Mailobject.Attachment
.Update
'Mailobject.Delete
Mailobject.Read
End With
Next

Set OlApp = Nothing
Set Inbox = Nothing
Set InboxItems = Nothing
Set Mailobject = Nothing
Set TempRst = Nothing
End Function
The emails which I would like to import contain an attachment. Does
anyone
know how I can either save the attachement in Access as a memo or OLE
object? or even save a link to open the attachement?

I have tried adding !Attachment = Mailobject.Attachment but nothing is
added
to the temporary table?

Thank you for your help.

Dean Spencer.
Hi, Dean.

Your question is really about the Outlook object model and not about
Access per se. That said, if you check the Object Browser you will see
that there is no "Attachment" property for the MailItem object.
However, there _is_ an "Attachments" collection through which you can
access the attachment(s) for the MailItem. (A single mail message can
have more than one attachment.)

So, your reference to the ".Attachment" property won't work. You can
either arbitrarily select the first attachment, something like this
(warning - air code):
Dim oMail As Outlook.MailItem, oAttach As Outlook.Attachment

' ...omitted code to set up object references, open recordset, etc.

If oMail.Attachments.Count 0 Then
Set oAttach = oMail.Attachments(0)
' do something useful
Set oAttach = Nothing
End If
or you could iterate through the Attachments collection, as in
Dim oMail As Outlook.MailItem, oAttach As Outlook.Attachment

' ...omitted code to set up object references, open recordset, etc.

For Each oAttach In oMail.Attachments
' do something useful
Next
Set oAttach = Nothing
Dec 12 '06 #4
Hi Gord,

I am getting a 'Object Type not defined' for the line 'Dim strmTemp As
ADODB.Stream'

Any ideas?

Dean
"Gord" <gd*@kingston.netwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...
>
Dean Spencer wrote:
>Hi Gord,

there is always one attachment with the emails we are processing. The
attachement is a html report which i would either like to open or view
from
Access.

Dean

The following will write the HTML attachment contents (text) to the
memo field called [Attachment]. To view it, you would write come code
to write it back to a temp file and then use FollowHyperlink to open
it.
Public Function ScanInbox(SubjectLine As String)

Const strTempFileSpec = "C:\__tmp\AttachTemp.txt"

Dim TempRst As DAO.Recordset
Dim OlApp As Outlook.Application
Dim Inbox As Outlook.MAPIFolder
Dim InboxItems As Outlook.Items
Dim OlMessage As Outlook.MailItem
Dim OlAttachment As Outlook.Attachment

Dim strmTemp As ADODB.Stream

Set OlApp = New Outlook.Application
Set Inbox =
OlApp.GetNamespace("Mapi").GetDefaultFolder(olFold erInbox)
Set TempRst = CurrentDb.OpenRecordset("tbl_OutlookTemp")

If SubjectLine <"" Then
Set InboxItems = Inbox.Items.Restrict("[Subject] = """ &
SubjectLine & """")
Else
Set InboxItems = Inbox.Items
End If

For Each OlMessage In InboxItems
With TempRst
.AddNew
!Subject = OlMessage.Subject
!From = OlMessage.SenderName
!To = OlMessage.To
!Body = OlMessage.Body
!DateSent = OlMessage.SentOn
If OlMessage.Attachments.Count 0 Then
' collection index starts at 1, not 0
Set OlAttachment = OlMessage.Attachments(1)
OlAttachment.SaveAsFile strTempFileSpec
Set OlAttachment = Nothing
Set strmTemp = New ADODB.Stream
' HTML attachment is text
strmTemp.Type = adTypeText
strmTemp.Charset = "us-ascii"
strmTemp.Open
strmTemp.LoadFromFile strTempFileSpec
strmTemp.Position = 0
' save to memo field in table
!Attachment = strmTemp.ReadText
strmTemp.Close
Set strmTemp = Nothing
Kill strTempFileSpec
End If
.Update
End With
Next

Set OlApp = Nothing
Set Inbox = Nothing
Set InboxItems = Nothing
Set OlMessage = Nothing
Set TempRst = Nothing

MsgBox "Done."
End Function

>"Gord" <gd*@kingston.netwrote in message
news:11**********************@73g2000cwn.googlegr oups.com...
Dean Spencer wrote:
Can anyone help? I am importing Emails from Outlook using the
following
code:

Public Function ScanInbox(SubjectLine As String)
Dim TempRst As Recordset
Dim OlApp As Outlook.Application
Dim Inbox As Outlook.MAPIFolder
Dim InboxItems As Outlook.Items
Dim Mailobject As Object
Set OlApp = CreateObject("Outlook.Application")
Set Inbox =
OlApp.GetNamespace("Mapi").GetDefaultFolder(olFol derInbox)
Set TempRst = CurrentDb.OpenRecordset("tbl_OutlookTemp")
If SubjectLine <"" Then
Set InboxItems = Inbox.Items.Restrict("[Subject] = """ &
SubjectLine
& """")
Else
Set InboxItems = Inbox.Items
End If
For Each Mailobject In InboxItems
With TempRst
On Error Resume Next
.AddNew
!Subject = Mailobject.Subject
!From = Mailobject.SenderName
!To = Mailobject.To
!Body = Mailobject.Body
!DateSent = Mailobject.SentOn
!Attachment = Mailobject.Attachment
.Update
'Mailobject.Delete
Mailobject.Read
End With
Next

Set OlApp = Nothing
Set Inbox = Nothing
Set InboxItems = Nothing
Set Mailobject = Nothing
Set TempRst = Nothing
End Function
The emails which I would like to import contain an attachment. Does
anyone
know how I can either save the attachement in Access as a memo or OLE
object? or even save a link to open the attachement?

I have tried adding !Attachment = Mailobject.Attachment but nothing is
added
to the temporary table?

Thank you for your help.

Dean Spencer.

Hi, Dean.

Your question is really about the Outlook object model and not about
Access per se. That said, if you check the Object Browser you will see
that there is no "Attachment" property for the MailItem object.
However, there _is_ an "Attachments" collection through which you can
access the attachment(s) for the MailItem. (A single mail message can
have more than one attachment.)

So, your reference to the ".Attachment" property won't work. You can
either arbitrarily select the first attachment, something like this
(warning - air code):
Dim oMail As Outlook.MailItem, oAttach As Outlook.Attachment

' ...omitted code to set up object references, open recordset, etc.

If oMail.Attachments.Count 0 Then
Set oAttach = oMail.Attachments(0)
' do something useful
Set oAttach = Nothing
End If
or you could iterate through the Attachments collection, as in
Dim oMail As Outlook.MailItem, oAttach As Outlook.Attachment

' ...omitted code to set up object references, open recordset, etc.

For Each oAttach In oMail.Attachments
' do something useful
Next
Set oAttach = Nothing

Dec 12 '06 #5

Dean Spencer wrote:
Hi Gord,

I am getting a 'Object Type not defined' for the line 'Dim strmTemp As
ADODB.Stream'

Any ideas?
In the VBA editor choose

Tools References...

and make sure that your ADO reference says "Microsoft ActiveX Data
Objects 2.5 Library" (or some value >= 2.5)
Dean
"Gord" <gd*@kingston.netwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...

Dean Spencer wrote:
Hi Gord,

there is always one attachment with the emails we are processing. The
attachement is a html report which i would either like to open or view
from
Access.

Dean
The following will write the HTML attachment contents (text) to the
memo field called [Attachment]. To view it, you would write come code
to write it back to a temp file and then use FollowHyperlink to open
it.
{snip}

Dec 12 '06 #6
Hi Gord,

The code you have given doesn't seem to work?

I am not getting any errors?

The code doesn't create a .txt file in the specified location, and nothing
is written to the Attachment field?

Are you able to help?

Dean
"Gord" <gd*@kingston.netwrote in message
news:11**********************@79g2000cws.googlegro ups.com...
>
Dean Spencer wrote:
>Hi Gord,

I am getting a 'Object Type not defined' for the line 'Dim strmTemp As
ADODB.Stream'

Any ideas?

In the VBA editor choose

Tools References...

and make sure that your ADO reference says "Microsoft ActiveX Data
Objects 2.5 Library" (or some value >= 2.5)
>Dean
"Gord" <gd*@kingston.netwrote in message
news:11**********************@l12g2000cwl.googleg roups.com...
>
Dean Spencer wrote:
Hi Gord,

there is always one attachment with the emails we are processing. The
attachement is a html report which i would either like to open or view
from
Access.

Dean

The following will write the HTML attachment contents (text) to the
memo field called [Attachment]. To view it, you would write come code
to write it back to a temp file and then use FollowHyperlink to open
it.

{snip}

Dec 14 '06 #7

Dean Spencer wrote:
Hi Gord,

The code you have given doesn't seem to work?
It works for me....
I am not getting any errors?
I noticed that your original code had

On Error Resume Next

at the beginning of the loop. Is it possible that your code is throwing
an error and you are suppressing it?
The code doesn't create a .txt file in the specified location, and nothing
is written to the Attachment field?

Are you able to help?
I don't have much to go on. If the temp file is not being created then
it appears that

OlAttachment.SaveAsFile strTempFileSpec

is failing. If it is, then perhaps it is a difference in behaviour
between different versions of Outlook. (I used Outlook_2000 for
testing.) If you set a breakpoint and step through the code can you
verify that OlAttachment.SaveAsFile is not doing its job?

>
Dean
"Gord" <gd*@kingston.netwrote in message
news:11**********************@79g2000cws.googlegro ups.com...

Dean Spencer wrote:
Hi Gord,

I am getting a 'Object Type not defined' for the line 'Dim strmTemp As
ADODB.Stream'

Any ideas?
In the VBA editor choose

Tools References...

and make sure that your ADO reference says "Microsoft ActiveX Data
Objects 2.5 Library" (or some value >= 2.5)
Dean
"Gord" <gd*@kingston.netwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...

Dean Spencer wrote:
Hi Gord,

there is always one attachment with the emails we are processing. The
attachement is a html report which i would either like to open or view
from
Access.

Dean

The following will write the HTML attachment contents (text) to the
memo field called [Attachment]. To view it, you would write come code
to write it back to a temp file and then use FollowHyperlink to open
it.
{snip}
Dec 14 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Chuck Farah | last post by:
I am unsuccessfullly trying to display an outlook email from a vb web forms (.net) outlook 2002 #1. is it possible to use outlook client email from a web form using the outlook object model...
0
by: VP | last post by:
g'day, i am posting a problem i have encountered with creating an email using outlook through some basic c# code. the problem arises when using different email editors in outlook. At the moment...
4
by: Max | last post by:
Hi, I would like to have a button and a combo box with options to select various versions of Microsoft Outlook: 2002, 2003. The user selects the email client and clicks the button. The only...
9
by: John | last post by:
Hi I am using the following code to search for an email message with id myID; Dim SentFld As Outlook.MAPIFolder Dim Email As Outlook.MailItem Dim I As Integer OutlookApp = New...
1
by: Michele | last post by:
Hi, I need to send the same Email to different people. I'm using Outlook XP and VB.Net. I tryed with the following code: Dim oOutL As Outlook.Application Dim oMail As Outlook._MailItem oOutL...
10
by: John | last post by:
Hi When I open a new outlook email from vb.net, sometimes outlook is very slow to appear or occasionally outlook freezes completely. I am targeting mixed office2000/xp environments so I am...
7
by: Chris Thunell | last post by:
I'm trying to loop through an exchange public folder contact list, get some information out of each item, and then put it into a vb.net datatable. I run though the code and all works fine until i...
2
by: Siv | last post by:
Hi, I have written an application which is used by sales staff when discussing products with their customers over the phone. It is a database application that holds detailed information about...
26
by: Tom Becker | last post by:
Is there a way, from Access, to programmatically click the Send and Receive button in Outlook?
23
by: andyoye | last post by:
How can I launch Outlook on users machines when they click a button on a web form (InfoPath)? Thanks
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.