I am working on VSTO Project. I need to detect the input focus in Excel vsto project.
I want to check whether focus is on excel cell or it is on other excel component like find dialog, document action pane or any other excel built-in dialog.
Is this possible to detect?
As shown in screen shot, I want to know whether input focus is set to excel cell or not?
To check if a cell contains text, select the output cell, and use the following formula: =IF(ISTEXT(cell), value_to_return, ""). For our example, the cell we want to check is A2, and the return value will be Yes. In this scenario, you'd change the formula to =IF(ISTEXT(A2), "Yes", "").
The active cell is the cell surrounded by a black border. Data can only be entered into the active cell. Even if more than one cell is selected, there is still only one active cell at a time. Excel spreadsheets are divided into cells.
To compare text strings in a case-sensitive way, you can use the EXACT function. The Excel EXACT function compares two text strings, taking into account upper and lower case characters, and returns TRUE if they are the same, and FALSE if not.
This will get the title of the active window (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 on the active window, but I assume a length of 100 characters will be enough.
This code should give a function that returns the current title, and correctly adjust for length. (I currently do not have c# installed, so I can't test 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 hWnd = 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 should then be able to test the string to see what it contains. In the VBA example, entering =ActiveWindowName()
into A1 returns Microsoft Excel - Book1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With