Solved

Adding Excel Chart in Word Template and Editing Progamatically

Posted on 2002-03-05
7
248 Views
Last Modified: 2012-08-13
I created a template in MS Word.  I also inserted an Excel chart object into the document.

What code do I use to access the Excel chart object using vb and edit the chart with new values?

Is the excel chart object the same as graph9.exe?

Thanks
0
Comment
Question by:dmontgom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 4

Accepted Solution

by:
WolfgangKoenig earned 100 total points
ID: 6844090
Here is the code to access the com chart object. Therefore
you must link the MS Excel 8.0 Object library to your Word
project file !
aXlsChart is then the com object handle to your chart
object! (the code is for office97, when use office2000 then Excel.Chart.9 and link the 9.0 Object Libary ...)


Sub Makro1()
Dim aWrdShape As InlineShape
Dim aXlsWkb As Workbook
Dim aXlsChart As Chart

Set aWrdShape = Selection.InlineShapes.AddOLEObject(ClassType:="Excel.Chart.8", FileName:="", LinkToFile:=False, DisplayAsIcon:=False)
Set aXlsWkb = aWrdShape.OLEFormat.Object
Set aXlsChart = aXlsWkb.Charts(1)
End Sub


;)
WoK
0
 
LVL 4

Expert Comment

by:WolfgangKoenig
ID: 6844179
Here is the complete code with the source assignment:

Sub Makro1()
Dim aWrdShape As InlineShape
Dim aXlsWkb As Workbook
Dim aXlsChart As Chart
Dim aXlsWs As Excel.Worksheet
Dim aRange As Excel.Range
Set aWrdShape = Selection.InlineShapes.AddOLEObject(ClassType:="Excel.Chart.8", FileName:="", LinkToFile:=False, DisplayAsIcon:=False)
Set aXlsWkb = aWrdShape.OLEFormat.Object
' Get Object Handle
Set aXlsChart = aXlsWkb.Charts(1)
Set aXlsWs = aXlsWkb.Worksheets(1)
' Assign Source
Set aRange = aXlsWs.Range("A1:D7")
Call aXlsChart.SetSourceData(aRange)
End Sub


Hope that solves your problem...
;)
WoK
0
 

Author Comment

by:dmontgom
ID: 6845049
Is the excel chart object using graph9.exe?  

Thanks
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:dmontgom
ID: 6845166
Also,

I already have an excel chart object on the word page.  I am not adding a chart programtically and then editing.  What I want to know is with an already existing chart, how do I edit the contents.

Thanks for your help
0
 
LVL 4

Expert Comment

by:WolfgangKoenig
ID: 6846907
Ok, here is the adapted code to change the content
of an existing chart in a word document:

Sub Makro1()
Dim aWrdShape As InlineShape
Dim aXlsWkb As Excel.Workbook
Dim aXlsChart As Excel.Chart
Dim aXlsWs As Excel.Worksheet
Dim aRange As Excel.Range

' Search for an existing chart in word document
For Each aWrdShape In Application.ActiveDocument.InlineShapes
   ' Get the handle to the excel workbook
    Set aXlsWkb = aWrdShape.OLEFormat.Object
   ' Get the handle to the excel worksheet
    Set aXlsWs = aXlsWkb.Worksheets(1)
   ' Get the handle to the excel chart in the worksheet
    Set aXlsChart = aXlsWs.ChartObjects(1)
   
    ' Assign new contents via source method
    Set aRange = aXlsWs.Range("A1:D7")
    Call aXlsChart.SetSourceData(aRange)
Next
End Sub


Beside:
The Range("A1:D7") holds data like this:
       food fuel hotel
jan    12   10   17
feb    17   11   21
march  ...
april
may
june


Hope that is what you need... When you need to modify
the content not programtically then just double click
on the chart and then right click (context menu) and then
choose 'data source' ...

;)
WoK
0
 
LVL 44

Expert Comment

by:bruintje
ID: 6914628
Hi dmontgom, any update on this question?

in an effort to clean up old open questions

your options are:

1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
  --> Post comments for expert of your intention to delete and why
  --> You cannot delete a question with comments, special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below, include the question
QID/link. http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

Please click this Help Desk link for Member Guidelines, Member Agreement and the Question/Answer process: Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues. http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

------------>  EXPERTS:    Please leave any comments regarding this question here on closing recommendations if this item remains inactive another seven (7) days.

:O)Bruintje
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6974197
Per recommendation, force-accepted by
Netminder
CS Moderator
0

Featured Post

RoboForm Secure Password Management System

RoboForm Everywhere - Superb Browser Support
Windows / Apple / IOS / Android / Linux / Chrome OS
Use different complex passwords everywhere
Best Secure Password Management by far
Synchronize all of your devices instantly
Safe, Secure & Highly Recommended!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you how to use shortcut menus in the Access run-time environment.
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question