none
Prevent other excel workbooks being opened from using my Excel process RRS feed

  • Question

  • Hi,

    I am writing an addin. I want to prevent other newly opened wrokbooks from using my Excel application instance. Is there a solution for this.

    if there is no solution for above question, then is there a way to hide the complete excel window of my workbook with out actually hiding other opened workbooks which are using my excel process.


    Uday

    Wednesday, September 4, 2013 4:36 PM

Answers

  • I found a simple way of doing

    I am using workbook_open event handler

    ExcelApp.WorkbookOpen += new AppEvents_WorkbookOpenEventHandler(ExcelApp_WorkbookOpen);
    below is the implementation.

     void ExcelApp_WorkbookOpen(Excel.Workbook Wb)
            {
                ExcelApp.Visible = false;
                string str = Wb.FullName;
                Wb.Close(false, Type.Missing, Type.Missing);
                Microsoft.Office.Interop.Excel.Application appnew = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbooks workbooks1 = appnew.Workbooks;
                if (str != null)
                    workbooks1.Open(str, missing, missing, missing, missing, missing, missing, missing,
        missing, missing, missing, missing, missing, missing, missing);
                appnew.Visible = true;
                appnew = null;
                ExcelApp.Visible = false;
            }



    Uday

    Tuesday, September 17, 2013 5:19 PM

All replies

  • Hi Uday,

    What do you mean by “prevent other newly opened wrokbooks from using my Excel application instance”? Based on my understanding, it means only one workbook will be opened at once, is that correct?

    If so, I think you can use the code below to work around:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Excel = Microsoft.Office.Interop.Excel;
    using Office = Microsoft.Office.Core;
    using Microsoft.Office.Tools.Excel;
    
    namespace ExcelAddInCSharp
    {
        public partial class ThisAddIn
        {
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                Application.WorkbookOpen += OnWorkbookOpen;
            }
    
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }
    
            public void OnWorkbookOpen(Excel.Workbook Wb)
            {
                if (Application.Workbooks.Count > 1)
                {
                    Wb.Close();
                }
            }
    
            #region VSTO generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
            
            #endregion
        }
    }


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Thursday, September 5, 2013 12:32 PM
    Moderator
  • Thanks for the quick response

    Thanks for the code. This piece of code will close all the other workbooks opened in user's  computer. But  My requirement is not to prevent user from opening the workbooks.

    This is the requirement.

    I writing a VSTO add-in which provides extra functionality(like opening a windows form) to few specific Excel workbooks. It recognizes these Excel work books using some criteria and doesn't provide the extra functinality to other workbooks.

    Now when Add-in displays the windows form it has to hide the Excel workbook from taskbar.

    for this I am using this.application.visible=false;

    Since the other workbooks opened by user are using the same Excel Applpication process, when they are getting hidden when ever I execute above code.

    So as a solution for this, I am trying to check if the other Excel workbooks can open in separate instance of Excel process.


    Uday

    Thursday, September 5, 2013 1:36 PM
  • Hi Uday

    In general, something like this is tricky when it's possible at all and doesn't work consistently across Office versions.

    One issue is how Office applications are registered and how the processes are loaded into the ROT. This article will give you some idea of what you're dealing with and also suggest a workaround that could be relevant for you: 

    http://support.microsoft.com/kb/188546/

    One problem certainly is that this is an add-in, running in-process... I'm wondering whether it might make more sense to design the Windows Form to be a separate solution, not part of the Add-in. This could close the workbook and deal with the content using OLE DB or perhaps the Open XML file format? Or store the information to be integrated into the workbook later? Then open it again when work in the Windows Form is finished?


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, September 9, 2013 3:16 PM
    Moderator
  • I found a simple way of doing

    I am using workbook_open event handler

    ExcelApp.WorkbookOpen += new AppEvents_WorkbookOpenEventHandler(ExcelApp_WorkbookOpen);
    below is the implementation.

     void ExcelApp_WorkbookOpen(Excel.Workbook Wb)
            {
                ExcelApp.Visible = false;
                string str = Wb.FullName;
                Wb.Close(false, Type.Missing, Type.Missing);
                Microsoft.Office.Interop.Excel.Application appnew = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbooks workbooks1 = appnew.Workbooks;
                if (str != null)
                    workbooks1.Open(str, missing, missing, missing, missing, missing, missing, missing,
        missing, missing, missing, missing, missing, missing, missing);
                appnew.Visible = true;
                appnew = null;
                ExcelApp.Visible = false;
            }



    Uday

    Tuesday, September 17, 2013 5:19 PM
  • Interesting approach. Thanks for sharing :-)

    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, October 1, 2013 5:00 PM
    Moderator