如何传递对组合框的引用?

时间:2016-07-12 13:20:45

标签: vba ms-access printing combobox access-vba

这是我之前的帖子的延续:How to select a printer for entire job?,我基本上想要从Access数据库中的表单打印一系列报告。

其中涉及此帮助文档:https://msdn.microsoft.com/en-us/library/ee336132(v=office.12).aspx

我正在尝试使用活动打印机创建组合框,以暂时更改一系列文档的默认打印机。我惹上了“传递对ComboBox控件的引用”的地方......如何实现这个?

这是我到目前为止的代码,其中cboPrinterSelect是组合框名称:

Private Sub cboPrinterSelect_Load(Cancel As Integer)
    Call GetPrinterList
    ' I'm not sure about this next part either'
    cboPrinterSelect.Value = GetPrinterList.value
End Sub

'***************************************************
Private Sub cboPrinterSelect_AfterUpdate(Cancel As Integer)
    Set Application.Printer = Application.Printers(cboPrinterSelect.ListIndex)
End Sub
'***************************************************

Private Sub GetPrinterList(ctl As Control)
    Dim prt As Printer
    For Each prt In Printers
        ctl.AddItem prt.DeviceName
    Next prt
    ctl = Application.Printer.DeviceName    
End Sub

任何帮助/建议都将不胜感激。

修改

这是我的更新代码,仍然会抛出错误(在Andre的评论中描述):

Private Sub Form_Load()
    Call GetPrinterList(Me.cboPrinterSelect)
End Sub

'*********************************************************

Private Sub cboPrinterSelect_AfterUpdate(Cancel As Integer)

Set Application.Printer = Application.Printers(cboPrinterSelect.ListIndex)

End Sub

'***************************************************************

Private Sub GetPrinterList(ctl As Control)

Dim prt As Printer
For Each prt In Printers
    ctl.AddItem prt.DeviceName
Next prt
ctl = Application.Printer.DeviceName

End Sub

2 个答案:

答案 0 :(得分:1)

第一部分是GetPrinterList()的来电,必须进入表格On Load事件。

Private Sub Form_Load()
    Call GetPrinterList(Me.cboPrinterSelect)
End Sub

那应该是所有缺失的。

此行预先选择了默认打印机:

ctl = Application.Printer.DeviceName

并确保cboPrinterSelect的RowSourceType为值列表

答案 1 :(得分:1)

尝试以下代码,它将在User_Form的combo_box中显示所有已连接的打印机。

Public Sub GetPrinters()

    ' Use a large array (supports up to 100 printers).
    ReDim result(100) As String
    Dim wshNetwork, allPrinters As Object

    ' Get the network object
    Set wshNetwork = CreateObject("WScript.Network")
    Set allPrinters = wshNetwork.EnumPrinterConnections

    ' Printers collection has two elements for each printer.
    For i = 0 To allPrinters.Count - 1 Step 2
        Print_Series.cboPrinterSelect.AddItem allPrinters.Item(i + 1)
    Next

    ' call your user form and combo box will have all active printers
    Print_Series.Show

End Sub