Vba 使用基于表单中记录集的access向电子邮件添加附件
对不起,我是新来的。我已经花了好几天的时间尝试合并我当前的代码,它可以根据表单中的信息发送电子邮件。就像一个“发送电子邮件”按钮。它以正确的信息作为前缀,但是,我的记录集包含的附件似乎无法正确编码。在过去的5天里,我一直在阅读附件代码,我似乎不明白如何将其纳入我当前的代码中。我只想在我的电子邮件中包括我的记录中保存的附件。这是我当前的代码,有人能告诉我如何在其中包含附件代码吗?提前非常感谢你Vba 使用基于表单中记录集的access向电子邮件添加附件,vba,email,outlook,Vba,Email,Outlook,对不起,我是新来的。我已经花了好几天的时间尝试合并我当前的代码,它可以根据表单中的信息发送电子邮件。就像一个“发送电子邮件”按钮。它以正确的信息作为前缀,但是,我的记录集包含的附件似乎无法正确编码。在过去的5天里,我一直在阅读附件代码,我似乎不明白如何将其纳入我当前的代码中。我只想在我的电子邮件中包括我的记录中保存的附件。这是我当前的代码,有人能告诉我如何在其中包含附件代码吗?提前非常感谢你 Private Sub btnEmail_Click() Dim objOutlo
Private Sub btnEmail_Click()
Dim objOutlook As Object
Dim objOutlookMsg As Object
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment
' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")
' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
' Add the To recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add("Nancy Davolio")
objOutlookRecip.Type = olTo
' Add the CC recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add("Michael Suyama")
objOutlookRecip.Type = olCC
' Add the BCC recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add("Andrew Fuller")
objOutlookRecip.Type = olBCC
' Set the Subject, Body, and Importance of the message.
.Subject = "This is an Automation test with Microsoft Outlook"
.Body = "This is the body of the message."
.Importance = olImportanceHigh 'High importance
' Resolve each Recipient's name.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next
' Should we display the message before sending?
.Display
' .Save
' .Send
End With
Set objOutlook = Nothing
End Sub
Function SaveAttachment()
Dim db As DAO.Database
Dim rst As DAO.Recordset2
Dim rstAttachment As DAO.Recordset2
Dim fld As DAO.Field2
Dim strPath As String
Dim intz As Integer
Set db = CurrentDb
Set rst = db.OpenRecordset("site inspections table", dbOpenDynaset)
rst.FindFirst "ID = " & Me!ID
Set rstAttachment = rst.Fields("Photos").Value
Set fld = rstAttachment.Fields("Filedata")
strPath = CurrentProject.Path & "\Attach\" _
& rstAttachment.Fields("Filename")
On Error Resume Next
Kill strPath & "\Attach\"
On Error GoTo 0
fld.SaveToFile strPath
rstAttachment.Close
rst.Close
Set rstAttachment = Nothing
Set rst = Nothing
Set db = Nothing
End Function
Private Sub cmdEmail_Click()
Dim outlookApp As Outlook.Application
Dim outlookNamespace As NameSpace
Dim objMailItem As MailItem
Dim objFolder As MAPIFolder
Dim strAttachementPath As String
Dim rst As DAO.Recordset2
Dim rstAttachment As DAO.Recordset2
Dim db As DAO.Database
Dim strHTML
'Call SaveAttachment
Set outlookApp = CreateObject("Outlook.Application")
Set outlookNamespace = outlookApp.GetNamespace("mapi")
Set objFolder = outlookNamespace.GetDefaultFolder(olFolderInbox)
Set objMailItem = objFolder.Items.Add(olMailItem)
Set db = CurrentDb
Set rst = db.OpenRecordset("site inspections table", dbOpenDynaset)
rst.FindFirst "ID = " & Me!ID
Set rstAttachment = rst.Fields("Photos").Value
'strAttachementPath = CurrentProject.Path & "\Attach\" _
' & rstAttachment.Fields("Filename")
' Build HTML for message body.
strHTML = "<HTML><HEAD>"
strHTML = "<br>"
strHTML = strHTML & "<FONT Face=Calibri><b>ID: </b></br>" & [ID] & "<br>"
strHTML = strHTML & "<FONT Face=Calibri><b>Date: </b></br>" & [Date] & "<br>"
strHTML = strHTML & "<FONT Face=Calibri><b>Time: </b></br>" & [Time] & "<br>"
strHTML = strHTML & "<FONT Face=Calibri><b>Technician: </b></br>" & [Technician] & "<br>"
strHTML = strHTML & "<FONT Face=Calibri><b>Area: </b></br>" & [Area] & "<br>"
strHTML = strHTML & "<FONT Face=Calibri><b>Blast No.: </b></br>" & [shot number] & "<br><br>"
strHTML = strHTML & "<FONT Face=Calibri><b>Comments: </b></br>" & [Comments] & "<br>"
strHTML = strHTML & "</FONT></br><BODY>"
'strHTML = strHTML & "<FONT Face=Arial Color=#ff0000 Size=5>Job #: 123456</FONT></br>"
'strHTML = strHTML & "<FONT Size=3>For: <FONT Size=2></B>a name here</br>"
'strHTML = strHTML & "<FONT Size=3><B>Description: </B><FONT Size=2>description of work to be
strHTML = strHTML & "</BODY></HTML>"
' Build the Email to be sent
With objMailItem
.BodyFormat = olFormatHTML
.To = "EMAIL ADDRESS HERE"
.Subject = "Site Inspection for " & [Area] & " At " & [Date]
' .Body = "Some text here"
.HTMLBody = strHTML
' Grab Attachments for Email if there are any
If rstAttachment.RecordCount > 0 Then
Call SaveAttachment
strAttachementPath = CurrentProject.Path & "\Attach\" _
& rstAttachment.Fields("Filename")
.Attachments.Add (strAttachementPath)
End If
.Display
End With
outlookApp.ActiveWindow
'SendKeys ("%s")
MsgBox "Mail Sent!", vbOKOnly, "Mail Sent"
Private Sub btnEmail\u Click()
将对象视为对象
Dim objOutlookMsg作为对象
作为Outlook.Recipient的Dim objOutlookRecip
Dim objOutlookAttach作为Outlook.Attachment
'创建Outlook会话。
设置objOutlook=CreateObject(“Outlook.Application”)
'创建消息。
设置objOutlookMsg=objOutlook.CreateItem(olMailItem)
带objOutlookMsg
'将收件人添加到邮件中。
设置objOutlookRecip=.Recipients.Add(“Nancy Davolio”)
objOutlookRecip.Type=olTo
'将抄送收件人添加到邮件中。
Set objOutlookRecip=.Recipients.Add(“Michael Suyama”)
objOutlookRecip.Type=olCC
'将密件抄送收件人添加到邮件中。
设置objOutlookRecip=.Recipients.Add(“Andrew Fuller”)
objOutlookRecip.Type=olBCC
'设置消息的主题、正文和重要性。
.Subject=“这是使用Microsoft Outlook进行的自动化测试”
.Body=“这是邮件的正文。”
.重要性=低重要性高“高重要性”
'解析每个收件人的名称。
对于收件人中的每个ObjectLookRecip
解决问题
下一个
'发送前是否应显示消息?
.展示
"救命!
’发送
以
设置objOutlook=Nothing
端接头
函数SaveAttachment()
Dim数据库作为DAO.Database
将rst设置为DAO.Recordset2
Dim R状态为DAO.Recordset2
将fld设置为DAO.Field2
将strPath设置为字符串
作为整数的Dim intz
Set db=CurrentDb
Set rst=db.OpenRecordset(“现场检查表”,dbOpenDynaset)
rst.FindFirst“ID=”和我!身份证件
设置rstatachment=rst.Fields(“照片”)值
Set fld=rstatachment.Fields(“文件数据”)
strPath=CurrentProject.Path&“\Attach\”_
&rstatachment.Fields(“文件名”)
出错时继续下一步
终止strPath&“\Attach\”
错误转到0
fld.SaveToFile strPath
RStatachment,关闭
rst.关闭
设置rstatachment=Nothing
设置rst=无
Set db=Nothing
端函数
专用子cmdEmail_Click()
Dim outlookApp作为Outlook.Application
Dim outlookNamespace作为命名空间
Dim objMailItem作为MailItem
Dim objFolder作为MAPIFolder
Dim StratachementPath作为字符串
将rst设置为DAO.Recordset2
Dim R状态为DAO.Recordset2
Dim数据库作为DAO.Database
Dim strHTML
'调用SaveAttachment
设置outlookApp=CreateObject(“Outlook.Application”)
设置outlookNamespace=outlookApp.GetNamespace(“mapi”)
设置objFolder=outlookNamespace.GetDefaultFolder(olFolderInbox)
设置objMailItem=objFolder.Items.Add(olMailItem)
Set db=CurrentDb
Set rst=db.OpenRecordset(“现场检查表”,dbOpenDynaset)
rst.FindFirst“ID=”和我!身份证件
设置rstatachment=rst.Fields(“照片”)值
'strAttachementPath=CurrentProject.Path&“\Attach\”_
“&rstatachment.Fields(“文件名”)
'为邮件正文生成HTML。
strHTML=“”
strHTML=“
”
strHTML=strHTML&“ID:”&[ID]&“
”
strHTML=strHTML&“日期:”&[Date]&“
”
strHTML=strHTML&“时间:”&[Time]&“
”
strHTML=strHTML&“技术员:”和[技术员]&“
”
strHTML=strHTML&“区域:”&[Area]&“
”
strHTML=strHTML&“爆炸编号:”&[爆炸编号]&“
”
strHTML=strHTML&“注释:”&[Comments]&“
”
strHTML=strHTML&“”
'strHTML=strHTML&“作业#:123456”
'strHTML=strHTML&“For:此处的名称”
“strHTML=strHTML&”说明:待完成工作的说明
strHTML=strHTML&“
'生成要发送的电子邮件
带有objMailItem
.BodyFormat=olFormatHTML
.To=“此处的电子邮件地址”
.Subject=“在”&[日期]对“&[区域]&”进行现场检查
'.Body=“此处有一些文本”
.HTMLBody=strHTML
'抓取电子邮件附件(如果有)
如果rstatachment.RecordCount>0,则
呼叫保存附件
strAttachementPath=CurrentProject.Path&“\Attach\”_
&rstatachment.Fields(“文件名”)
.Attachments.Add(StrattachmentPath)
如果结束
.展示
以
outlookApp.ActiveWindow
'发送键(“%s”)
MsgBox“邮件已发送!,vBookOnly,“已发送邮件”
这就是所需要的。你有什么错误?请提供你试图合并的附件代码。我有一些错误代码,比如来自callAttachment行的编译错误。但老实说,我尝试的代码是我从google找到的。我现在不在我的笔记本电脑附近,我很高兴能得到我正在努力工作的代码ther tmr,我出于沮丧删除了它。我希望有人能提供给我添加到现有代码中的代码,我必须说明添加附件的原因。我该怎么做?再做一次,然后粘贴我使用的代码和错误,这可能更有意义。谢谢伙计们,我会把它拿回来的tmrOh谢谢你!但是我该怎么做呢因此,它将加载链接到保存在tblCustomers中的记录集的附件,我已将重点放在我的表单上。您需要将blob保存到一个临时文件中,调用Attachments。添加、删除临时文件。access中保存的附件的过程也需要
objOutlookMsg.Attachments.Add("c:\temp\MyTestFile.txt")