Often times, it's necessary to email a Microsoft Graph 97 Chart
from an Access database. To export out the chart from Access, we can use
the Export method of the OleGraph object.
To export out the chart as a JPG image, you can use this code.
Private Sub cmdExportGraph_Click()
On Error GoTo cmdExportGraph_Click_Err
Dim strErrMsg As String
Dim oleGrf As Object
Dim strFileName As String
Dim strFilter As String
Dim lngFlags As Long
Set oleGrf = Me.OLEchart.Object
strFileName = ahtCommonFileOpenSave(Flags:=lngFlags, InitialDir:="C:\", _
Filter:="JPEG Files (*.jpg)", FilterIndex:=1, DefaultExt:="jpg", FileName:="MyGraph", _
DialogTitle:="Save the Graph", OpenFile:=False)
oleGrf.Export FileName:=strFileName
MsgBox vbCrLf & "The Chart " & strFileName & " has been exported", _
vbInformation + vbOKOnly, "Chart Exported :"
cmdExportGraph_Click_Exit:
Set oleGrf = Nothing
Exit Sub
cmdExportGraph_Click_Err:
Select Case Err
Case 1004
Resume cmdExportGraph_Click_Exit
Case Else
strErrMsg = strErrMsg & "Error #: " & Format$(Err.Number) & vbCrLf & vbCrLf
strErrMsg = strErrMsg & "Error Description: " & Err.Description & vbCrLf
MsgBox strErrMsg, vbInformation, "cmdExportGraph_Click"
Resume cmdExportGraph_Click_Exit
End Select
End Sub
To send the chart via email with Outlook as the email
client, use this code.
Private Sub cmdEmailGraph_Click()
On Error GoTo cmdEmailGraph_Click_Err
Dim strErrMsg As String
Dim olApp As New Outlook.Application
Dim olNameSpace As Outlook.NameSpace
Dim olMail As Outlook.MailItem
Dim oleGrf As Object
Dim strFileName As String
Set olNameSpace = olApp.GetNamespace("MAPI")
Set olMail = olApp.CreateItem(olMailItem)
Set oleGrf = Me.OLEchart.Object
strFileName = "c:\temp\Graph.jpg"
oleGrf.Export FileName:=strFileName
With olMail
.To = "Graeme.Wilson@nationsbank.co.uk"
.Subject = "Graph Info " & Format(Now(), "dd mmm yyyy hh:mm")
.Attachments.Add strFileName
.ReadReceiptRequested = False
.Send
End With
Kill strFileName
MsgBox vbCrLf & "Chart has been E-Mailed", _
vbInformation + vbOKOnly, "Chart Exported :"
cmdEmailGraph_Click_Exit:
Set olApp = Nothing
Set olMail = Nothing
Set oleGrf = Nothing
Exit Sub
cmdEmailGraph_Click_Err:
Select Case Err
Case Else
strErrMsg = strErrMsg & "Error #: " & Format$(Err.Number) & vbCrLf & vbCrLf
strErrMsg = strErrMsg & "Error Description: " & Err.Description & vbCrLf
MsgBox strErrMsg, vbInformation, "cmdEmailGraph_Click"
Resume cmdEmailGraph_Click_Exit
End Select
End Sub
|