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.
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.
Bonjour,
Créer un fichier csv temporaire ...
Cordialement
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
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é).
En passant par l'enregistreur de macro ...
Cordialement
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
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.
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
Pour ce qui est de l’enregistreur …
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
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
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
Partager