I am trying to embed an excel workbook into WPF application. I have used the Microsoft.Office.Interop.Excel for achieving this, It works and opens the excel workbook in WPF application.
Problem: When I try to run the macros in VBA the workbook gives away the focus (deactivates the worksheet) and in order to work further I need to double click and activate the worksheet first which is not user friendly and usable for the customers.
Is there any way to overcome this issue or listen to the events from VBA in WPF application to activate the worksheet automatically.
setting AutomationSecurity in Excel to msoAutomationSecurityLow allows running the VBA in excel and causes this behavior.
Microsoft.Office.Interop.Excel excelApplication = new Microsoft.Office.Interop.Excel();
excelApplication.Visible = true;
// this is to enable the VBA, disabling the VBA works fine.
excelApplication.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityLow;
Microsoft.Office.Interop.Workbooks workbooks = excelApplication.Workbooks;
Microsoft.Office.Interop.Workbook workbook = workbooks.Open("excel file path");
//setting the parent of excel to WPF window.
**SetParent(excelHandle, parentWindowHandle);**
Expectation: Run the excel workbook same as running standalone application in WPF application.