views:

16

answers:

1

Hi every one this is my first question here.

I want to add a Office Graph in word 2007 using C# 4.0,

I am using Office 2007 word, for better charts like 3D Bubble My task is to Genrate Graph and table from sql database i have done it in good menner with using the excel chart then copy chart as image in word.

now i want to add chart itself in word so that the user can change the chart or its value. currently i m doing this code.

       object missing = Type.Missing;          

        Word.Application application = new
        Microsoft.Office.Interop.Word.Application();
        application.Visible = true;
        Word.Document document = application.Documents.Add(ref missing, ref missing, ref missing,
        ref missing);
        Random rd = new Random();

        objchart = (Graph.Chart)document.Shapes.AddOLEObject("MSGraph.Chart.8").OLEFormat.Object;
        dataSheet = objchart.Application.DataSheet;
        for (int r = 1; r < 10; r++)
        { for (int c = 1; c < 5; c++) { dataSheet.Cells[r, c] = rd.Next(10, 50); } }

the below code is working fine but the result is not good as i want. if i use "Excel.Chart.8" insteed of "MSGraph.Chart.8" it gives me error.

A: 

Oh. no body is here give the answer for my question. let me help my self. I have written these code for above problem and its word fine. hope help u too.

     object missing = Type.Missing;

        Word.Application application = new Microsoft.Office.Interop.Word.Application();
        application.Visible = true;
        Word.Document document = application.Documents.Add(ref missing, ref missing, ref missing, ref missing);
        object classtype = "Excel.Chart.8";
        object oEndOfDoc = "\\endofdoc";
        Word.InlineShape wrdInlineShape = document.InlineShapes.AddOLEObject(classtype);
        if (wrdInlineShape.OLEFormat.ProgID == "Excel.Chart.8")
        {
            // Word doesn't keep all of its embedded objects in the running state all the time.
            // In order to access the interface you first have to ensure the object is in the running state,
            // ie: OLEFormat.Activate() (or something)
            object verb = Word.WdOLEVerb.wdOLEVerbHide;
            wrdInlineShape.OLEFormat.DoVerb(ref verb);
            Random rn = new Random();
            Excel.Workbook obook = (Excel.Workbook)wrdInlineShape.OLEFormat.Object;
            Excel.Worksheet sheet = (Excel.Worksheet)obook.Worksheets["Sheet1"];
            for (int i = 1; i <= 7; i++)
            {
                for (int c = 1; c <= 4; c++)
                {
                    ((Excel.Range)sheet.Cells[i, c]).Value = rn.Next(10, 50);
                    ((Excel.Range)sheet.Cells[i, c]).Value = rn.Next(10, 50);
                }
            }
            wrdInlineShape.Width = 400;

            obook.ActiveChart.ChartType = Excel.XlChartType.xlBubble3DEffect;
            Word.Range wrdRng = document.Bookmarks.get_Item(ref oEndOfDoc).Range;
            object oRng = document.Bookmarks.get_Item(ref oEndOfDoc).Range;
            wrdRng = document.Bookmarks.get_Item(ref oEndOfDoc).Range;
            sheet.UsedRange.Copy();
            document.SetDefaultTableStyle("Light List - Accent 4", false);
            for (int i = 0; i < 5; i++)
            {
                wrdRng.InsertBreak(Word.WdBreakType.wdLineBreak); 
            }
            wrdRng.PasteExcelTable(true, true, false);
            wrdInlineShape.ConvertToShape();
        }
        // quit the word

hi there if u have some more bueatifull code for above one please post it.

J S Jodha