المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : VS2012, C# Office 2007 problem



C# Programming
02-28-2013, 09:04 AM
I've asked maybe to broad of a question before. So I have created a very simple application. My Word document has 1 chart in it.

In a nutshell, where is what happens:
1. Word Document is opened.
2. Find the number of Charts in the document.
3. Loop through the inline shapes
4. Check to see if they are Charts.

So as I said, nothing fancy.

private void Form1_Load(object sender, EventArgs e) { Document oReportDoc = new Document(); Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application(); oReportDoc = oWord.Documents.Open(@"C:\ChartTest.docx", ReadOnly: false); label2.Text = oReportDoc.InlineShapes.Count.ToString(); int j = 1; foreach (Microsoft.Office.Interop.Word.InlineShape wshape in oReportDoc.InlineShapes) { try { if (wshape.OLEFormat.ProgID.Equals("Excel.Sheet.8")) { if (wshape.HasChart == Microsoft.Office.Core.MsoTriState.msoTrue) { wshape.OLEFormat.Open(); Microsoft.Office.Interop.Excel.Workbook xlWorkBook = wshape.OLEFormat.Object; if (xlWorkBook != null) { xlWorkBook.ActiveChart.HasTitle = true; xlWorkBook.ActiveChart.ChartTitle.Text = "test" + j.ToString() + "!"; j++; } } } } catch (Exception ex) { richTextBox1.Text = ex.Message; } } }

So it appears that, when debugging the code, that the OLEFormat property is Null, which I assume is causing the exception to fire off.

Can anyone tell me what is happening here and why the OLEFormat is null? And how to get around this issue?

Very frustrating.

Thanks,
Jeff