Hi nobi,
folgender Code in eine Form und einen Button "Command1"
damit wir z.B. die Steuernummer an der richtigen Position im Formular gedruckt
Option Explicit
Private Declare Function GetDeviceCaps Lib "gdi32" _
(ByVal hDC As Long, ByVal nIndex As Long) As Long
Public pLeft As Long
Public pTop As Long
Public pRight As Long
Public pBottom As Long
Public pWidth As Long
Public pHeight As Long
Public sMsg As String
' Ermitteln der nichtbedruckbaren Ränder
Public Sub GetPhysicalPageSize(Printer As Object, _
pLeft As Long, pTop As Long, _
Optional pWidth As Long, Optional pHeight As Long, _
Optional pRight As Long, Optional pBottom As Long, _
Optional ByVal ScaleMode As Integer = vbMillimeters)
Const PHYSICALWIDTH = 110
Const PHYSICALHEIGHT = 111
Const PHYSICALOFFSETX = 112
Const PHYSICALOFFSETY = 113
'RequestStart:
On Error GoTo Errhandler:
With Printer
' Papiergröße
pWidth = .ScaleX(GetDeviceCaps(.hDC, _
PHYSICALWIDTH), vbPixels, ScaleMode)
pHeight = .ScaleY(GetDeviceCaps(.hDC, _
PHYSICALHEIGHT), vbPixels, ScaleMode)
' nicht bedruckbare Ränder: Links und Oben
pLeft = .ScaleX(GetDeviceCaps(.hDC, _
PHYSICALOFFSETX), vbPixels, ScaleMode)
pTop = .ScaleY(GetDeviceCaps(.hDC, _
PHYSICALOFFSETY), vbPixels, ScaleMode)
' nicht bedruckbare Ränder: Rechts und unten
pRight = pWidth - pLeft - _
.ScaleX(.ScaleWidth, .ScaleMode, ScaleMode)
pBottom = pHeight - pTop - _
.ScaleY(.ScaleHeight, .ScaleMode, ScaleMode)
End With
Exit Sub
Errhandler:
End Sub
Private Sub Get_nicht_bedruckbare_Bereiche(Optional ByVal oAnzeigen As Boolean _
= True)
' Einstellungen des Standard-Druckers
RequestStart:
On Error GoTo Errhandler
GetPhysicalPageSize Printer, pLeft, pTop, _
pWidth, pHeight, pRight, pBottom, vbMillimeters
sMsg = "Papiergröße: " & CStr(pWidth) & _
" x " & CStr(pHeight) & "mm" & vbCrLf & _
"Linker Rand: " & CStr(pLeft) & "mm" & vbCrLf & _
"Rechter Rand: " & CStr(pRight) & "mm" & vbCrLf & _
"Oberer Rand: " & CStr(pTop) & "mm" & vbCrLf & _
"Unterer Rand: " & CStr(pBottom) & "mm"
If oAnzeigen = True Then _
MsgBox sMsg, vbInformation + vbOKOnly
Exit Sub
Errhandler:
MsgBox "Fehler"
End Sub
Private Sub Command1_Click()
Drucke_Formular
End Sub
Private Sub Form_Load()
Me.Show
DoEvents
Call Get_nicht_bedruckbare_Bereiche
End Sub
Private Sub Drucke_Formular()
Dim nPosX&, nPosY&, sText$
sText = "471100/081500" 'Steuernummer
nPosX = Int(62 * 56.7) 'Umrechnung mm in dpi
nPosY = Int(32.5 * 56.7)
nPosX = nPosX - pLeft 'ggf. nicht druckbaren Bereich abziehen
nPosY = nPosY - pTop
With Printer
.PaperSize = vbPRPSA4
.Orientation = vbPRORPortrait
.ScaleTop = 0
.ScaleLeft = 0
.FontName = "Arial"
.FontSize = 12
.FontBold = False
End With
Printer.Print "" 'damit der Drucker initialisiert
With Printer
.CurrentX = nPosX 'Abstand Links
.CurrentY = nPosY 'Abstand Oben
End With
Printer.Print sText
Printer.EndDoc 'wenn Document fertig
'printer.NewPage 'bei mehrseitigem Druck
End Sub hoffe das hilft dir weiter
cu,
VBRunner |