Adding charts in Word without Excel popping up  
Author Message
Jay-P





PostPosted: Visual Studio Tools for Office, Adding charts in Word without Excel popping up Top

Hi,

What is the best way to programatically add a chart to word, without userinput. Im trying to write a function in Visual Studio that pulls some data and creates charts from it in word. I managed to add a chart but whenever i do, it opens up excel for specifying the series data, wich i dont want. I simply want to fill the data and formatting options trough code without the user ever noticing it. Should i create an instance of excel and fill/format my chart in there and when im done, copy it to word

Regards, Jape.

Dim Ishape As Word.InlineShape

Dim ImpRange As Word.Range = Globals.ThisAddIn.Application.Selection.Range

Ishape = Globals.ThisAddIn.Application.ActiveDocument.InlineShapes.AddChart(Microsoft.Office.Core.XlChartType.xlBarStacked, impRange)



Visual Studio Tools for Office18  
 
 
Jay-P





PostPosted: Visual Studio Tools for Office, Adding charts in Word without Excel popping up Top

I've managed to get it to work trough an instance of excel, but how do i get it copied as an chart object instead of a picture. The Chart.copy method only copies to a different worksheet in excel. Am i missing something here

Dim exApp As Interop.Excel.Application

Dim exBook As Interop.Excel.Workbook

Dim exSheet As Interop.Excel.Worksheet

Dim Series() As Byte = {13, 53}

exApp = New Interop.Excel.Application

exApp.Visible = True

exBook = exApp.Workbooks.Add

exSheet = exBook.ActiveSheet

exSheet.Range("A1").Value = "Fitness"

exSheet.Range("A2").Value = 15

exSheet.Range("B2").Value = 56

Dim exChart As Interop.Excel.ChartObject = exSheet.ChartObjects.add(160, 40, 30, 70)

exChart.Chart.ApplyChartTemplate("C:\Temp\Chart1.crtx")

exSheet.ChartObjects(1).Activate()

exApp.ActiveChart.SeriesCollection.add(Source:=exSheet.Range("A1"))

exApp.ActiveChart.SeriesCollection(1).Values = exSheet.Range("A2").Value

exApp.ActiveChart.SeriesCollection.add(Source:=exSheet.Range("A1"))

exApp.ActiveChart.SeriesCollection(2).Values = exSheet.Range("B2").Value

exApp.ActiveChart.CopyPicture(Interop.Excel.XlPictureAppearance.xlScreen, Interop.Excel.XlCopyPictureFormat.xlPicture)

Globals.ThisAddIn.Application.Selection.Paste()


 
 
Cindy Meister





PostPosted: Visual Studio Tools for Office, Adding charts in Word without Excel popping up Top

Hi Jay

As this question is not VSTO-technology-related, it's off-topic in this forum. The best place to ask this would be the office.developer.automation newsgroup, as stated in the "Please Read First" message at the top of this forum.

You need to think very carefully about what applications will be available on the clients' machines. You have basically three approaches:

1. MS Graph. You can automate MS Graph as an embedded object in Word, but the user will see the interface, same as you've seen with Excel. This is how object linking and embedding, with in-place activiatation, is designed to function.

2. Create the embedded object in Word, as you've already tried.

3. Create the chart in Excel, then import it into Word as an object. In this case, I would not use copy/paste, for the usual reasons. What you can do is save the workbook to disk, then insert the object from the file into Word. Word has an Insert/Object/From File command for this.

Personally, however, I prefer to create a LINK field to bring in the object, then unlink the field if I don't want the link. I find this less error-prone than the other method.