Email 使用SendObject从Access发送Excel对象,电子邮件中没有附件
亲爱的飞越者: 我正在尝试使用VBA中的SendObject从Access发送Excel对象 我有生成图形的代码:Email 使用SendObject从Access发送Excel对象,电子邮件中没有附件,email,ms-access,vba,ms-access-2007,excel-2007,Email,Ms Access,Vba,Ms Access 2007,Excel 2007,亲爱的飞越者: 我正在尝试使用VBA中的SendObject从Access发送Excel对象 我有生成图形的代码: Dim oXL As Object ' Excel application Dim oBook As Object ' Excel workbook Dim oSheet As Object ' Excel Worksheet Dim oChart As Object ' Excel Chart C
Dim oXL As Object ' Excel application
Dim oBook As Object ' Excel workbook
Dim oSheet As Object ' Excel Worksheet
Dim oChart As Object ' Excel Chart
Const cNumCols = 100 ' Number of points in each Series
Const cNumRows = 26 ' Number of Series
ReDim aTemp(1 To cNumRows, 1 To cNumCols)
Set oXL = CreateObject("Excel.application")
Set oBook = oXL.Workbooks.Add
Set oSheet = oBook.Worksheets.Item(1)
Dim rs01 As DAO.Recordset
Set rs01 = CurrentDb.OpenRecordset("SELECT * FROM qryWOperweekCombined")
Dim Teller As Integer
Teller = 0
Dim iRow As Integer
iRow = 1
Dim iCol As Integer
iCol = 5
With rs01
If .RecordCount > 0 Then
.MoveLast
TotRecords = .RecordCount
.MoveFirst
For Teller = 1 To TotRecords
aTemp(iRow, 1) = !Week
aTemp(iRow, 2) = !Total
aTemp(iRow, 3) = !companyk
aTemp(iRow, 4) = !companyv
.MoveNext
iRow = iRow + 1
Next Teller
oSheet.Range("A1").Resize(cNumRows, cNumCols).Value = aTemp
End If
End With
Set oChart = oSheet.ChartObjects.Add(200, 1, 745, 380).Chart
oChart.SetSourceData Source:=oSheet.Range("A1:D26")
oXL.Visible = True
oChart.HasLegend = True
oChart.HasTitle = True
oChart.SeriesCollection(4).ApplyDataLabels
oChart.SeriesCollection(4).DataLabels.Format.TextFrame2.TextRange.Font.Size = 7
oChart.SeriesCollection(2).ApplyDataLabels
oChart.SeriesCollection(2).DataLabels.Format.TextFrame2.TextRange.Font.Size = 7
oChart.SeriesCollection(3).ApplyDataLabels
oChart.SeriesCollection(3).DataLabels.Format.TextFrame2.TextRange.Font.Size = 7
oSheet.Columns("A:A").ColumnWidth = 18.71
oChart.SeriesCollection(1).Name = "=""Dates"""
oChart.SeriesCollection(1).XValues = "=Sheet1!$A:$A"
oChart.SeriesCollection(2).Name = "=""Total"""
oChart.SeriesCollection(2).XValues = "=Sheet1!$B:$B"
oChart.SeriesCollection(3).Name = "=""companyk"""
oChart.SeriesCollection(3).XValues = "=Sheet1!$C:$C"
oChart.SeriesCollection(4).Name = "=""companyv"""
oChart.SeriesCollection(4).XValues = "=Sheet1!$D:$D"
oChart.SeriesCollection(1).Delete
oChart.SeriesCollection(1).Name = "=""Total"""
oChart.SeriesCollection(1).XValues = "=Sheet1!$A:$A"
oChart.SeriesCollection(1).Interior.Color = vbBlue
oChart.SeriesCollection(2).Interior.Color = vbGreen
oChart.SeriesCollection(3).Interior.Color = vbRed
oChart.SeriesCollection(1).Trendlines.Add
oChart.SeriesCollection(2).Trendlines.Add
oChart.SeriesCollection(3).Trendlines.Add
oChart.SeriesCollection(1).Trendlines.Add(Type:=xlMovingAvg, Period:= _
2, Forward:=1, Backward:=0, DisplayEquation:=False, DisplayRSquared:= _
False, Name:="Average").Select
oChart.SeriesCollection(2).Trendlines.Add(Type:=xlMovingAvg, Period:= _
2, Forward:=1, Backward:=0, DisplayEquation:=False, DisplayRSquared:= _
False, Name:="Average").Select
oChart.SeriesCollection(3).Trendlines.Add(Type:=xlMovingAvg, Period:= _
2, Forward:=1, Backward:=0, DisplayEquation:=False, DisplayRSquared:= _
False, Name:="Average").Select
With oChart.SeriesCollection(1).Trendlines(1).Border
.ColorIndex = 5
.Weight = xlThick
.LineStyle = xlContinuous
End With
With oChart.SeriesCollection(1).Trendlines(2).Border
.ColorIndex = 5
.Weight = xlMedium
.LineStyle = xlContinuous
End With
With oChart.SeriesCollection(2).Trendlines(1).Border
.ColorIndex = 4
.Weight = xlThick
.LineStyle = xlContinuous
End With
With oChart.SeriesCollection(2).Trendlines(2).Border
.ColorIndex = 4
.Weight = xlMedium
.LineStyle = xlContinuous
End With
With oChart.SeriesCollection(3).Trendlines(1).Border
.ColorIndex = 3
.Weight = xlThick
.LineStyle = xlContinuous
End With
With oChart.SeriesCollection(3).Trendlines(2).Border
.ColorIndex = 3
.Weight = xlMedium
.LineStyle = xlContinuous
End With
oChart.Legend.Position = xlBottom
oChart.SetElement (msoElementChartTitleCenteredOverlay)
oChart.HasTitle = True
oChart.ChartTitle.Text = "Workorders per week - last 26 weeks"
oSheet.Visible = True
oXL.UserControl = True
我有一个发送电子邮件的代码:
Dim varName As Variant
Dim varCC As Variant
Dim varSubject As Variant
Dim varBody As Variant
varName = "name@server.com"
varCC = "name2@server2.com"
varSubject = "Hello"
varBody = "Text bla bla bla"
DoCmd.SendObject , oXL, acFormatXLS, varName, varCC, , varSubject, varBody, False, False
当我把这些结合在一起时,我得到了一个代码,它确实发送了一封电子邮件,但是它没有excel附件,而且它仍然在打开excel,但我稍后会解决这个问题
这两个代码组合在一个子代码中
知道为什么不发送附件吗?我是否使用了错误的对象名称oXL,因为oSheet和oBook不工作。或者它可能与excel仍在打开有关吗?SendObject用于Ms Access对象,如查询、表单或报表
如果将前两个参数留空,还可以使用它发送不带附件的电子邮件
您正在发送命令忽略的第二个参数中的Excel对象
相反,您应该使用Outlook对象通过邮件将Excel文件作为附件发送
或者,您也可以使用ShellExecute非常感谢您的帮助。为我工作的示例测试代码是:
Dim olApp As Outlook.Application
Dim objMail As Outlook.MailItem
Set olApp = Outlook.Application
'Create e-mail item'
Set objMail = olApp.CreateItem(olMailItem)
Set objAttachments = objMail.Attachments
With objMail
.Subject = "Weekly Rapport"
.Body = "Hi xyz, here is your Weekly Rapport"
.Recipients.Add "xyz@abc.com"
.Recipients.ResolveAll
.Display
End With
objAttachments.Add "C:\Users\USERNAME\Documents\graphs\Test123.xls", olByValue, 1, "Test123"
'objMail.Display
objMail.Send
您好,谢谢您的建议。ShellExecute希望我在发送文件之前先保存该文件。我得弄清楚那是怎么回事。如果我错了,请纠正我,但我读到SendObject是用于发送vba中创建的对象的命令。这就是我的代码所做的。它创建一个Excel图形,并在Excel中打开它,而不首先保存它。还有其他方法从我的代码开始吗?SendObject只发送内置对象,就像我在回答中写的那样。你必须将Excel文件保存在某个地方,然后你可以使用我在回答中显示的方法之一将其作为附件发送。啊,好的,我将尝试找出如何将其作为文件进行安全保护,然后tyvm:在它工作后,我会给你接受的awnser;要另存为文件:oBook.SaveAs C:\Temp\ExcelFileForAttachment.xli以awnser响应,因为我的帖子需要太多空间。抱歉