To resolve this problem, you can use either of the following
methods:
Set the Action Property to acOLEClose After the Object Is Exported
Because the Graph object is contained in an OLE object frame, you
can set the Action property of the object frame to acOLEClose. This closes the OLE server and therefore prevents the error.
Although this prevents the shutdown, the Graph object is not accessible until
you close the form and then you reopen the form.
For example, to
resolve the problem that is mentioned in the "Steps to Reproduce the Behavior"
section, you must set the Action property of the object frame to acOLEClose. To do this, follow these steps:
Start Access.
Open the database where you want to export the Graph
object from.
Open the form that contains the Graph object in Design
view.
Insert code that is similar to the following code for
the Click event of the command button:
Private Sub Command1_Click()
Dim grpApp As Graph.Chart
Set grpApp = Me.Graph1.Object
grpApp.Export "C:\Graph1.jpg", "JPEG"
Set grpApp = Nothing
Me.Graph1.Action = acOLEClose
End Sub
On the File menu, click Close
and Return to Microsoft Office Access.
On the File menu, click
Save.
On the View menu, click Form
View.
Notice that the form opens and that the Graph object
appears.
Click the command button to export the Graph object to
the file that is named C:\Graph1.jpg.
On the View menu, click Design
View.
Notice that the form opens in Design view without any
errors and that the Graph object is successfully exported to JPEG
format.
Create a PivotChart Object Instead of Using Graph
When you use the PivotChart view for forms in Access, you can
programmatically export the chart to a graphics file to avoid this problem. To
create a PivotChart and to programmatically export the chart to a graphics
file, follow these steps:
Start Access.
Open the Northwind.mdb sample database.
On the Insert menu, click
Form.
In the New Form dialog box, click
AutoForm: PivotChart.
In the Choose the table or query where the
object's data comes from list, click Sales by
Category, and then click OK.
Move CategoryName from Chart Field List to Drop Category
Fields Here.
Move ProductSales from Chart Field List to Drop Data
Fields Here.
Notice that the chart that appears shows the
Sum of Product Sales by Category.
On the File menu, click
Close. When you are prompted to save the form, click
Yes. Save the form as frmPivotChart.
On the Insert menu, click
Form.
Click Design View, and then click
OK.
Do not select a table or a query.
Move frmPivotChart from the Database window to the new form.
This creates a
subform.
On the View menu, click
Properties.
Click the subform object.
In the property sheet, click the Other
tab, and then set the Name property to
frmPivotChart.
Add a command button to the main form, and then set the
Name property of the command button to
Command1.
In the Command Button Wizard dialog
box, click Cancel.
On the View menu, click
Code to view the module of the form.
Use the copy and paste feature to move the following
code to the Visual Basic Editor:
Private Sub Command1_Click()
Dim frm As Access.Form
Set frm = Me.frmPivotChart.Form
frm.ChartSpace.ExportPicture "C:\PivotChart1.jpg", "JPEG"
End Sub
On the File menu, click Close
and Return to Microsoft Office Access.
On the File menu, click
Save, and then save the form as
frmMain.
On the View menu, click Form
View.
Notice that the subform appears in PivotChart
view.
Click the command button to export the PivotChart to
the file that is named C:\PivotChart1.jpg.
On the View menu, click Design
View.
Notice that the form opens in Design view without any
errors.
Caution If you follow the steps in this example, you modify the sample
database Northwind.mdb. You may want to back up the Northwind.mdb file and
follow these steps on a copy of the database.
Start Access.
Open the Northwind.mdb sample database.
On the Insert menu, click
Form.
In the New Form dialog box, click
Chart Wizard.
In the Choose the table or query where the object's
data comes from box, click Sales by Category, and
then click OK.
Add the CategoryName field and the
ProductSales field to the Fields for Chart
box, and then click Finish.
Notice that a new form
opens with a Graph object.
On the View menu, click Design
View.
On the View menu, click
Properties.
Click MSGraph to view the properties of
the MSGraph object.
On the Other tab, set the
Name property to Graph1.
Add a command button to the form, and then set the
Name property of the command button to
Command1.
On the View menu, click
Code.
On the Tools menu, click
References.
In the References dialog box, click to
select the check box that is next to Microsoft Graph 11.0 Object
Library.
If you do not see the Microsoft Graph 11.0 Object
Library, click Browse, look for the file in <installation drive>:\Program Files\Microsoft
Office\Office11\Graph.exe, and then click Open.
Click OK to close the
References dialog box.
Use the copy and paste feature to put the following Visual
Basic for Applications code in the module of the form:
Private Sub Command1_Click()
Dim grpApp As Graph.Chart
Set grpApp = Me.Graph1.Object
grpApp.Export "C:\Graph1.jpg", "JPEG"
Set grpApp = Nothing
End Sub
On the File menu, click Close and
Return to Microsoft Office Access.
On the File menu, click
Save. Save the form as Form1.
On the View menu, click Form
View.
Notice that the form opens and that the MSGraph object appears.
Click the command button to export the Graph object to the
file that is named C:\Graph1.jpg.
On the View menu, click Design
View.
Notice that you receive the following error
message:
The operation on the Chart object
failed.
The OLE server may not be registered.
To register the
OLE server, reinstall it.
Click OK.
You may receive the
following error message, and then Access quits unexpectedly:
Microsoft Office Access has encountered a problem and needs to
close. We are sorry for the inconvenience.