如何在VBA中更改打印机对话框的默认名称?(使用打印机对话框的句柄时)

Mad*_*ina 5 printing sap winapi vba

我从SAP获得了一些数据,然后我按下了SAP的打印按钮以打印数据.然后出现PRINT窗口:

  1. 我把它处理成了hWnd变量(请在LINE 1评论中找到);

  2. 然后我想将默认打印机名称更改为"Microsoft Print to PDF"(我不知道如何更改它);

  3. 然后我按了按钮OK(请找LINE 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;
Run Code Online (Sandbox Code Playgroud)

请帮我改变打印机名称.

Mad*_*ina 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
Run Code Online (Sandbox Code Playgroud)

解释代码:我认为它们最多可以有 30 台打印机:

  • 我搜索了 print-name 下拉菜单= ChildhwndPrint11 的处理程序(我使用了一个名为 Spy++ 的 Visual Studio 工具,单独用于我的宏,并且该工具显示桌面上打开的窗口的所有子窗口)[另外,请在 Windows API Viewer for MS Excel x64 中找到上述标准声明,我是从 Internet 下载的)
  • 我已经用 CB_SHOWDROPDOWN 显示了下拉菜单
  • 所以我循环直到满足最大打印机数(直到 30 台打印机)或循环直到打印名称为“Microsoft Print to PDF”
  • 我使用了包含打印机名称的向下滚动下拉列表
  • 对于每个CURSEL(下拉列表中的元素),我提取了打印机名称和打印机长度名称
  • 如果找到打印机,我将 printPDFFound 设置为 TRUE