Stampa un determinato foglio excel, in pdf o su carta tramite due pulsanti

Anonimo
2018-03-06T13:20:50+00:00

Buon giorno,

Ho creato un file Excel con tre fogli, sul primo immetto i dati, i quali modificano i fogli due e tre, nel primo foglio ho inserito 2 pulsanti, i quali dovrebbero stampare il foglio2 uno in pdf mentre il secondo inviare il foglio2 alla stampante. Ai pulsanti ho assegnato due macro, non essendo esperto le ho registrate, la prima scegliendo nel menù "scelta stampante" (Cute pdf Writer) e nella seconda il nome della stampante.

Il risultato è che stampa con entrambi o pdf o stampante a seconda di quale opzione c'è nel menù "Scelta stampante", in pratica la scelta della stampante non si registra  nella macro.

Sub Macro1()

'

' Macro1 Macro

' Stampa Foglio 1 su carta

'

'

    Sheets("Foglio 1").Select

    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _

        IgnorePrintAreas:=False

End Sub

Sub Macro2()

'

' Macro2 Macro

' Stampa Foglio 1 su pdf

'

'

    Sheets("Foglio 1").Select

    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _

        IgnorePrintAreas:=False

End Sub

Come vedete sono uguali.

Spero di essermi spiegato, se qualcuno mi può aiutare

grazie

Microsoft 365 e Office | Excel | Per la casa | Windows

Domanda bloccata. Questa domanda è stata eseguita dalla community del supporto tecnico Microsoft. È possibile votare se è utile, ma non è possibile aggiungere commenti o risposte o seguire la domanda.

0 commenti Nessun commento

10 risposte

Ordina per: Più utili
  1. Anonimo
    2018-03-07T17:18:55+00:00

    Ciao Sandro,

    Grazie mille

    Prova a sostituire il codice precedente con la seguente versione e fammi vedere l'eventuale messaggio che riscontri:

    '=========>>

    Option Explicit

    '--------->>

    Public Sub Stampa2Stampanti()

        Dim WB As Workbook

        Dim SH As Worksheet

        Dim sStampante As String

        Dim vVar As Variant

        Const sFoglioDaStampare = "Foglio2"                     '<<=== Modifica

        Const sStampantePDF = "pdf"                                 

        Set WB = ThisWorkbook

        Set SH = WB.Sheets(sFoglioDaStampare)

        With Application

            sStampante = .ActivePrinter

            vVar = GetPrinter(sStampantePDF)

        On Error GoTo XIT

            .ActivePrinter = vVar 

            SH.PrintOut

            .ActivePrinter = sStampante

            SH.PrintOut

        End With

    XIT:

    MsgBox vVar

    End Sub

    '--------->>

    Public Function GetPrinter(ByVal PrinterName As String) As String

        Dim arrDevices As Variant

        Dim Device As Variant

        Dim Devices As Variant

        Dim sPrinter As String

        Dim RegObj As Object

        Dim RegValue As String

        Const HKEY_CURRENT_USER = &H80000001

        Const sdevices As String = _

              "Software\Microsoft\Windows NT\CurrentVersion\Devices"

        Set RegObj = GetObject("winmgmts:{impersonationLevel=impersonate}!\." _

                                                        & "root\default:StdRegProv")

        RegObj.EnumValues HKEY_CURRENT_USER, sdevices, _

                                                                        Devices, arrDevices

        For Each Device In Devices

           RegObj.GetStringValue HKEY_CURRENT_USER, _

                                                            sdevices, Device, RegValue

            sPrinter = Device & " on " & Split(RegValue, ",")(1)

            If InStr(1, sPrinter, PrinterName, vbTextCompare) > 0 Then

                GetPrinter = sPrinter

                Exit Function

            End If

        Next

        Set RegObj = Nothing

    End Function

    '<<=========

    ===

    Regards,

    Norman

    La risposta è stata utile?

    0 commenti Nessun commento
  2. Anonimo
    2018-03-07T16:03:29+00:00

    Grazie mille

    La risposta è stata utile?

    0 commenti Nessun commento
  3. Anonimo
    2018-03-07T13:48:29+00:00

    Ciao Sandro,

     Mi da questo errore

    Sarebbe stato utile se avessi anche fornito uno screenshot del messaggio di errore.

    Comunque, in attesa di uno screenshot, pare che ci sia un problema con il nome del tuo stampante e, pertanto, fammi anche vedere uno sreenshot del nome completo del stampante pdf.

    ===

    Regards,

    Norman

    La risposta è stata utile?

    0 commenti Nessun commento
  4. Anonimo
    2018-03-07T08:27:01+00:00

    Mi da questo errore

    La risposta è stata utile?

    0 commenti Nessun commento
  5. Anonimo
    2018-03-06T15:54:12+00:00

    Ciao Sandro,

    Ho creato un file Excel con tre fogli, sul primo immetto i dati, i quali modificano i fogli due e tre, nel primo foglio ho inserito 2 pulsanti, i quali dovrebbero stampare il foglio2 uno in pdf mentre il secondo inviare il foglio2 alla stampante. Ai pulsanti ho assegnato due macro, non essendo esperto le ho registrate, la prima scegliendo nel menù "scelta stampante" (Cute pdf Writer) e nella seconda il nome della stampante.

    Il risultato è che stampa con entrambi o pdf o stampante a seconda di quale opzione c'è nel menù "Scelta stampante", in pratica la scelta della stampante non si registra  nella macro.

    Sub Macro1()

    '

    ' Macro1 Macro

    ' Stampa Foglio 1 su carta

    '

    '

        Sheets("Foglio 1").Select

        ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _

            IgnorePrintAreas:=False

    End Sub

    Sub Macro2()

    '

    ' Macro2 Macro

    ' Stampa Foglio 1 su pdf

    '

    '

        Sheets("Foglio 1").Select

        ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _

            IgnorePrintAreas:=False

    End Sub

    Come vedete sono uguali.

    Prova qualcosa del genere:

    '=========>>

    Option Explicit

    '--------->>

    Public Sub Stampa2Stampanti()

        Dim WB As Workbook

        Dim SH As Worksheet

        Dim sStampante As String

        Const sFoglioDaStampare = "Foglio2"               '<<=== Modifica

        Const sStampantePDF = "Cute pdf Writer"       '<<=== Modifica

        Set WB = ThisWorkbook

        Set SH = WB.Sheets(sFoglioDaStampare)

        With Application

            sStampante = .ActivePrinter

            .ActivePrinter = GetPrinter(sStampantePDF)

            SH.PrintOut

            .ActivePrinter = sStampante

            SH.PrintOut

        End With

    End Sub

    '--------->>

    Public Function GetPrinter(ByVal PrinterName As String) As String

        Dim arrDevices As Variant

        Dim Device As Variant

        Dim Devices As Variant

        Dim sPrinter As String

        Dim RegObj As Object

        Dim RegValue As String

        Const HKEY_CURRENT_USER = &H80000001

        Const sdevices As String = _

              "Software\Microsoft\Windows NT\CurrentVersion\Devices"

        Set RegObj = GetObject("winmgmts:{impersonationLevel=impersonate}!\." _

                                                        & "root\default:StdRegProv")

        RegObj.EnumValues HKEY_CURRENT_USER, sdevices, _

                                                                        Devices, arrDevices

        For Each Device In Devices

            RegObj.GetStringValue HKEY_CURRENT_USER, _

                                                            sdevices, Device, RegValue

            sPrinter = Device & " on " & Split(RegValue, ",")(1)

            If InStr(1, sPrinter, PrinterName, vbTextCompare) > 0 Then

                GetPrinter = sPrinter

                Exit Function

            End If

        Next

        Set RegObj = Nothing

    End Function

    '<<=========

    ===

    Regards,

    Norman

    La risposta è stata utile?

    0 commenti Nessun commento