Finding Outlook Email Address of mailitem via SQL/VBA with Access

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Finding Outlook Email Address of mailitem via SQL/VBA with Access



Using Access/VBA/SQL to get various email properties from the inbox as I transfer it into a new table "MyInbox".


Sub InboxImport()
Dim SqlString As String
Dim ConnectionString As String
Dim EmailTableName As String
Dim UserIdNum As String
Dim EmailAddr As String
Dim olNS As Outlook.NameSpace
Dim olFol As Outlook.Folder

Set ol = CreateObject("Outlook.Application")
Set olNS = ol.GetNamespace("MAPI")
Set olFol = olNS.GetDefaultFolder(olFolderInbox)

EmailTableName = "MyInbox"
UserIdNum = Environ("USERNAME") '1277523A...
EmailAddr = olFol.Parent.Name 'Gives your user email address
ConnectionString = "Outlook 9.0;MAPILEVEL=" & EmailAddr & "|;PROFILE=Default Outlook Profile;TABLETYPE=0;TABLENAME=MyInbox;COLSETVERSION=12.0;DATABASE=C:Users" & UserIdNum & "AppDataLocalTemp"

SqlString = "SELECT [From] As [Sender], [Email] As [Email Addy], [Subject Prefix] & [Normalized Subject] As Subject, [Contents] As [Body], [Received] As [ReceivedTime]" & _
" INTO [MyInbox]" & _
" From [" & ConnectionString & "].[Inbox]"

DoCmd.SetWarnings False
DoCmd.RunSQL SqlString
DoCmd.SetWarnings True
End Sub



I'm trying to find the "Sender Email" address for every email item in the inbox. As run, it currently pops up with a "Enter Parameter..." with a blank value for [Email].



Is there a good compiled reference for looking up all these different kinds of email SQL terms?





What is the issue - error message, wrong result, nothing happens?
– June7
Aug 6 at 18:32





Review stackoverflow.com/questions/34922075/…
– June7
Aug 6 at 18:44





I'm looking for what would replace [Email] in this SQL string. "Sender", "Email Addy", "Subject", "Body", and "ReceivedTime" are my table column names. When I use [Sender Email Address] and run the sub, it gives me a pop-up, 'Enter Parameter... (for Sender Email Address). If I type anything in there, it'll be the value that gets used for ever single table row in that column "Email Addy".
– Jazzmasterflex
Aug 7 at 2:55





Why do you want to create a new table instead of inserting to existing table? I use SenderEmailAddress in different code to pull the email address from each mail item in folder. Unfortunately it is not working with your code.
– June7
Aug 7 at 7:17





As far as I can tell, the folder does not have this property by any name. It is a property of the mail item. Only solution I can see is looping through the mail items and executing SQL for each item. Cannot get SQL referencing folder as a whole to capture that property.
– June7
Aug 7 at 8:04




1 Answer
1



Loop through email items. Unfortunately, the INSERT sql has issue with embedded special characters in the email body, apparently related to hyperlinks. I didn't think it worthwhile figuring out how to get around it.


Public Sub ImportEmails()

' Set up Outlook objects.
Dim ol As New Outlook.Application
Dim of As Outlook.MAPIFolder
Dim objItems As Outlook.Items
Dim mo As Outlook.MailItem, Atmt As Outlook.Attachment
'Set of = ol.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox).Folders("Repairs")
Set of = ol.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox)
Set objItems = of.Items

Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("MyInbox")

For Each mo In objItems

'CurrentDb.Execute "INSERT INTO MyInbox SELECT '" & mo.SenderEmailAddress & "' AS Sender, '" & _
mo.SenderName & "' AS SenderName, '" & mo.Subject & "' AS Subject, '" & _
mo.body & "' AS Body, #" & mo.ReceivedTime & "# AS Received"

rst.AddNew
rst!EmailAdd = mo.SenderEmailAddress
rst!SenderName = mo.Sender
rst!Subject = mo.Subject
rst!body = mo.body
rst!Received = mo.ReceivedTime
rst.Update
'For Each Atmt In mo.Attachments
' Atmt.SaveAsFile "C:path" & Atmt.FileName
'Next

Next
End Sub





With this, I get Run-time error '-2147467259 (80004005)': Method 'SenderEmailAddress' of object'_MailItem' failed (for SenderEmailAddress), Application-defined or object-defined error (for Sender and Body) I think I'm getting stone-walled by my Outlook security settings being on a government computer.
– Jazzmasterflex
Aug 7 at 20:08






Could be because code works flawlessly on my personal laptop.
– June7
Aug 7 at 20:25





and this was the same findings from my previous post. I think I'm outta luck...
– Jazzmasterflex
Aug 7 at 23:27






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard