none
Office automation - building graphs in Word RRS feed

  • Question

  • Hi, I have worked out how to insert text and table into a Word document, but can't find tutorials on how to build graphs in Word. What is the best approach to do so? Any useful links?
    Wednesday, August 10, 2011 5:47 AM

Answers

All replies

  • You might be confusing a funtion. In Word there are Tables and then there are Charts. Charts are graphs but they use Excel table table data not Word table data.

    Here's a link to a Step by Step guide about Tables: http://officeimg.vo.msecnd.net/en-us/files/898/377/AF102264638.pdf

    For information on Charts go to Word > Help > Charts

    Hope this helps

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    • Marked as answer by Silan Liu Wednesday, August 10, 2011 11:22 PM
    Wednesday, August 10, 2011 1:23 PM
  • Which version of Word is involved, here?

    If you create this graph in Word as an end-user, which commands are you using?


    Cindy Meister, VSTO/Word MVP
    Wednesday, August 10, 2011 2:33 PM
    Moderator
  • Sorry Rich I thought posting this question in "office automation" column implies that I want to do things programmatically - I should have said so explicitly. Yes I was meant to ask how to create a graph in Word using C#, and I have worked out how:

          object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
    
          //Start Word and create a new document.
          Word._Application wordApp = new Word.Application();
          wordApp.Visible = true;
          Word._Document wordDoc = wordApp.Documents.Add();
    
          object oClassType = "MSGraph.Chart.8";
          wordRange = wordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
    
          Word.InlineShape oShape = wordRange.InlineShapes.AddOLEObject(ref oClassType);
          Graph.Chart graphChart = oShape.OLEFormat.Object as Graph.Chart;
          graphChart.ChartType = Graph.XlChartType.xl3DBarClustered;
          graphChart.Left = 100;
          graphChart.Top = 100;
          graphChart.Width = 700;
          graphChart.Height = 700;
          graphChart.ChartArea.Font.Size = 8;
          graphChart.HasTitle = true;
          graphChart.ChartTitle.Text = "Sales Per Product";
          graphChart.ChartTitle.Font.Size = 12;
          Graph.Axis axis = graphChart.Axes(Graph.XlAxisType.xlValue) as Graph.Axis;
          axis.HasTitle = true;
          axis.AxisTitle.Caption = "Dollars ($)";
    
          graphChart.Application.DataSheet.Cells.Clear();
    
          graphChart.Application.DataSheet.Cells[2, 1] = "Widgets";
          graphChart.Application.DataSheet.Cells[3, 1] = "Gadgets";
          graphChart.Application.DataSheet.Cells[4, 1] = "Gizmos";
    
          graphChart.Application.DataSheet.Cells[1, 2] = "1999";
          graphChart.Application.DataSheet.Cells[1, 3] = "2000";
          Random random = new Random();
          int iSales = 0;
    
          for (int i = 2; i < 5; i++)
          {
            for (int j = 2; j < 4; j++)
            {
              iSales = random.Next(100000);
              graphChart.Application.DataSheet.Cells[i, j] = iSales.ToString();
            }
          }
    
          graphChart.Application.Update();
          graphChart.Application.Quit();
    
    


    Wednesday, August 10, 2011 11:29 PM