none
Extract Excel charts embedded in MS Office 2010 Word document (*.docx) RRS feed

  • Question

  • I am currently developing a C# application and I need to extract from a MS Office 2010 Word document (*.docx) all embedded EXCEL charts to modify some properties of these charts and save the word document.

    The problem is that for all embedded Excel Chart objects the Microsoft.Office.Interop.Word.InlineShape.OLEFormat object is always “null” so obviously I will not be able to modify the embedded object (the EXCEL Chart in my case)

    All my Excel Charts are Office 2010 objects, so I presume that they have ProgID = “Excel.Sheet.12”

    When I have looked on MSDN for InlineShape.HasChart Property I have noticed the following information:

    This property always returns False for OLE charts. For OLE charts, use InlineShape.OLEFormat.ProgID and check for the following possible values: "Excel.Chart.8", "MSGraph.Chart.8", "Excel.Sheet.8", "Excel.Chart.5", "MSGraph.Chart.5", or "Excel.Sheet.5".

    Can anybody help me, please?

    Thank in advance.




    • Edited by bio74 Friday, July 6, 2012 10:01 AM
    Wednesday, July 4, 2012 9:23 AM

Answers

  • Hi benone_ionescu,

    I noticed that you open a new thread about it. I will reply you addtional question at that place. Would you please mark this?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by bio74 Tuesday, July 10, 2012 1:52 PM
    Monday, July 9, 2012 7:33 AM
    Moderator

All replies

  • Hi benone_ionescu,

    Thanks for posting in the MSDN Forum.

    if (inlineShape.HasChart == Microsoft.Office.Core.MsoTriState.msoTrue)

    This statement will let you get nothing in your snippet. And as a Embedded Excel instance, you need "Open" it before you access it's instance. Please following my snippet to handle your issue. If you have additional question, please feel free to let me know.

    using Microsoft.Office.Core;
    using Word = Microsoft.Office.Interop.Word;
    using Excel = Microsoft.Office.Interop.Excel;
    
    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "ConsoleApplication2.exe.config", Watch = true)]
    namespace ConsoleApplication2
    {
        class Program
        {
            [STAThread]
            static void Main(string[] args)
            {
                ILog log = log4net.LogManager.GetLogger(typeof(Program));
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.InitialDirectory = @"C:\Users\****\Desktop\****";
                ofd.Multiselect = false;
                ofd.Filter = "Word Document|*.docx;*.doc";
                ofd.ShowDialog();
                string DocPath = ofd.FileName;
                log.Info("Open the file : " + DocPath);
                try
                {
                    Word.Application wdApplication = new Word.Application();
                    //var missing = Type.Missing;
                    wdApplication.Visible = true;
                    Word.Document wdDoc = wdApplication.Documents.Open(DocPath);
                    foreach (Word.InlineShape wdInlineShape in wdDoc.InlineShapes)
                    {
                        if (wdInlineShape.OLEFormat.ProgID.Equals("Excel.Sheet.8"))
                        {
                            wdInlineShape.OLEFormat.Open();
                            Excel.Workbook xlWorkbook = wdInlineShape.OLEFormat
                                .Object;
    
                            if (xlWorkbook != null)
                            {
                                xlWorkbook.ActiveChart.HasTitle = true;
                                xlWorkbook.ActiveChart.ChartTitle.Text = "Test";
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Fatal(string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
                }
                log.Error("Finished");
                Console.ReadKey();
            }
        }
    }

    PS: please omit the part which regard log4net. I just use it debug my program. It will not affect the Program if you omit it.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us


    Thursday, July 5, 2012 6:31 AM
    Moderator
  • Hi benone_ionescu,

    I noticed that you open a new thread about it. I will reply you addtional question at that place. Would you please mark this?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by bio74 Tuesday, July 10, 2012 1:52 PM
    Monday, July 9, 2012 7:33 AM
    Moderator