PROBLEM SOLVED (for Windows 10): Please note that the required code:
'Declarations Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Public Declare Function GetWindow Lib "user32" _ (ByVal hwnd As Long, ByVal wCmd As Long) As Long Dim i 'index to the selected item Dim getPrintName As String ' the text of the selected item Dim getPrintNameLength As Long ' the length of the selected item text Dim printPDFFound As Boolean Dim ChildhwndPrint11 As Long Dim ChildhwndPrint1 As Long Dim ChildhwndPrint As Long Dim hwndPrint As Long Const GW_CHILD = 5 Const GW_HWNDNEXT = 2 Const CB_GETLBTEXTLEN = &H149 Const CB_GETLBTEXT = &H148 getPrintName = Space(20) 'Code Between Previous Lines LINE1 and LINE4 Used To Change The Print Name hwndPrint = hwnd ChildhwndPrint = GetWindow(hwndPrint, GW_CHILD) If ChildhwndPrint = 0 Then MsgBox "ChildhwndPrint not found." Exit Sub End If ChildhwndPrint1 = GetWindow(ChildhwndPrint, GW_HWNDNEXT) If ChildhwndPrint1 = 0 Then MsgBox "ChildhwndPrint1 not found." Exit Sub End If ChildhwndPrint11 = GetWindow(ChildhwndPrint1, GW_HWNDNEXT) If ChildhwndPrint11 = 0 Then MsgBox "ChildhwndPrint11 not found." Exit Sub End If Call SendMessage(ChildhwndPrint11, CB_SHOWDROPDOWN, True, 0) i = 0 printPDFFound = False 'getPrintName = "" Do Until (i = 30) Or (getPrintName = "Microsoft Print to PDF") Call SendMessage(ChildhwndPrint11, CB_SETCURSEL, i, 0) 'Call SendMessage(ChildhwndPrint11, CB_GETLBTEXT, 2, buffer) getPrintNameLength = SendMessage(ChildhwndPrint11, CB_GETLBTEXTLEN, ByVal CLng(i), ByVal CLng(0)) getPrintName = Space(getPrintNameLength) & vbNullChar getPrintNameLength = SendMessage(ChildhwndPrint11, CB_GETLBTEXT, ByVal CLng(i), ByVal getPrintName) getPrintName = Left(getPrintName, getPrintNameLength) 'MsgBox getPrintName If getPrintName = "Microsoft Print to PDF" Then printPDFFound = True End If i = i + 1 Loop If printPDFFound = False Then MsgBox "<Microsoft Print to PDF> print name was not found." Exit Sub End If
Explanation CODE: I suggested that they can be no more than 30 printers:
- I was looking for the print_name = ChildhwndPrint11 drop-down handler (I used the Visual Studio tool under the name Spy ++, separately for my macro, and this tool shows all the children of open windows on the desktop) [also, please find the standard declarations in the Windows API Viewer for MS Excel x64, I, ve downloaded it from the Internet).
- I showed a dropdown with CB_SHOWDROPDOWN
- so I went in cycles until I reached the maximum printer (up to 30 printers) or cycle until the name "Microsoft Print to PDF" was printed.
- I used a scroll down list containing printer names
- for each CURSEL item (from the drop-down list), I extracted the printer name and printer name
- If a printer is found, I set printPDFFound to TRUE
source share