473,465 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 6772
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
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.