gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:35:38 25 4
gpt4 key购买 nike

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

  • 我已将其处理为 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;

    请帮我更改打印机名称。

    最佳答案

    已解决的问题(适用于 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 drop-down= ChildhwndPrint11 的处理程序(我使用了一个名为 Spy++ 的 Visual Studio 工具,分别用于我的宏,并且这个工具显示了桌面上打开窗口的所有子项)[另外,请在 Windows API Viewer for MS Excel x64 中找到上述标准声明,我已从 Internet 下载)
  • 我用 CB_SHOWDROPDOWN
  • 显示了下拉菜单
  • 所以我循环直到遇到最大打印机(直到 30 台打印机)或循环直到打印名称为“Microsoft Print to PDF”
  • 我使用了包含打印机名称的向下滚动下拉列表
  • 对于每个 CURSEL(下拉列表中的元素),我提取了打印机名称和打印机长度名称
  • 如果找到打印机,我将 printPDFFound 设置为 TRUE
  • 关于vba - 如何在 VBA 中更改打印机对话框的默认名称? (使用打印机对话框的句柄时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48303411/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com