Solved

Adding Excel Chart in Word Template and Editing Progamatically

Posted on 2002-03-05
7
247 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
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

821 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