How to Determine if a Workbook is Embedded

Recently I had a very interesting request from a customer. How do you determine if a workbook is embedded inside another document and opened in Excel for editing? Excel has a property that used to help in this regard:

if (Wb.IsInplace)
    // do something here


MSDN states that this is to help you determine if the document is being edited in-place. In-place being the operative term for embedded. However, this now seems to always return FALSE since Office applications no longer really support OLE in-place editing in the traditional sense. So the predicament. How can you determine this state now… Confused smile

Well, I found a way, across two events and a few properties and looking at a few other properties and making some educated decisions based on those properties values. Hot smile

The way this works is to determine two different conditions:

  1. If the Caption changed from the Open Event to the Activate event and there is no Path to the file, then we know it is embedded as that is the only state in which this occurs.
  2. In condition where we are New and the Open event does not fire, the caption name in the Activate event is “object” so we look for that condition if condition #1 is not true.

Here is the code:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
    // hook to events
    Application.WorkbookOpen += new Microsoft.Office.Interop.Excel.AppEvents_WorkbookOpenEventHandler(Application_WorkbookOpen);
    Application.WorkbookActivate += new Microsoft.Office.Interop.Excel.AppEvents_WorkbookActivateEventHandler(Application_WorkbookActivate);

string wbOpenCaption = "";
void Application_WorkbookOpen(Microsoft.Office.Interop.Excel.Workbook Wb)
    // get the captions name because iof this is an embedded workbook
    // the name will have changed by the time we get to the Activate
    // event and that is a sure sign of an embdedded state
    wbOpenCaption = Wb.Windows[1].Caption.ToString();

void Application_WorkbookActivate(Microsoft.Office.Interop.Excel.Workbook Wb)
    // on activate we see if the OPEN document caption has changed.
    // When the user has opened a workbook enbedded anywhere, the intial
    // caption for the window will be something like "Book2" in the
    // Workbook_Open event. However, by the time we get the Activate
    // event the caption will be updated to "Workbook in Document.docx"
    // So here we are looking for that condition along with the
    // condition where the workbook has no path. This assures us we
    // have an embedded document.
    // In scenarios where a new workbook is just now embeeded into a
    // document the Caption for the window will be "Object" also in
    // those scenarios the Open event is not called.
    string wbActivateCaption = Wb.Windows[1].Caption.ToString();
    if (wbOpenCaption.Length > 0 &&
        wbOpenCaption != wbActivateCaption &&
       Wb.Path.Length == 0)
        MessageBox.Show("This is an embedded workbook.");
    else if (wbOpenCaption.Length == 0 &&
            wbActivateCaption.ToLower() == "object")
        MessageBox.Show("New embedded workbook.");
    wbOpenCaption = ""; // reset

1 thought on “How to Determine if a Workbook is Embedded”

  1. Hi,
    thanks for figuring this out, but are you aware that it
    doesn’t work in the case, when Excel is already running besides?

Leave a Reply