Automating WordBasic.InsertChart

P

PromisedOyster

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,
 
J

Jean-Guy Marcil

PromisedOyster was telling us:
PromisedOyster nous racontait que :
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,

Why can't you use

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

How "differently" does it work?

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
P

PromisedOyster

PromisedOysterwas telling us:promisedOysternous racontait que :










Why can't you use

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

How "differently" does it work?

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:http://www.word.mvps.org- Hide quoted text -

- 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.
 
J

Jean-Guy Marcil

PromisedOyster was telling us:
PromisedOyster nous 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
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
P

PromisedOyster

PromisedOysterwas telling us:promisedOysternous racontait que :



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
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
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.
 
J

Jean-Guy Marcil

PromisedOyster was telling us:
PromisedOyster nous racontait que :
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.

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.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top