casperharkin
New Member
- Joined
- Mar 1, 2023
- Messages
- 1
- Office Version
- 2016
- Platform
- Windows
Hi all, first time poster here. Hoping someone will be able to help me.
I am looking to get the zoom percentage figure from the status bar of a notepad window. The text between “Ln 1, Col 1” and “Windows (CRLF)” on the bottom right of the window.
Here is my attempt at doing what I want in VBA;
I am looking to get the zoom percentage figure from the status bar of a notepad window. The text between “Ln 1, Col 1” and “Windows (CRLF)” on the bottom right of the window.
Here is my attempt at doing what I want in VBA;
VBA Code:
Option Explicit
Declare PtrSafe Function AccessibleObjectFromWindow Lib "OLEACC.DLL" (ByVal hwnd As LongPtr, ByVal dwId As Long, ByVal riid As LongPtr, ppvObject As Any) As Long
Declare PtrSafe Function AccessibleChildren Lib "oleacc" (ByVal paccContainer As IAccessible, ByVal iChildStart As Long, ByVal cChildren As Long, ByRef rgvarChildren As Any, ByRef pcObtained As Long) As Long
Declare PtrSafe Function IIDFromString Lib "ole32.dll" (ByVal lpsz As LongPtr, ByVal lpiid As LongPtr) As LongPtr
Declare PtrSafe Function FindWindowEx Lib "User32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr
Declare PtrSafe Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Const S_OK = &H0
Const OBJID_SELF = &H0&
Const CHILDID_SELF = &H0&
Const ID_ACCESSIBLE As String = "{00020400-0000-0000-C000-000000000046}"
Sub Test()
' This will only work with notepad open.
Dim tGUID(0 To 3) As Long
Dim oIAc As IAccessible
Dim vAcc As Variant, vAccChild As Variant
Dim i As Long
Dim hWndParentNotepad As Long
Dim hWndstatusbar As Long
'Getting the hWnds of the windows / controls
hWndParentNotepad = FindWindow("Notepad", vbNullString)
hWndstatusbar = FindWindowEx(hWndParentNotepad, ByVal 0&, "msctls_statusbar32", vbNullString)
If IIDFromString(StrPtr(ID_ACCESSIBLE), VarPtr(tGUID(0))) = S_OK Then
If AccessibleObjectFromWindow(hWndstatusbar, OBJID_SELF, VarPtr(tGUID(0)), oIAc) = S_OK Then
Set vAcc = oIAc
AccessibleChildren vAcc, 3, 1, vAcc, 1
' I can see the Control I care about and the number of children.
Debug.Print "Control: " & vAcc.accName(CHILDID_SELF) & " has " & vAcc.accChildCount & " Children. "
' Control/window "Status Bar" should have the below five children.
' 1st Child has the Name " "
' 2nd Child has the Name " Ln 1, Col 1"
' 3rd Child has the Name " 100%"
' 4th Child has the Name " Windows (CRLF)"
' 5th Child has the Name " UTF-8"
' Now I want the Name of the third child of the Status Bar; The Zoom Percentage
' but I cant seem to work out how to access it, can anyone help me out?
AccessibleChildren vAcc, 3, 1, vAccChild, 1
Debug.Print vAccChild.accName(CHILDID_SELF)
End If
End If
End Sub