none
Activate method of Range class failed - Excel 2016 RRS feed

  • Question

  • After embedding the excel into a window control using the SetParent()  i.e SetParent(ExcelWnd, this.Handle.ToInt32());

    Then If I select a range and then do an activate  I'm getting the following error

    "Activate method of Range class failed"

     Excel.Worksheet srcworkSheet1 = (Excel.Worksheet)wbook.Worksheets.Item[1];
                srcworkSheet1.Activate();
                wbook.Activate();
               
                    Excel.Range diffnewrange = srcworkSheet1.get_Range(("A5"), ("A10"));   //selecting range - hardcoded for testing 
                    diffnewrange.Interior.ColorIndex = 8;
                    diffnewrange.Activate();             //throwing error in excel 2013 and above
               

    were as the same code works in excel version 2010 and below

    Any help highly appreciated!!


    Rajesh Yesurajan


    Wednesday, January 3, 2018 6:19 AM

All replies

  • Hi Rajesh Yesurajan,

    I could reproduce your issue using Excel 2013 and Excel 2016. 

    According to your previous thread, you are trying to embed the excel into a window form application using Win32 API. To be honest, I'm not familar with Win32 API. I tried to reproduce the process without embedding excel to window form and it work well.  Searching for documents doesn't give a desirable explanation for this.

    After attempting, I found a solution for it and I would suggest you refer to it as workaround.

    if (oBook != null)
                    {
                        Microsoft.Office.Interop.Excel.Worksheet srcworkSheet1 = (Excel.Worksheet)oBook.Worksheets.Item[1];
                        //srcworkSheet1.Activate(); //removed
                        //oBook.Activate(); //removed
                        try
                        {
                            Microsoft.Office.Interop.Excel.Range diffnewrange =srcworkSheet1.get_Range(("A5"), ("A10"));
                            diffnewrange.Interior.ColorIndex  = 10;
                            //call the activate like this                    
                            srcworkSheet1.get_Range(("A5"), ("A10")).Activate();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 4, 2018 9:59 AM
  • Hi Terry,

    Thanks for the reply!!

    Without embedding excel into the window form control it will work.

    But I need to embed and then activate the range.


    Rajesh Yesurajan

    Thursday, January 4, 2018 10:41 AM
  • Hello Rajesh Yesurajan,

    In my test, I tried to embed the excel into a windows form based on the code in your previous thread. It seems that you are now using another SetParent(ExcelWnd, this.Handle.ToInt32()) different from the SetParent(IntPtr hWndChild, IntPtr hWndParent) in your previous thread.

    Could you please check if below whole code which using SetParent(IntPtr hWndChild, IntPtr hWndParent) could work for you?

     public partial class Form1 : Form
        {
            [DllImport("User32.dll", SetLastError = true)]
            public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
    
            [DllImport("Atl.dll", SetLastError = true)]
            public static extern bool AtlAxWinInit();
    
            [DllImport("Atl.dll", SetLastError = true)]
            public static extern int AtlAxGetControl(IntPtr h, [MarshalAs(UnmanagedType.IUnknown)] out object pp);
    
            public const int WS_OVERLAPPED = 0x0;
            public const int WS_BORDER = 0x00800000;
            public const int WS_POPUP = unchecked((int)0x80000000L);
            public const int WS_CHILD = 0x40000000;
            public const int WS_MINIMIZE = 0x20000000;
            public const int WS_VISIBLE = 0x10000000;
            public const int WS_DISABLED = 0x8000000;
    
            [DllImport("User32.dll", SetLastError = true)]
            public static extern IntPtr CreateWindowEx(int dwExStyle, string lpClassName, string lpWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);
    
            [DllImport("User32.dll", SetLastError = true)]
            public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndParent);
    
            [DllImport("User32.dll")]
            private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
            private IntPtr hWndContainer = IntPtr.Zero;
            private IntPtr hWndExcel = IntPtr.Zero;
            private Microsoft.Office.Interop.Excel.Application oExcel = null;
    
            public Form1()
            {
                //InitializeComponent();
                Load += Form1_Load;
                SizeChanged += Form1_SizeChanged;
                FormClosing += Form1_FormClosing;
            }
            Excel.Workbook oBook;
            private void Form1_Load(object sender, EventArgs e)
            {
                this.ClientSize = new System.Drawing.Size(980, 460);
                CenterToScreen();
                if (AtlAxWinInit())
                {
                    hWndContainer = CreateWindowEx(0, "AtlAxWin", "Excel.Application", WS_VISIBLE | WS_CHILD | WS_BORDER, 0, 0, Width, Height, this.Handle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                    object pp;
                    int hr = AtlAxGetControl(hWndContainer, out pp);
                    oExcel = (Excel.Application)pp;
                    oBook = oExcel.Workbooks.Open(@"C:\Users\v-guaxu\Desktop\1.xlsx");
                    oExcel.Visible = true;
    
                    // the title should be added instead of null in case another instance exists
                    hWndExcel = FindWindow("XLMAIN", null);
                    SetParent(hWndExcel, hWndContainer);
                    
                    MoveWindow(hWndContainer, 0, 0, Width - 20, Height - 42, true);
                    MoveWindow(hWndExcel, 0, 0, Width - 20, Height - 42, true);
    
                    if (oBook != null)
                    {
                        Microsoft.Office.Interop.Excel.Worksheet srcworkSheet1 = (Excel.Worksheet)oBook.Worksheets.Item[1];
                        try
                        {
                            Microsoft.Office.Interop.Excel.Range diffnewrange =srcworkSheet1.get_Range(("A5"), ("A10"));
                            diffnewrange.Interior.ColorIndex  = 10;                
                            srcworkSheet1.get_Range(("A5"), ("A10")).Activate();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }
            }
            private void Form1_SizeChanged(object sender, EventArgs eventArgs)
            {
                MoveWindow(hWndContainer, 0, 0, Width - 20, Height - 42, true);
                MoveWindow(hWndExcel, 0, 0, Width - 20, Height - 42, true);
    
            }
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (oExcel != null)
                {
                    oExcel.Visible = false;
                    SetParent(hWndExcel, IntPtr.Zero);
                    oExcel.Quit();
                }
            }
        }

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, January 5, 2018 1:37 AM