Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel vsto end edit mode

Tags:

.net

excel

com

vsto

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?

like image 388
GermanSniper Avatar asked Mar 03 '14 14:03

GermanSniper


1 Answers

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.");
        }
    }
}
like image 188
MattPerry Avatar answered Nov 04 '22 05:11

MattPerry