IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

Imprimer le contenu d'une variable [XL-2002]


Sujet :

Macros et VBA Excel

  1. #1
    Membre éclairé
    Homme Profil pro
    En reconversion
    Inscrit en
    Mai 2006
    Messages
    509
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : En reconversion

    Informations forums :
    Inscription : Mai 2006
    Messages : 509
    Par défaut Imprimer le contenu d'une variable
    Bonsoir a tous,

    Est il possible d'imprimer le contenu d'une variable string et de faire une mise en page pour l'imprimer au format souhaité ?
    Si oui quel est la démarche pour y arriver.

    Merci d'avance.

  2. #2
    Membre averti Avatar de Bric-a-brac
    Homme Profil pro
    Bidouilleur
    Inscrit en
    Mars 2017
    Messages
    14
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine et Marne (Île de France)

    Informations professionnelles :
    Activité : Bidouilleur

    Informations forums :
    Inscription : Mars 2017
    Messages : 14
    Par défaut
    Bonjour,

    Créer un fichier csv temporaire ...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    Sub Une_Idee()
     
    Dim Var As String, Chemin As String, Fichier As String
    Var = "Ma variable à imprimer"
     
    ' Chemin => C:\WINDOWS\Temp\
    Chemin = Environ("systemroot") & "\Temp\"
    Fichier = "tmp.csv"
     
    ' Ecrire un fichier text type csv
    Close
    Open Chemin & Fichier For Output As #1
    Print #1, Var
    Close
     
    ' Ouverture du fichier pour l'impression
    Workbooks.OpenText Filename:=Chemin & Fichier
     
    ' Lancer boîte de dialogue d'impression
    Application.Dialogs(xlDialogPrint).Show
     
    ' Fermer et supprimer le fichier
    Workbooks(Fichier).Close False
    Kill Chemin & Fichier
     
    End Sub
    Cordialement

  3. #3
    Membre éclairé
    Homme Profil pro
    En reconversion
    Inscrit en
    Mai 2006
    Messages
    509
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : En reconversion

    Informations forums :
    Inscription : Mai 2006
    Messages : 509
    Par défaut
    Merci Bric-a brac,

    ça pourrait correspondre a ce que je recherche mais y a t il possibilité de gérer la taille de la police et le position sur la feuille imprimé (centré).

  4. #4
    Membre averti Avatar de Bric-a-brac
    Homme Profil pro
    Bidouilleur
    Inscrit en
    Mars 2017
    Messages
    14
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine et Marne (Île de France)

    Informations professionnelles :
    Activité : Bidouilleur

    Informations forums :
    Inscription : Mars 2017
    Messages : 14
    Par défaut
    En passant par l'enregistreur de macro ...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    Sub Une_Idee()
     
    Dim Var As String, Chemin As String, Fichier As String
    Var = "Ma variable à imprimer"
     
    ' Chemin => C:\WINDOWS\Temp\
    Chemin = Environ("systemroot") & "\Temp\"
    Fichier = "tmp.csv"
     
    ' Ecrire un fichier text type csv
    Close
    Open Chemin & Fichier For Output As #1
    Print #1, Var
    Close
     
    ' Ouverture du fichier pour l'impression
    Workbooks.OpenText Filename:=Chemin & Fichier
     
    ' Modifier la taille de la police
    Range("A1").Select 
    Selection.Font.Size = 48
     
    ' Centrer sur la page
    With Workbooks(Fichier).ActiveSheet.PageSetup
        .CenterHorizontally = True
        .CenterVertically = True
    End With
     
    ' Lancer boîte de dialogue d'impression
    Application.Dialogs(xlDialogPrint).Show
     
    ' Fermer et supprimer le fichier
    Workbooks(Fichier).Close False
    Kill Chemin & Fichier
     
    End Sub
    Cordialement

  5. #5
    Membre éclairé
    Homme Profil pro
    En reconversion
    Inscrit en
    Mai 2006
    Messages
    509
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : En reconversion

    Informations forums :
    Inscription : Mai 2006
    Messages : 509
    Par défaut
    OK merci Bric-a brac je suis en train de l'adapter mais c'est exactement ce que je recherchais.

    Je ne sais pas comment tu arrive au résultât avec l'enregistreur, mais je ne pense pas que j'aurais trouvé tout seule même avec l'aide de l'enregistreur.

    un grand Merci pour les infos que tu m'as donné.

    Puis je abuser encore un peu si j'ai deux variables et les avoir sur deux lignes, comment je peux faire, car avec les fichier csv je suis un peu perdu.

  6. #6
    Membre averti Avatar de Bric-a-brac
    Homme Profil pro
    Bidouilleur
    Inscrit en
    Mars 2017
    Messages
    14
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine et Marne (Île de France)

    Informations professionnelles :
    Activité : Bidouilleur

    Informations forums :
    Inscription : Mars 2017
    Messages : 14
    Par défaut
    Bonsoir,

    Le .csv n’est ni plus ni moins qu'un fichier texte on aurait pu mettre un .txt, mais à l’ouverture Excel n’aurait pas eu le même comportement en fonction du contenu de tes variables.
    Dans le cas d’un fichier texte il faudrait paramétrer le format de colonne et surtout délimiter les données (tabulation, point-virgule, etc...)
    L’avantage avec le csv est qu’Excel l’ouvre avec une donnée par ligne (si Local:=True)
    En bref plus facile à traiter pour ce cas

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    Option Explicit
     
    Sub ImpVar()
     
    Dim Var(1 To 3) As String ' 3 étant le nombre de variables à stocker
    Dim Chemin As String, Fichier As String
    Dim i As Integer
     
    Var(1) = "Ma variable à imprimer"
    Var(2) = "Une, autre !"
    Var(3) = "etc ..."
     
    ' Chemin => C:\WINDOWS\Temp\
    Chemin = Environ("systemroot") & "\Temp\"
    Fichier = "tmp.csv"
     
    ' Ecrire 1 variable/ligne dans un fichier text type csv
    Close
    Open Chemin & Fichier For Output As #1
    For i = 1 To UBound(Var)
        Print #1, Var(i)
    Next i
    Close
     
    ' Ouverture du fichier pour l'impression
    Workbooks.OpenText Filename:=Chemin & Fichier, Local:=True
     
    ' Modifier la taille de la police
    Range(Cells(1, 1), Cells(UBound(Var), 1)).Select
    Selection.Font.Size = 48
     
    ' Centrer sur la page
    With Workbooks(Fichier).ActiveSheet.PageSetup
        .CenterHorizontally = True
        .CenterVertically = True
    End With
     
    ' Lancer boîte de dialogue d'impression
    Application.Dialogs(xlDialogPrint).Show
     
    ' Fermer et supprimer le fichier
    Workbooks(Fichier).Close False
    Kill Chemin & Fichier
     
    End Sub
    Pour ce qui est de l’enregistreur …
    Tu lances l’enregistreur, tu changes la taille de la police et tu fais ta mise en page tu arrêtes l’enregistreur.
    Ensuite il faut élaguer le code du module en ne gardant que les instructions qui t’intéresse et faire des tests en cas de doute.

    Cordialement

  7. #7
    Invité
    Invité(e)
    Par défaut
    Bonjour,
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    Option Explicit  Private Type DOCINFO
          pDocName As String
          pOutputFile As String
          pDatatype As String
      End Type
    #If VBA7 Then
      Private PtrSafe Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
      Private PtrSafe Declare Function EndDocPrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
      Private PtrSafe Declare Function EndPagePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
      Private PtrSafe Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, ByVal pDefault As Long) As Long
      Private PtrSafe Declare Function StartDocPrinter Lib "winspool.drv" Alias "StartDocPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pDocInfo As DOCINFO) As Long
      Private PtrSafe Declare Function StartPagePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
      Private PtrSafe Declare Function WritePrinter Lib "winspool.drv" (ByVal hPrinter As Long, pBuf As Any, ByVal cdBuf As Long, pcWritten As Long) As Long
    #Else
      Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
      Private Declare Function EndDocPrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
      Private Declare Function EndPagePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
      Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, ByVal pDefault As Long) As Long
      Private Declare Function StartDocPrinter Lib "winspool.drv" Alias "StartDocPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pDocInfo As DOCINFO) As Long
      Private Declare Function StartPagePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
      Private Declare Function WritePrinter Lib "winspool.drv" (ByVal hPrinter As Long, pBuf As Any, ByVal cdBuf As Long, pcWritten As Long) As Long
    #End If
    Sub test()
    MyPrintOut "HP LaserJet P2015 PCL6", "Regardes comme c'est magique !"
    End Sub
      Sub MyPrintOut(Imprimante As String, Txt As String)
          Dim lhPrinter As Long
          Dim lReturn As Long
          Dim lpcWritten As Long
          Dim lDoc As Long
          Dim sWrittenData As String
          Dim MyDocInfo As DOCINFO
          lReturn = OpenPrinter(Imprimante, lhPrinter, 0)
          If lReturn = 0 Then
              MsgBox "The Printer Name you typed wasn't recognized."
              Exit Sub
          End If
          MyDocInfo.pDocName = "AAAAAA"
          MyDocInfo.pOutputFile = vbNullString
          MyDocInfo.pDatatype = vbNullString
          lDoc = StartDocPrinter(lhPrinter, 1, MyDocInfo)
          Call StartPagePrinter(lhPrinter)
          sWrittenData = Txt & vbFormFeed
          lReturn = WritePrinter(lhPrinter, ByVal sWrittenData, _
             Len(sWrittenData), lpcWritten)
          lReturn = EndPagePrinter(lhPrinter)
          lReturn = EndDocPrinter(lhPrinter)
          lReturn = ClosePrinter(lhPrinter)
      End Sub

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [ACC 97] imprimer le contenu d'une variable
    Par 6ril25 dans le forum VBA Access
    Réponses: 4
    Dernier message: 11/06/2008, 17h25
  2. [C#] Imprimer le contenu d'une fenêtre avec ascenseur
    Par biglong dans le forum Windows Forms
    Réponses: 6
    Dernier message: 06/08/2007, 23h29
  3. Imprimer/Visualiser le contenu d'une variable (VBS)
    Par Tartenpion dans le forum VBScript
    Réponses: 2
    Dernier message: 29/09/2005, 10h32
  4. imprimer le contenu d'une TPaintBox
    Par stimbo dans le forum C++Builder
    Réponses: 2
    Dernier message: 15/03/2004, 19h42
  5. [Kylix] Imprimer le contenu d'une boîte de dialogue
    Par cmp-france dans le forum EDI
    Réponses: 13
    Dernier message: 18/10/2003, 20h31

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo