none
Chart in Word document-level customization RRS feed

  • Question

  • Hi all!

    Im writing MS Word document-level customization for statistic purpose. It works pretty well. In designer i create design of whole document with empty tables and bookmarks and then i fill it. Now i want to add a chart, but i dont know how. When i try to use table-like code,

    Word.Table table1 = this.Tables[1];
    

    it dont work besause collection of charts in "this" dont exist. Thank for any help.

     

    Regard Ondro


    Friday, August 19, 2011 8:49 AM

Answers

  • <<Now the code is working but i don't know how to edit excel sheet, never did that before>>

    Sub Chart2007()
        Dim ils As word.InlineShape
        Dim c As word.Chart
        Dim wb As Workbook
        Dim ws As Excel.Worksheet
        Dim lo As Excel.ListObject
       
        Set ils = ActiveDocument.InlineShapes.AddChart(xlColumnStacked, Selection.Range)
        Set c = ils.Chart
        Set wb = c.ChartData.Workbook
        Set ws = wb.Worksheets(1)
        Set lo = ws.ListObjects(1)
        lo.Resize Range("A1:D7")
    End Sub


    Cindy Meister, VSTO/Word MVP
    Saturday, August 20, 2011 11:26 AM
    Moderator

All replies

  • Which version AND SP of Word?


    Cindy Meister, VSTO/Word MVP
    Friday, August 19, 2011 11:08 AM
    Moderator
  • Thanks for reply.

    Im using MS word 2010

    14.0.4734.1000 (32 bit)

    I think there is no SP installed.

     

    Regard Ondro



    Friday, August 19, 2011 11:16 AM
  • Hi Ondro

    Your starting point is here:

    Word.InlineShape ils = wordDoc.InlineShapes.AddChart(//params here);
    WordChart c = ils.Chart;

    This is going to call up the Excel charting engine, but you can use it through the Word object model. From the point where you have c, work as you would to manipulate a chart in excel, including all the XL-Enums.


    Cindy Meister, VSTO/Word MVP
    Friday, August 19, 2011 4:50 PM
    Moderator
  • Thanks for reply.

     

    I for two problems implementing that code.

     

    The first, bigger is that WordChart interface is not recognized by my MS VS, probably im missing some using statement or references.

    The second problem is, that i don't know how to set up second parameter of InlineShapes.AddChart, the range. Can you help me pls how to set up range if i want to place the chart to the second page after heading.

     

     

    Thanks for any help,

    Regard Ondro

     

    edit: First problem fixed its Word.Chart of course, my bad.

    Now the code is working but i don't know how to edit excel sheet, never did that before


    Friday, August 19, 2011 5:39 PM
  • <<Now the code is working but i don't know how to edit excel sheet, never did that before>>

    Sub Chart2007()
        Dim ils As word.InlineShape
        Dim c As word.Chart
        Dim wb As Workbook
        Dim ws As Excel.Worksheet
        Dim lo As Excel.ListObject
       
        Set ils = ActiveDocument.InlineShapes.AddChart(xlColumnStacked, Selection.Range)
        Set c = ils.Chart
        Set wb = c.ChartData.Workbook
        Set ws = wb.Worksheets(1)
        Set lo = ws.ListObjects(1)
        lo.Resize Range("A1:D7")
    End Sub


    Cindy Meister, VSTO/Word MVP
    Saturday, August 20, 2011 11:26 AM
    Moderator
  • Thanks for reply,

     

    and thanks for help, i don't know how to write applications in vb, but i tried convert that code to c sharp, and it works!!!

    The last thing i don't know how to set up is the range, in which is chart generated.

    my actual code is: 

    private void Chart(string name,double[] input, Microsoft.Office.Core.XlChartType type)
    		{
    			Word.InlineShape ils = this.InlineShapes.AddChart(type, ref missing);
    			Word.Chart c = ils.Chart;
    			Excel.Workbook wb = c.ChartData.Workbook;
    			Excel.Worksheet ws = wb.Worksheets[1];
    			Excel.ListObject lo = ws.ListObjects[1];
    			
    			lo.Resize(ws.Range[string.Format("A1:B{0}", input.Length + 1)]);
    
    			ws.Cells[1, 2] = name;
    			for (int i = 1; i <= input.Length; i++)
    			{
    				ws.Cells[i + 1, 1] = i;
    				ws.Cells[i + 1, 2] = input[i - 1];
    			}  	 
    		}
    

    Regards Ondro

    Saturday, August 20, 2011 2:25 PM
  • Hi Ondro

    The key is handing over an object that represents a Range instead of ref missing:

     Word.InlineShape ils = this.InlineShapes.AddChart(type, ref missing);

    You say you want it on the second page, but that's not accurate enough identification for targeting a Range. I'm assuming this second page exists, already, at the time you're creating the chart?

    What kind of thing are you using to target data input, already? Or, if you're not, are you familiar with the concept of Bookmarks or Content Controls?

    What's the "history" of this document at the point your code is working on it? Will end-users be mucking around in it, or will you get it in a "pristine" condition?

    Edit: Sorry about the VBA. That's easiest for me to research object model questions because I can watch things while in Debug mode. I forgot I was in a .NET forum when I posted! I switch between all three languages so regularly that I foget...


    Cindy Meister, VSTO/Word MVP
    Saturday, August 20, 2011 2:33 PM
    Moderator
  • Hi Cindy, thanks for quick answer,

     

    I know that second parameter should be range of document where the item should be inserted, but i dont know how to use it, so i use ref missing to test if function work.

    Second page exist, and there is reserved some place.

     

    Now I'm using bookmarks (i got almost 60 bookmarks in document).

     

    Please apologize my poor English, i don't understand what do you mean with fourth paragraph (What's the "history" of this....)

     

    Its OK, everyone has his habits.

     

    Regards Ondro

     

    Saturday, August 20, 2011 3:25 PM
  • Hi Ondro

    <<i don't understand what do you mean with fourth paragraph...>>

    Basically, I wanted to know whether there's a chance that a user could remove the bookmark (or whatever), which could cause your code to fail. But if you're already using bookmarks, then that's what you should use.

    So. In the Word Document designer put a bookmark where you'd like that chart to go.

    To get the Range object, something like this:

    Word.Range rng = this.Bookmarks["Name"].Range;
    object oRng = rng;
    Word.InlineShape ils = this.InlineShapes.AddChart(type, ref oRng);


    Cindy Meister, VSTO/Word MVP
    Saturday, August 20, 2011 3:45 PM
    Moderator
  • Hi Cindy, thanks for quick answer

     

    And want to say big thanks for helping me.

     

    I updated the function to

    private void Chart(string name,double[] input, Microsoft.Office.Core.XlChartType type, string Bookmarkname)
    		{
    
                Word.Range rng = this.Bookmarks[Bookmarkname].Range;
                object oRng = rng;
    
    			Word.InlineShape ils = this.InlineShapes.AddChart(type, ref oRng);
    			Word.Chart c = ils.Chart;
    			Excel.Workbook wb = c.ChartData.Workbook;
                Excel.Worksheet ws = wb.Worksheets[1];
    			Excel.ListObject lo = ws.ListObjects[1];
    			
    			lo.Resize(ws.Range[string.Format("A1:B{0}", input.Length + 1)]);
    
    			ws.Cells[1, 2] = name;
    			for (int i = 1; i <= input.Length; i++)
    			{
    				ws.Cells[i + 1, 1] = i;
    				ws.Cells[i + 1, 2] = input[i - 1];
    			}  	 
    		}
    

    Now its absolutely great, but i want 3 charts at one page, is there any way to small these charts a little bit?

    I want to ask another thing, about editing equation, if i put bookmark into equation i get some "not accessible" error, how to do that? (or should i create new thread?)

     

    Thanks for any answer,

    Regards Ondro

     

    Saturday, August 20, 2011 4:08 PM
  • Hi Ondro

    Have you tried setting the Height and Width properties of the InlineShape object?

    For equations, etc., yes, please start a new discussion. And I recommend you ask in the Word for Developers forum as this isn't really related to the VSTO technology. You'll find quite a few more application specialists there.

    http://social.msdn.microsoft.com/Forums/en-US/worddev/threads

    Don't forget to mention the version of Word and exactly what you mean by an equation (I think I know, but there are a couple of possibilities, so...)


    Cindy Meister, VSTO/Word MVP
    Saturday, August 20, 2011 4:32 PM
    Moderator
  •  

    Oh thank you very much, but there is one more thing.

    How can i close excel window so end-user is not affected by opening three (or more windows) of excel? (or turning it invisible)

     

    when i try to use

    wb.Close(no, missing, missing);
    

    at the end of function the charts are not plotted right (charts are made of some default values, no my array of numbers)

     

    Regards Ondro.

     

    edit: if i try to put this code:

    ils.ScaleHeight = 0.7F;
    

    at the end of function i get error

    Calling of COM component return error HRESULT E_FAIL (translated from Czech)

    i think before setting height i have to wait to load customization (but have no clue how)

     

    Thanks for any answer

    Regard Ondro

    Saturday, August 20, 2011 4:51 PM
  • Try Chart.Refresh(); for the first.

    It's the end of my day, so I can't look at the last at the moment. But does Height or Width work?


    Cindy Meister, VSTO/Word MVP
    Saturday, August 20, 2011 5:28 PM
    Moderator
  • if you think c.Refresh()  then it doesn't work, same error.

    i also tried ils.Chart.Refresh()

     

    Thanks for any help

    Ondro

    Saturday, August 20, 2011 5:36 PM
  • Hi Ondro

    OK, I remember having this problem before and writing about it in my book. It was late yesterday, when I replied, so had forgotten about that...

    SAVE the Word document before finishing in Excel: this.Save();


    Cindy Meister, VSTO/Word MVP
    Sunday, August 21, 2011 8:38 AM
    Moderator
  • Now the function look like

    private void Chart(string name,double[] input, Microsoft.Office.Core.XlChartType type, string Bookmarkname)
    		{
    			Word.Range rng = this.Bookmarks[Bookmarkname].Range;
    			object oRng = rng;
    
    			Word.InlineShape ils = this.InlineShapes.AddChart(type, ref oRng);
    			Word.Chart c = ils.Chart;
    			Excel.Workbook wb = c.ChartData.Workbook;
    			Excel.Worksheet ws = wb.Worksheets[1];
    			Excel.ListObject lo = ws.ListObjects[1];			
    			lo.Resize(ws.Range[string.Format("A1:B{0}", input.Length + 1)]);
    
    			ws.Cells[1, 2] = name;
    			for (int i = 1; i <= input.Length; i++)
    			{
    				ws.Cells[i + 1, 1] = i;
    				ws.Cells[i + 1, 2] = input[i - 1];
    			}
    
          this.Save();
          c.Refresh();
          ils.Chart.Refresh();
    			//ils.Chart.Refresh();
    			ils.ScaleHeight = 0.7F;
    		}
    
    and throw still the same error HRESULT E_FAIL

    Sunday, August 21, 2011 11:20 AM
  • which line of code is throwing the error.

    I'm only talking about updating the chart, nothing else...


    Cindy Meister, VSTO/Word MVP
    Sunday, August 21, 2011 1:16 PM
    Moderator
  • Finally i make smaller chart.

    ScaleHeight/ScaleWidth is not working in my case, but i try

    ils.Height = 200;
    


    and it works. Is there any way to automatically close excel windows (im generating more charts so i have many windows opened after loading customization).

     

    Thanks for any answer/help.

    Regard Ondro

     

    PS> back to equations, there isnt any way to edit it thought vsto?

    Tuesday, September 6, 2011 9:15 AM
  • Try wb.Close();

    In my experience, that works. Do it after saving the Word document.

    Please start a new question thread for additional questions, especially about a different topic.

    And please mark the messages in this thread that you consider Answers.


    Cindy Meister, VSTO/Word MVP
    Tuesday, September 6, 2011 1:07 PM
    Moderator
  • its not working, if i use

    this.Save();
    wb.Close(false, missing, missing);
    

     

    its not working, cause chart its filled by some default values (but excel window close successfully)

    I think problem is in this.Save() function, cause when i use it for first time it doesn't pop up save-as dialog. Any ideas?

    Thanks for any ideas,

    Regard Ondro



    Tuesday, September 6, 2011 5:11 PM
  • Hello Ondro

    If this is a document-level VSTO customization (a *.docx file) then this.Save() will never trigger the FileSaveAs dialog box because the document already exists (has been opened from disk).

    It would really help a lot if you'd stick to one question / message thread. You've brought up so many different topics in this one thread that it's become very difficult to follow the discussion.


    Cindy Meister, VSTO/Word MVP
    Thursday, September 8, 2011 3:10 PM
    Moderator