Home  |  About  |   Search   

What's New
Table Of Contents
Credits
Netiquette
10 Commandments 
Bugs
Tables
Queries
Forms
Reports
Modules
APIs
Strings
Date/Time
General
Downloads
Resources
Search
Feedback
mvps.org

RunCommand Constants

Terms of Use


 

General: Email/Export Charts from Access

Author(s)
Graeme Wilson
  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.
   Download chartex.zip

To export out the chart as a JPG image, you can use this code.

'******** Code Start *********
' This code was originally written by Graeme Wilson.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Graeme Wilson
'
Private Sub cmdExportGraph_Click()
  '=============================================
  '     Purpose: Export Graph
  '      E-Mail: Graeme.Wilson@nationsbank.co.uk
  '  Programmer: Graeme Wilson
  '=============================================
On Error GoTo cmdExportGraph_Click_Err
    Dim strErrMsg As String 'For Error Handling
    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   ' Export Cancelled
            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
'********* Code End ***********

To send the chart via email with Outlook as the email client, use this code.

'********* Code End ***********
' This code was originally written by Graeme Wilson.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Graeme Wilson
'
Private Sub cmdEmailGraph_Click()
  '=============================================
  '     Purpose: E-Mail Graph
  '      E-Mail: Graeme.Wilson@nationsbank.co.uk
  '  Programmer: Graeme Wilson
  '=============================================
On Error GoTo cmdEmailGraph_Click_Err
    Dim strErrMsg As String 'For Error Handling
    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
'************ Code End *************

1998-2009, Dev Ashish & Arvin Meyer, All rights reserved. Optimized for Microsoft Internet Explorer