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

时间:2018-01-17 14:26:02

标签: vba winapi printing sap

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

  1. 我已将其处理为hWnd变量(请参阅第1行评论);

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

  3. 然后我按了按钮OK(请参阅第4行评论);

  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;
    

    请帮我改变打印机名称。

1 个答案:

答案 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台打印机:

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