sábado, 12 de agosto de 2023

Imprimir una hoja desde un userform


Creamos un formulario como se muestra y escribimos el siguiente código:

Public wsh As Worksheet
Private Sub UserForm_Initialize()
Dim WshNetwork, Printers, i
Set WshNetwork = CreateObject("WScript.Network")
Set Printers = WshNetwork.EnumPrinterConnections
For i = 0 To Printers.Count - 1 Step 2
    Me.cboimpresora.AddItem Printers.Item(i + 1)
Next
Me.cboimpresora.Text = Application.ActivePrinter
Me.txtcopias.Text = 2
Me.cmdaceptar.Default = True
End Sub
Private Sub SpinButton1_Change()
Me.txtcopias.Text = Me.SpinButton1.Value
End Sub
Private Sub cmdaceptar_Click()
Set wsh = Worksheets("Hoja1")
'Me.cboimpresora.Text = Application.ActivePrinter
wsh.PrintOut Copies:=Me.txtcopias.Value, ActivePrinter:=Me.cboimpresora.Text
'Application.ActivePrinter = Me.cboimpresora.Text
Unload Me
End Sub
Private Sub cboimpresora_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 27 Then Unload Me
End Sub
Private Sub cmdaceptar_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 27 Then Unload Me
End Sub
Private Sub SpinButton1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 27 Then Unload Me
End Sub
Private Sub txtcopias_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 27 Then Unload Me
End Sub


Imprimir una hoja desde un userform


No hay comentarios.:

Publicar un comentario