简体   繁体   中英

Open multiple excel files in WebBrowser, only the last one gets activated

This question has been asked by others several years ago but no answers yet ( https://go4answers.webhost4life.com/Example/excel-web-browser-control-locks-other-206723.aspx ), we have the similar issue and need the solution, so I paste here. Note we are using System.Windows.Forms.WebBrowser, this is different from his question:

background: In order to automate and embed Excel in a windows form application I've used the webBrowser control.

I am able to navigate to the Excel file without a problem.To navigate I am using

System.Windows.Forms.WebBrowser WebBrowserExcel; this.WebBrowserExcel.Navigate(filename,false);

After navigating to the Excel file, I am querying the running Object table to attach to the workBook and then manipulate the cells in the workbook.

public Workbook GetActiveWorkbook(string xlfile) {


IRunningObjectTable prot=null;
                IEnumMoniker pmonkenum=null;
                try {

                    //return m_Workbook;       
                    IntPtr pfetched = IntPtr.Zero;
                    // Query the running object table (ROT)
                    if (GetRunningObjectTable(0, out prot) != 0 || prot == null) return null;
                    prot.EnumRunning(out pmonkenum);
                    pmonkenum.Reset();
                    IMoniker[] monikers = new IMoniker[1];
                    while (pmonkenum.Next(1, monikers, pfetched) == 0)
                    {
                        IBindCtx pctx; string filepathname;
                        CreateBindCtx(0, out pctx);
                        // Get the name of the file
                        monikers[0].GetDisplayName(pctx, null, out filepathname);
                        // Clean up
                        Marshal.ReleaseComObject(pctx);
                        // Search for the workbook
                        if (filepathname.IndexOf(xlfile) != -1)
                        {
                            object roval;
                            // Get a handle on the workbook
                            prot.GetObject(monikers[0], out roval);
                            return roval as Workbook;
                        }
                    }
                } catch {
                    return null;
                } finally {
                    // Clean up
                    if(prot!=null) Marshal.ReleaseComObject(prot);
                    if(pmonkenum!=null) Marshal.ReleaseComObject(pmonkenum);
                }
                return null;
            }

The code works fine and I am able to work with the Excel workbook UNTIL no other workbook is open in the system(another workbook opened by double clicking the file in the local system).

The following is the scenario:

1) I opened a workbook from explorer by double clicking it. Let's call it Excel A. This started an EXCEL.EXE process. 2) I navigated to another Excel workbook from my Windows Form web browser. Let's call it Excel B. Excel B opens in the Form.It uses the already existing EXCEL.EXE started in step 1. 3) Now if I try to edit Excel A(opened in step 1).It does not allow me. The focus is always there on Excel B(navigated in step 2). I cannot edit the cells, select text or even close Excel A.

Please kindly let me know any solution to solve it.

This issue was solved with the hard work of the team and with the help from Microsoft deep support. I share the final solution in Google open source projects https://code.google.com/p/form-based-excel-solution/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM