Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / April 2007

Tip: Looking for answers? Try searching our database.

Automating WordBasic.InsertChart

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
PromisedOyster - 30 Mar 2007 08:19 GMT
When I run a Word macro, I see the following code in my macro:

WordBasic.InsertChart

However, there is no such method as

_wordApp.WordBasic.InsertChart(); // from C#

Is it possible to automate the calling of the method InsertChart from
C#.

Note that the _wordApp.Selection.InlineShapes.AddOLEObject works
slightly differently.

Ideally, what I want to do (in code) is select a table and convert
this table to a chart,
Jean-Guy Marcil - 30 Mar 2007 22:46 GMT
PromisedOyster was telling us:
PromisedOyster nous racontait que :

> When I run a Word macro, I see the following code in my macro:
>
[quoted text clipped - 12 lines]
> Ideally, what I want to do (in code) is select a table and convert
> this table to a chart,

Why can't you use

   Selection.InlineShapes.AddOLEObject ClassType:="MSGraph.Chart.8",
FileName _
       :="", LinkToFile:=False, DisplayAsIcon:=False
???

How "differently" does it work?

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

PromisedOyster - 02 Apr 2007 00:43 GMT
> PromisedOysterwas telling us:PromisedOysternous racontait que :
>
[quoted text clipped - 33 lines]
>
> - Show quoted text -

If you select a table and then execute the above AddOLEObject, then
the chart gets inserted into the first cell of the table. I was hoping
that it would use the table to populate the chart.
Jean-Guy Marcil - 03 Apr 2007 21:31 GMT
PromisedOyster was telling us:
PromisedOyster nous racontait que :

>> PromisedOysterwas telling us:PromisedOysternous racontait que :
>
> If you select a table and then execute the above AddOLEObject, then
> the chart gets inserted into the first cell of the table. I was hoping
> that it would use the table to populate the chart.

Again one of those things that works when you use the macro recorder, but
then if you run the recorded macro in a perfectly similar situation, it
doesn't work anymore.

I guess you are going to have to do it the "hard way"...
Something like this in "pseudo code":

Declare a range (For the table)
Declare another range for the chart position
Declare an OLE object (For the graph)
Set the range to the target table
Set the range for the place where you want the chart to be
Set the OLE object
Use the table range to populate the OLE object data sheet
Delete the table

This would give something like:
(Remember to set  a reference to the Microsoft Graph library)

'_______________________________________
Sub ConvertTableToChart()

Dim rgeInsertChart As Range
Dim rgeTable As Range
Dim oShape As InlineShape
Dim oChart As Graph.Chart
Dim i As Long
Dim j As Long

With Selection
   If Not .Information(wdWithInTable) Then
       MsgBox "You must position the cursor inside a table.", _
           vbExclamation, "Invalid selection"
       Exit Sub
   End If

   'If table is right at beginning of document, error will occur here _
       because rgeInsertChart will equal "Nothing" _
       You will need code to check for that
   Set rgeInsertChart = .Tables(1).Range.Characters.First.Previous
   Set rgeTable = .Tables(1).Range

   rgeTable.Select
   Set oShape =
Selection.InlineShapes.AddOLEObject(ClassType:="MSGraph.Chart.8", _
       FileName:="", LinkToFile:=False, DisplayAsIcon:=False,
Range:=rgeInsertChart)
End With

oShape.OLEFormat.Activate
Set oChart = oShape.OLEFormat.Object

With rgeTable.Tables(1)
   For i = 1 To .Columns.Count
       For j = 1 To .Rows.Count
       'The following allows you to manipulate the data
       oChart.Application.DataSheet.Cells(j, i).Value = _
           Left(.Cell(j, i).Range.Text, Len(.Cell(j, i).Range.Text) - 2)
       Next
   Next
End With

rgeTable.Tables(1).Delete

'The only way I kow of moving the focus off the chart... pretty lame, _
   I know, but there does not seem to be any other way around that
SendKeys "{ESC}"
Selection.Collapse wdCollapseEnd

End Sub
'_______________________________________

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

PromisedOyster - 04 Apr 2007 06:21 GMT
> PromisedOysterwas telling us:PromisedOysternous racontait que :
>
[quoted text clipped - 83 lines]
> jmarcilREM...@CAPSsympatico.caTHISTOO
> Word MVP site:http://www.word.mvps.org

Thanks Jean

I figured out my initial problem ie not being able to access the
DataSheet property. (I simply needed to call the Activate() method)

Therefore, this solution is now redundant. Thank-you for your input
though.
Jean-Guy Marcil - 04 Apr 2007 20:47 GMT
PromisedOyster was telling us:
PromisedOyster nous racontait que :

>> PromisedOysterwas telling us:PromisedOysternous racontait que :
>>
[quoted text clipped - 92 lines]
> Therefore, this solution is now redundant. Thank-you for your input
> though.

It would have been nice if you had posted a message stating that you had
found the solution and what it was...
This way we, readers, would not end up spending time trying to help when it
is not necessary...

I am glad you sorted it out.

Cheers.

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.