我从SAP获得了一些数据,然后我按下了SAP的“打印”按钮以打印数据。 然后出现PRINT窗口:
我已将其处理为hWnd变量(请参阅第1行评论);
然后我想将默认打印机名称更改为“Microsoft Print to PDF”(我不知道如何更改);
然后我按了按钮OK(请参阅第4行评论);
以下是代码:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
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
Const BM_CLICK = &HF5
'''
hWnd = FindWindow("#32770", "Print") 'LINE 1 comment;
Childhwnd = FindWindowEx(hWnd, ByVal 0&, "Button", "OK")';
'ON THIS LINE NEED TO INSERT CODE THAT CHANGES PRINTER NAME;
SendMessage Childhwnd, BM_CLICK, 0, ByVal 0& 'LINE 4 comment;
请帮我改变打印机名称。
答案 0 :(得分:0)
问题已解决(适用于Windows 10): 请注意,所需的代码是:
'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's 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
解释代码: 我认为他们最多可以有30台打印机: