|
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
|
|
|
|
|
Daytona675 wrote: So it appears that, when debugging the code, that the OLEFormat property is Null, which I assume is causing the exception to fire off. You should check this with your debugger, and provide the exact content of wshape and the exception at the point of error.
Use the best guess
|
|
|
|
|
Richard is exactly right - put a breakpoint on the first line in the method, and single step through the code. At each point, look at the appropriate variables and see which of them is wrong. You can then try to work back to where that wrong value came from, or at least give us better information on the actual problem.
Normally, an exception holds a good level of detail - including the line number that the error occurred on. This can help - but only if you tell us what the error message is exactly, and which line it appears on!
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|