2

I have a wpf window which should be opened beside an Excel window. Excel is opened and handled with Interop.Excel. In my Window i have a method, which should set the size of the excel window.

private void SetLayout()
        {
            Top = 0;
            Left = 0;
            Height = SystemParameters.WorkArea.Height;
            ((ConfiguratorWindowViewModel) DataContext).Manager.App.Height =
                SystemParameters.WorkArea.Height;
            ((ConfiguratorWindowViewModel) DataContext).Manager.App.Left = Width;
            ((ConfiguratorWindowViewModel) DataContext).Manager.App.Width = SystemParameters.WorkArea.Width - Width;
        }

In my Viewmodel I have a Manager which gives back the open Excel App (Office.Interop.Excel.Application). I can set the Height,Top,.. of my wpf window and it works fine, but not on the Excel window. How can i set the size of the Excel window beside my wpf window so that the two fill the complete screen together?

Edit:

I tried this, but it is also not working:

((ConfiguratorWindowViewModel) DataContext).Manager.App.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlNormal;
((ConfiguratorWindowViewModel) DataContext).Manager.App.ActiveWindow.Height = Screen.PrimaryScreen.Bounds.Height;
((ConfiguratorWindowViewModel) DataContext).Manager.App.ActiveWindow.Width = Screen.PrimaryScreen.Bounds.Width - Width;

Both sets the Height to a different value but not to the correct one, when i hardcode it like .Height=800; it's also not working.

Unfortunately i can't post images to show you how it Looks and it should look like.

Edit:

The question is not the same as this one (How do I change another program's window's size?). I don't want to change the size of ah "foreign" application.

Community
  • 1
  • 1
funtastisch
  • 21
  • 1
  • 4

1 Answers1

6

Lets say you have excel application stored in a variable called App. Then you can do something like this:

App.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlNormal;
App.ActiveWindow.Height = Screen.PrimaryScreen.Bounds.Height;
App.ActiveWindow.Width = Screen.PrimaryScreen.Bounds.Width;

Screen class is used to get the resolution of your primary screen.

You could probably also just maximize the window:

 App.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMaximized
Mitja Bezenšek
  • 2,503
  • 1
  • 14
  • 17
  • For some reasons its not working. It dont put the correct value into App.ActiveWindow.Height and .Width there are completely different values. – funtastisch Jul 17 '14 at 09:04
  • What is the range or what values to be passed to the App.ActiveWindow.Height and .Width. Documentation for these is not very clear, thanks – Sai krishna Oct 20 '21 at 04:02