| auteur : SilkyRoad |
Ce chapitre de la FAQ présente quelques exemples pour imprimer les classeurs. Lorsque vous faites des essais ou que vous souhaitez
simplement avoir une idée du résultat de votre travail, utilisez l'option d'aperçu avant impression. Vous économiserez ainsi de l'encre,
du papier et du temps.
Plus généralement, il faut toujours avoir une réflexion sur l'utilisation du papier pour ne pas le gaspiller.
|
| auteur : SilkyRoad |
Pour imprimer le classeur, utilisez:
Vba |
ActiveWorkbook. PrintOut Copies:= 3 , Collate:= True
|
Vba |
Worksheets (" Feuil2 " ). PrintOut
|
pour imprimer une plage de cellules:
Vba |
Worksheets (" Feuil1 " ). Range (" A1:D10 " ). printOut
|
|
| auteur : SilkyRoad |
Pour effectuer un aperçu de la Feuille nommée "Feuil2", utilisez la méthode PrintPreview:
Vba |
Worksheets (" Feuil2 " ). PrintPreview
|
Vous pouvez aussi effectuer l'aperçu avant impression en utilisant les boîtes de dialogues intégrées d'Excel:
Vba |
Application. Dialogs (xlDialogPrintPreview). Show
|
Remarque:
L'argument False permet de rendre inactifs les boutons "Mise en page" et "Marges".
Vba |
Application. Dialogs (xlDialogPrintPreview). Show False
|
|
| auteur : SilkyRoad |
Cette macro boucle sur les liens de la feuille active et ouvre le fichier lié s'il s'agit d'un classeur.
Chaque feuille de ce classeur est ensuite imprimée.
Vba |
Sub imprimerPageActiveEt_Liensclasseurs ()
Dim Lien As Hyperlink
Dim I As Byte
Application. ScreenUpdating = False
ActiveSheet. PrintOut
For Each Lien In ActiveSheet. Hyperlinks
If Right (Range (Lien. Range . Address ). Hyperlinks (1 ). Address , 4 ) = " .xls " Then
Range (Lien. Range . Address ). Hyperlinks (1 ). Follow NewWindow:= False
ActiveWorkbook. PrintOut
ActiveWorkbook. Close
End If
Next
Application. ScreenUpdating = True
End Sub
|
|
| auteur : SilkyRoad | Vba |
Sub impressionNoirEtBlanc ()
With Worksheets (" Feuil1 " )
. PageSetup . BlackAndWhite = True
. PrintOut
. PageSetup . BlackAndWhite = False
End With
End Sub
|
|
| auteur : SilkyRoad |
La procédure suivante affiche la boîte de dialogue d'impression, en précisant le nombre de copies par défaut (3).
Vba |
Application. Dialogs (xlDialogPrint). Show , , , 3
|
Cet autre exemple affiche la boîte de dialogue permettant de choisir l'imprimante pour l'édition:
Vba |
If Application. Dialogs (xlDialogPrinterSetup). Show = True Then Feuil1. printOut
|
|
| auteur : SilkyRoad |
Si les macros sont activées dans le classeur, vous pouvez utiliser l'évènement Workbook_BeforePrint.
Vba |
Private Sub Workbook_BeforePrint (Cancel As Boolean)
Cancel = True
End Sub
|
Cet évènement survient avant l'impression. L'impression commence uniquement à l'issue de cette procédure.
Le paramètre Cancel = True bloque toute impression.
|
| auteur : SilkyRoad | Vba |
Sub listeImprimantes_et_Statut ()
Dim objWMIService As Object, colInstalledPrinters As Object, objPrinter As Object
Dim nomPC As String , Resultat As String
nomPC = " . "
Set objWMIService = GetObject (" winmgmts: " & _
" {impersonationLevel=impersonate}!\\ " & nomPC & " \root\cimv2 " )
Set colInstalledPrinters = objWMIService. execQuery (" Select * from Win32_Printer " )
For Each objPrinter In colInstalledPrinters
Resultat = Resultat & objPrinter. Name & " imprimante active : " & objPrinter. Default & vbLf
Next
MsgBox Resultat
End Sub
|
|
| auteur : SilkyRoad | Vba |
Sub ProprietesImprimantes ()
Dim objWMIService As Object, colItems As Object
Dim objItem As Object
Dim strComputer As String
Dim i As Byte
On Error Resume Next
strComputer = " . "
Set objWMIService = GetObject (" winmgmts:\\ " & strComputer & " \root\cimv2 " )
Set colItems = objWMIService. execQuery (" Select * from Win32_printerConfiguration " , , 48 )
For Each objItem In colItems
i = i + 1
Cells (1 , i) = " bitsPerPel: " & objItem. bitsPerPel
Cells (2 , i) = " Caption: " & objItem. Caption
Cells (3 , i) = " Collate: " & objItem. Collate
Cells (4 , i) = " Color: " & objItem. Color
Cells (5 , i) = " Copies: " & objItem. Copies
Cells (6 , i) = " Description: " & objItem. Description
Cells (7 , i) = " deviceName: " & objItem. deviceName
Cells (8 , i) = " displayFlags: " & objItem. displayFlags
Cells (9 , i) = " displayFrequency: " & objItem. displayFrequency
Cells (10 , i) = " ditherType: " & objItem. ditherType
Cells (11 , i) = " driverVersion: " & objItem. driverVersion
Cells (12 , i) = " Duplex: " & objItem. Duplex
Cells (13 , i) = " formName: " & objItem. formName
Cells (14 , i) = " horizontalResolution: " & objItem. horizontalResolution
Cells (15 , i) = " ICMIntent: " & objItem. ICMIntent
Cells (16 , i) = " ICMMethod: " & objItem. ICMMethod
Cells (17 , i) = " logPixels: " & objItem. logPixels
Cells (18 , i) = " mediaType: " & objItem. mediaType
Cells (19 , i) = " Name: " & objItem. Name
Cells (20 , i) = " Orientation: " & objItem. Orientation
Cells (21 , i) = " paperLength: " & objItem. paperLength
Cells (22 , i) = " paperSize: " & objItem. PaperSize
Cells (23 , i) = " paperWidth: " & objItem. paperWidth
Cells (24 , i) = " pelsHeight: " & objItem. pelsHeight
Cells (25 , i) = " pelsWidth: " & objItem. pelsWidth
Cells (26 , i) = " printQuality: " & objItem. PrintQuality
Cells (27 , i) = " Scale: " & objItem. Scale
Cells (28 , i) = " SettingID: " & objItem. SettingID
Cells (29 , i) = " specificationVersion: " & objItem. specificationVersion
Cells (30 , i) = " TTOption: " & objItem. TTOption
Cells (31 , i) = " verticalResolution: " & objItem. verticalResolution
Cells (32 , i) = " XResolution: " & objItem. Xresolution
Cells (33 , i) = " YResolution: " & objItem. Yresolution
Columns (i). AutoFit
Next
End Sub
|
Cette autre adaptation permet de vérifier si l'imprimante est paramétrée pour imprimer en couleur ou en noir et blanc.
Vba |
Sub verifier_parametre_Couleur_NB_Imprimante_V02 ()
Dim objWMIService As Object, colItems As Object
Dim objItem As Object
Dim strComputer As String
On Error Resume Next
strComputer = " . "
Set objWMIService = GetObject (" winmgmts:\\ " & strComputer & " \root\cimv2 " )
Set colItems = objWMIService. execQuery (" Select * from Win32_printerConfiguration " , , 48 )
For Each objItem In colItems
Select Case objItem. Color
Case 1 : MsgBox objItem. Name & " : 'noir et blanc' "
Case 2 : MsgBox objItem. Name & " : 'couleur' "
End Select
Next
End Sub
|
|
| auteur : SilkyRoad | Vba |
Option Explicit
Declare Function CreateDC Lib " gdi32 " Alias " CreateDCA " _
(ByVal lpDriverName As String , ByVal lpDeviceName As String , _
ByVal lpOutput As Long, ByVal lpInitData As Long) As Long
Declare Function GetDeviceCaps Lib " gdi32 " _
(ByVal hdc As Long, ByVal nIndex As Long) As Long
Const HORZRES = 8
Const VERTRES = 10
Const LOGPIXELSX = 88
Const LOGPIXELSY = 90
Const PHYSICALWIDTH = 110
Const PHYSICALHEIGHT = 111
Const PHYSICALOFFSETX = 112
Const PHYSICALOFFSETY = 113
Sub ProprietesZoneImpressionImprimante ()
Dim dpiX As Long, dpiY As Long
Dim MarginLeft As Long, MarginRight As Long
Dim MarginTop As Long, MarginBottom As Long
Dim PrintAreaHorz As Long, PrintAreaVert As Long
Dim PhysHeight As Long, PhysWidth As Long
Dim Info As String , Cible As String
Dim HwndPrint As Long
Cible = " hp deskjet 940c "
HwndPrint = CreateDC (0 , Cible, 0 , 0 )
dpiX = GetDeviceCaps (HwndPrint, LOGPIXELSX)
Info = " Pixels X: " & dpiX & " dpi "
dpiY = GetDeviceCaps (HwndPrint, LOGPIXELSY)
Info = Info & vbCrLf & " Pixels Y: " & dpiY & " dpi "
MarginLeft = GetDeviceCaps (HwndPrint, PHYSICALOFFSETX)
Info = Info & vbCrLf & " Unprintable space on left: " & _
MarginLeft & " pixels ( " & Format (MarginLeft / dpiX, " 0.000 " ) & " inches) "
MarginTop = GetDeviceCaps (HwndPrint, PHYSICALOFFSETY)
Info = Info & vbCrLf & " Unprintable space on top: " & _
MarginTop & " pixels ( " & Format (MarginTop / dpiY, " 0.000 " ) & " inches) "
PrintAreaHorz = GetDeviceCaps (HwndPrint, HORZRES)
Info = Info & vbCrLf & " Printable space (Horizontal): " & _
PrintAreaHorz & " pixels ( " & Format (PrintAreaHorz / dpiX, " 0.000 " ) & " inches) "
PrintAreaVert = GetDeviceCaps (HwndPrint, VERTRES)
Info = Info & vbCrLf & " Printable space (Vertical): " & _
PrintAreaVert & " pixels ( " & Format (PrintAreaVert / dpiY, " 0.000 " ) & " inches) "
PhysWidth = GetDeviceCaps (HwndPrint, PHYSICALWIDTH)
Info = Info & vbCrLf & " Total space (Horizontal): " & _
PhysWidth & " pixels ( " & Format (PhysWidth / dpiX, " 0.000 " ) & " inches) "
MarginRight = PhysWidth - PrintAreaHorz - MarginLeft
Info = Info & vbCrLf & " Unprintable space on right: " & _
MarginRight & " pixels ( " & Format (MarginRight / dpiX, " 0.000 " ) & " inches) "
PhysHeight = GetDeviceCaps (HwndPrint, PHYSICALHEIGHT)
Info = Info & vbCrLf & " Total space (Vertical): " & _
PhysHeight & " pixels ( " & Format (PhysHeight / dpiY, " 0.000 " ) & " inches) "
MarginBottom = PhysHeight - PrintAreaVert - MarginTop
Info = Info & vbCrLf & " Unprintable space on bottom: " & _
MarginBottom & " pixels ( " & Format (MarginBottom / dpiY, " 0.000 " ) & " inches) "
MsgBox Info, , " Information "
End Sub
|
|
| auteur : SilkyRoad |
Cet exemple modifie la zone d'impression et les marges dans la feuille.
Vba |
Sub miseEnPageAvantImpression ()
With Feuil1. PageSetup
. PrintArea = " $A$1:$E$10 "
. LeftMargin = Application. InchesToPoints (0 . 5 )
. RightMargin = Application. InchesToPoints (0 . 75 )
. TopMargin = Application. InchesToPoints (1 . 5 )
. BottomMargin = Application. InchesToPoints (1 )
. HeaderMargin = Application. InchesToPoints (0 . 5 )
. FooterMargin = Application. InchesToPoints (0 . 5 )
End With
Feuil1. PrintPreview
End Sub
|
Il est aussi possible d'adapter la zone d'impression à une seule feuille pour une économie de papier:
Vba |
With Feuil1. PageSetup
. PrintArea = " A1:M100 "
. Zoom = False
. FitToPagesWide = 1
. FitToPagesTall = 1
End With
|
Vous pouvez réinitialiser la zone d'impression à la feuille complète ainsi:
Vba |
Feuil1. PageSetup . PrintArea = " "
|
Une autre solution:
Vba |
Feuil1. PageSetup . PrintArea = False
|
Pour centrer le contenu de la feuille lors de l'impression, utilisez:
Vba |
With Feuil1
. PageSetup . CenterHorizontally = True
. PageSetup . CenterVertically = True
. PrintOut
End With
|
Ce dernier exemple comment imprimer la première page en mode Paysage et la deuxième page en mode Portrait.
Vba |
With Feuil1
. PageSetup . Orientation = xlLandscape
. PrintOut From:= 1 , To := 1
. PageSetup . Orientation = xlPortrait
. PrintOut From:= 2 , To := 2
End With
|
|
| auteur : SilkyRoad | Vba |
Shell " notepad.exe /P " " C:\monRepertoire\leFichier.txt " " " , 1
|
|
| auteur : SilkyRoad | Vba |
Sub imprimerTexte ()
Open " LPT1: " For Output As #1
Print #1 , " Test d'impression. "
Print #1 , " Test 2eme ligne. "
Close #1
End Sub
|
|
| auteur : SilkyRoad | Vba |
Private Sub CommandButton1_Click ()
Me. Hide
Feuil1. PrintPreview
Me. Show
End Sub
|
|
| auteur : SilkyRoad |
Lorsque vous imprimez un onglet dont les données se suivent sur plusieurs pages verticales, il est possible de répéter
l'impression des premières lignes d'entête dans chaque page. Cela facilite ensuite la relecture du document.
Si par exemple vous souhaitez répéter l'impression des 4 premières lignes dans chaque page:
Menu Fichier
Mise en page
Onglet "Feuille"
Dans le champ "Lignes à répéter en haut", Sélectionnez les 4 lignes qui devront apparaître sur chaque page imprimée.
Vous pouvez aussi saisir directement $1:$4 dans ce champ.
Cliquez sur le bouton OK pour valider.
Cette propriété fonctionne aussi pour répéter l'impression de colonnes.
|
| auteur : SilkyRoad |
Vous pouvez spécifier le nom des feuilles à imprimer dans un tableau Array().
Vba |
Worksheets (Array (" Feuil1 " , " Feuil3 " )). PrintOut
|
|
| auteur : Bidou |
La gestion de la mise en page de votre feuille pour l'impression passe par l'intermédiaire de la propriété PageSetup.
Celle-ci renvoie un objet PageSetup qu'il faut manipuler.
Par exemple:
Vba |
With objFeuille. PageSetup
. CenterFooter = " &P "
. CenterHeader = " &F "
. FirstPageNumber = 3
. FitToPagesWide = 1
. Orientation = xlLandscape
. PrintGridlines = False
. PrintHeadings = False
End With
objFeuille. PrintOut 1 , 1 , 1 , False
|
Ce code imprime la feuille en mettant le nom du fichier dans l'en-tête, le numéro de page dans le pied, celui-ci commençant
à 3, force l'impression sur une page en largeur en mode paysage. Ni les lignes, ni les
numéros de lignes/colonnes ne seront imprimés.
|
| auteur : SilkyRoad | Vba |
Option Explicit
Option Base 1
Sub Test ()
MasqueContenuCell_Print Worksheets (1 ), Range (" C4:F15 " )
End Sub
Sub MasqueContenuCell_Print (Feuille As Worksheet, Plage As Range)
Dim FormatInit () As Variant
Dim Cell As Range
Dim i As Integer
ReDim Preserve FormatInit (Plage. Cells . Count )
For Each Cell In Plage
i = i + 1
FormatInit (i) = Cell. NumberFormat
Next Cell
Plage. NumberFormat = " ;;; "
Feuille. PrintOut
i = 0
For Each Cell In Plage
i = i + 1
Cell. NumberFormat = FormatInit (i)
Next Cell
End Sub
|
|
| auteur : SilkyRoad |
Cette méthode fonctionne aussi pour d'autres formats de fichiers.
Vba |
Option Explicit
Private Declare Function FindWindow Lib " user32 " Alias " FindWindowA " _
(ByVal lpClassName As String , ByVal lpWindowName As String ) As Long
Private Declare Function ShellExecute Lib " shell32.dll " Alias " ShellExecuteA " _
(ByVal hWnd As Long, ByVal lpOperation As String , ByVal lpFile As String _
, ByVal lpParameters As String , ByVal lpDirectory As String , ByVal nShowCmd As Long) As Long
Sub ImprimerFichier ()
Dim NomFichier As String
Dim x As Long
x = FindWindow (" XLMAIN " , Application. Caption )
NomFichier = " C:\dossier\rapport.pdf "
ShellExecute x, " print " , NomFichier, " " , " " , 1
End Sub
|
|
| auteur : SilkyRoad |
En utilisant les anciennes fonctions ExecuteExcel4Macro, il est possible d'identifier le nombre
total de pages qui seront imprimées dans la feuille active.
Vba |
MsgBox ExecuteExcel4Macro (" GET.DOCUMENT(50) " )
|
|
| auteur : SilkyRoad |
Cette solution, par macro, personnalise le pied de page central et lance l'impression.
La première page va contenir le texte ""Pied de page 1"". Les autres pages contiendront le texte "Pieds de Pages suivants".
Vba |
With Feuil1
. PageSetup . CenterFooter = " Pied de page 1 "
. PrintOut From:= 1 , To := 1
. PageSetup . CenterFooter = " Pieds de Pages suivants "
. PrintOut From:= 2 , To := ExecuteExcel4Macro (" GET.DOCUMENT(50) " )
End With
|
|
| auteur : SilkyRoad |
Saisissez le symbole deux fois pour que celui-ci soit visible dans l'en-tête ou le pied de page:
Nitro && Glycérine.
|
Consultez les autres F.A.Q's
Les sources présentés sur cette pages sont libre de droits,
et vous pouvez les utiliser à votre convenance. Par contre cette page de présentation de ces sources constitue une oeuvre intellectuelle protégée par les droits d'auteurs.
Copyright ©2008
Developpez LLC. Tout droits réservés Developpez LLC.
Aucune reproduction, même partielle, ne peut être faite de ce site et de
l'ensemble de son contenu : textes, documents et images sans l'autorisation
expresse de Developpez LLC. Sinon vous encourez selon la loi jusqu'à 3 ans
de prison et jusqu'à 300 000 E de dommages et intérets.
Cette page est déposée à la SACD.
|