2

I have created a WPF window (non-modal) that I am opening up on click of a button present in MS excel add-in ribbon. I was facing the issue of non-editable textbox in WPF window which I tried to overcome using the solution given on : WPF modeless dialog from MS Excel add-in

Now, the problem of non-editable textbox in WPF window got solved. But once I closes the excel file and opens it again, the MS excel add-in tab stopped showing (also, when I close the excel file and before I reopen it, I can the see the excel process under Background processes in Task Manager). As a result, I need to manually remove and re-add the excel add-in every time I open an excel file. This is really unacceptable for user environment. Kindly suggest if there is a solution to overcome this.

Here is the code that I have written on button (present on add-in ribbon) click event handler.

Thread newWindowThread = new Thread(new ThreadStart(() =>
                    {
                        // Create our context, and install it:
                        SynchronizationContext.SetSynchronizationContext(
                            new DispatcherSynchronizationContext(
                                Dispatcher.CurrentDispatcher));
                        
                        Window1 tempWindow = new Window1(ExcelApp.ActiveWorkbook.Name);
                        // When the window closes, shut down the dispatcher
                        tempWindow.Closed += (s, e) =>
                           Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background);

                        tempWindow.Show();
                        // Start the Dispatcher Processing
                        System.Windows.Threading.Dispatcher.Run();
                    }));

                    // Set the apartment state
                    newWindowThread.SetApartmentState(ApartmentState.STA);
                    // Make the thread a background thread
                    //newWindowThread.IsBackground = true;
                    // Start the thread
                    newWindowThread.Start(); 
Garima
  • 81
  • 1
  • 4

1 Answers1

0

There are two way to solve this issue. 1)Don't use thread. Instead do this:-

Window1 tempWindow = new Window1(ExcelApp.ActiveWorkbook.Name); tempWindow.ShowDialog();

but it block the Excel because of ShowDialog() method.

2)Follow below link for using Wpf in Excel using Excel-Dna https://github.com/AddinX/Sample.Wpf/tree/master/src

YUVRAJ
  • 109
  • 3