0

I am making a VSTO PowerPoint add-in, that adds charts to a slide among other things. When I add a chart I use the following line of code:

private void button2_Click(object sender, RibbonControlEventArgs e)
{
    Globals.ThisAddIn.Application.ActivePresentation.Slides[1].Shapes.AddChart(XlChartType.xlLine);
}

This works, and the chart gets added. However, if the user has an Excel application open and is actively editing a cell, I get the following Exception:

System.Runtime.InteropServices.COMException: 'Error HRESULT E_FAIL has been returned from a call to a COM component.'

I found this blog post regarding this issue, but seeing as my add-in is made in PowerPoint, I don't have access to the Excel application. Is there any way in VSTO to make sure that Excel is not in edit mode from a PowerPoint add-in?

Frederik Hansen
  • 506
  • 4
  • 21
  • I am not 100% sure, but I'm almost 100% sure that your diagnosis of the problem is incorrect. I believe a user can be editing in Excel. The problem is that `AddChart` method creates a new Excel instance/window which immediately steals focus, and thus intercepts keystroke(s) from the user. So this problem manifests if the user is typing an email in outlook, or entering a search term in a browser window, etc. Literally anything the user is doing can cause this behavior with PowerPoint. – David Zemens Jul 09 '18 at 13:01
  • I have tested this myself, without typing. The chart gets added if a cell is not being edited, while an exception is thrown if the cell is in edit mode. – Frederik Hansen Jul 09 '18 at 13:03
  • OK, well word of caution: if you have not encountered the error condition I described above, yet, you will. It cannot be avoided other than recommending that users just let the add-in work without trying to multitask on other applications. – David Zemens Jul 09 '18 at 13:04
  • I have, but thanks for pointing it out. – Frederik Hansen Jul 09 '18 at 13:05
  • OK also, I do not replicate the error you describe (https://imgur.com/a/ojN2vvF -- Book1 is open in edit mode, cell A1, and my application successfully created a powerpoint presentation, slide, and then added a chart in that slide), so, you may need to include more code and detail about the use-case to produce a [mcve]. – David Zemens Jul 09 '18 at 13:13
  • Okay, that's weird, cause I can definitely replicate this. I have added a simple button with the update line of code, and if I have a Excel sheet open and I'm editing a field it fails. – Frederik Hansen Jul 09 '18 at 13:31
  • Have you attempted to implement the solution described in that blog? – David Zemens Jul 09 '18 at 13:35
  • No, I'm not sure as how I would get a reference to the Excel application, as this most likely is opened by the user and not by the add-in. – Frederik Hansen Jul 09 '18 at 13:36
  • 1
    might try something like this: https://stackoverflow.com/questions/1118735/get-instance-of-excel-application-with-c-sharp-by-handle – David Zemens Jul 09 '18 at 14:16

0 Answers0