更新:
以下符合推荐的SQL构造的当前代码:SqlString =中的错误
运行时错误“ 3011”:Microsoft Access数据库引擎找不到对象“。确保对象存在并且您正确拼写了它的名称和路径名。如果“不是本地对象,请检查您的网络连接或与服务器管理员联系。
值得注意的是,我正在使用USAF未分类的网络系统,并通过CAC登录。
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" 'My table name
UserIdNum = Environ("USERNAME") '1277523A... acct #
EmailAddr = olFol.Parent.name 'user's email address
ConnectionString = "Outlook 9.0;MAPILEVEL=" & EmailAddr & "|;PROFILE=Default Outlook Profile;TABLETYPE=0;TABLENAME=MyInbox;COLSETVERSION=12.0;DATABASE=C:\Users\" & UserIdNum & "\AppData\Local\Temp\"
SqlString = "SELECT [From] As [Sender], [Sender Name] As SenderName, [Subject Prefix] & [Normalized Subject] As Subject, [Contents] As [Body], [Received] As [ReceivedTime]" & _
" INTO [Email]" & _
" From [" & ConnectionString & "].[MyInbox]"
DoCmd.RunSQL SqlString
end sub
原始文本:
我正在尝试将默认的Outlook收件箱电子邮件拉到Access中的表中。我能够使用该向导成功检索电子邮件并填充各个列,并通过名为“收件箱”的Access表查看我当前的收件箱。
我的Access数据库将由多个员工同时使用,我不能要求他们为他们登录的每台不同计算机运行向导。
我正在使用从页面中间复制的代码... "Export Outlook Emails to Access table - VBA"。
我正在尝试使用
DoCmd.RunSQL "INSERT INTO [Email] " & _
"([Sender], [SenderName], [Subject], [Body], [ReceivedTime])" & _
"VALUES " & _
"'" & objProp(i).Sender & "', '" & _ 'ERROR!
objProp(i).SenderName & "', " & _ 'ERROR!
objProp(i).Subject & "', '" & _
objProp(i).Body & "', '" & _ 'ERROR!
objProp(i).ReceivedTime & "';"
代码偶然发现除MailItem
或.ReceivedTime
之外的任何.Subject
属性,这些属性引发错误...
运行时错误'287':应用程序定义或对象定义的错误
供我参考-数据库:
答案 0 :(得分:0)
我强烈建议您从Outlook导入邮件时不要采用尝试的方法。 Access可以与SQL查询中的Outlook数据文件一起使用。当然,您可以使用VBA执行这些查询。但这会更加优化。
技巧是获取正确的连接字符串。您可以通过以下过程轻松获得连接字符串:
Debug.Print CurrentDb.TableDefs!MyLinkedOutlookFolder.Connect
获取连接字符串,并使用Debug.Print CurrentDb.TableDefs!MyLinkedOutlookFolder.SourceTableName
获取外部表名称使用获得的变量执行以下查询:
SELECT [From] As [Sender], [Sender Name] As SenderName, [Subject Prefix] & [Normalized Subject] As Subject, [Contents] As [Body], [Received] As [ReceivedTime]
INTO [Email]
FROM [ThatConnectionString].[ThatSourceTableName]
示例连接字符串:
Outlook 9.0;MAPILEVEL=me@example.com|;PROFILE=Default Outlook Profile;TABLETYPE=0;TABLENAME=Inbox;COLSETVERSION=12.0;DATABASE=C:\Users\Me\AppData\Local\Temp\
示例源表名称:
Inbox
这就是您所需要的,不需要复杂的VBA。