Check if focus is set to excel cell

I am working on a VSTO project. I need to determine input focus in an Excel vsto project.

I want to check if the focus is in the excel cell, or on another excel component, for example in the search dialog box, document action bar or any other built-in excel dialog box.

Can this be detected?

Please refer screen shot

As shown in the screenshot, I want to know if the input focus is set to the excel cell or not?

+7
source share
2 answers

This will get the title of the active window (using vba)

Option Explicit Private Declare Function GetActiveWindow Lib "User32.dll" () As Long Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _ (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Function ActiveWindowName() Dim hWnd As Long Dim lngRet As Long Dim strText As String hWnd = GetActiveWindow() strText = String(100, Chr(0)) lngRet = GetWindowText(hWnd, strText, 100) ActiveWindowName=strText End Function 

It will return the title in the active window, but I assume that 100 characters will suffice.

This code should give a function that returns the current header, and correctly adjust the length. (I currently don't have C #, so I can't verify this):

 [DllImport("user32.dll")] static extern IntPtr GetActiveWindow(); [DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)] static extern int GetWindowTextLength(IntPtr hWnd); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); public static string GetActiveWindowText() { IntPtr hWind = GetActiveWindow(); // Allocate correct string length first int length = GetWindowTextLength(hWnd); StringBuilder sb = new StringBuilder(length + 1); GetWindowText(hWnd, sb, sb.Capacity); return sb.ToString(); } 

You can then check the string to see what it contains. In the VBA example, input =ActiveWindowName() in A1 returns Microsoft Excel - Book1

+3
source

You need to do something like this:

 private bool CheckInputisinExcelCell() { Microsoft.Office.Core.CommandBarControl cmdEdited; cmdEdited=YourExcelApplicationobject.CommandBars.FindControl(Microsoft.Office.Core.MsoControlType.msoControlButton, 23, System.Reflection.Missing.Value, System.Reflection.Missing.Value); return cmdEdited.Enabled; } 
0
source

All Articles