Question

in an excel COM addin I need to access the pageSetup property. But if edit mode in excel is active i get an exception.

I can check if edit mode is active with this code:

CommandBarControl oNewMenu = excelApp.CommandBars["Worksheet Menu Bar"].FindControl(
    1, //the type of item to look for
    18, //the item to look for
    refmissing, //the tag property (in this case missing)
    refmissing, //the visible property (in this case missing)
    true); //we want to look for it recursively so the last argument should be true.

    if ( oNewMenu != null )
    {
        // edit mode = true
        if (!oNewMenu.Enabled) {
        }
    }

I've found some solutions to exit edit mode, but they didn't work:

SendKeys.Flush();
excelApplication.SendKeys("{ENTER}");

How can I exit edit mode so that I can write the pageSetup property?

No correct solution

OTHER TIPS

You can try to use this to exit edit mode if you are using an add in:

Globals.ThisAddIn.Application.SendKeys("{ENTER}");

And I would recommend somthing similar to this this method to determine if Excel is in Edit Mode:

public static void VerifyExcelIsNotInCellEditMode()
{
    if (Globals.ThisWorkbook.Application.Interactive)
    {
        try
        {
            //Will throw an error if user is editing cell
            Globals.ThisWorkbook.Application.Interactive = false;
            Globals.ThisWorkbook.Application.Interactive = true;
        }
        catch 
        {
            throw new Exception("Excel is in Edit Mode.");
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top