access 2003中自动完成报表的页面设置
使用过Access 2003报表的用户会发现一个问题,报表的页边距在设计时往往事先已经设计好,预览打印都很正常,但当下次再预览或打印时就会发现报表的页边距不正常了,这主要是由于在access2003中页面设置在设计时的参数不能很好的保留,造成了今后使用报表时出现页边距不理想的状况,能否利用VBA程序的方法让报表的页边距自动控制,而无需人工来再次进行页面的设置。
这里给出一个过程,利用这个过程,在打印报表时会自动按照定义的页面参数进行页面的自动设置。
将下面的过程放在模块中,使用时只需调用这个过程即可进行报表页面的自动设置。
调用方法:
Setprint(报表名称,方向,上边距,下边距,左边距,右边距)。
'例:设置纵向打印 Setprint("销售报表", 1, 20, 10, 20, 10)
'例:设置横向打印 Setprint("销售报表", 0, 20, 10, 20, 10)
Option Compare Database
Type str_PRTMIP
strRGB As String * 28
End Type
Type type_PRTMIP
xLeftMargin As Long
yTopMargin As Long
xRightMargin As Long
yBotMargin As Long
fDataOnly As Long
xWidth As Long
yHeight As Long
fDefaultSize As Long
cxColumns As Long
yColumnSpacing As Long
xRowSpacing As Long
rItemLayout As Long
fFastPrint As Long
fDatasheet As Long
End Type
Type str_DEVMODE
RGB As String * 94
End Type
Type type_DEVMODE
strDeviceName As String * 16
intSpecVersion As Integer
intDriverVersion As Integer
intSize As Integer
intDriverExtra As Integer
lngFields As Long
intOrientation As Integer
intPaperSize As Integer
intPaperLength As Integer
intPaperWidth As Integer
intScale As Integer
intCopies As Integer
intDefaultSource As Integer
intPrintQuality As Integer
intColor As Integer
intDuplex As Integer
intResolution As Integer
intTTOption As Integer
intCollate As Integer
strFormName As String * 16
lngPad As Long
lngBits As Long
lngPW As Long
lngPH As Long
lngDFI As Long
lngDFr As Long
End Type
'以下过程用来进行报表页面自动设置,过程所带参数为(报表名称,方向,上边距,下边距,左边距,右边距)。
'例:设置纵向打印 Setprint("销售报表", 1, 20, 10, 20, 10)
'例:设置横向打印 Setprint("销售报表", 0, 20, 10, 20, 10)
Sub Setprint(strName, fx, sbj, xbj, zbj, ybj)
Const DM_PORTRAIT = 1
Const DM_LANDSCAPE = 2
Dim DevString As str_DEVMODE
Dim dm As type_DEVMODE
Dim PrtMipString As str_PRTMIP
Dim PM As type_PRTMIP
Dim strDevModeExtra As String
Dim rpt As Report
' Opens report in Design view.
DoCmd.OpenReport strName, acDesign
Set rpt = Reports(strName)
PrtMipString.strRGB = rpt.PrtMip
LSet PM = PrtMipString
PM.yTopMargin = sbj * 1440 * 0.0394 ' 设置上边距。
PM.yBotMargin = xbj * 1440 * 0.0394 ' 设置下边距。
PM.xLeftMargin = zbj * 1440 * 0.0394 ' 设置左边距。
PM.xRightMargin = ybj * 1440 * 0.0394 ' 设置右边距。
LSet PrtMipString = PM ' 更新属性。
rpt.PrtMip = PrtMipString.strRGB
If Not IsNull(rpt.PrtDevMode) Then
strDevModeExtra = rpt.PrtDevMode
DevString.RGB = strDevModeExtra
LSet dm = DevString
dm.lngFields = dm.lngFields or _
dm.intOrientation ' Initialize fields.
If fx = 1 Then
dm.intOrientation = DM_PORTRAIT '设置纵向打印
Else
dm.intOrientation = DM_LANDSCAPE '设置横向打印
End If
LSet DevString = dm ' Update property.
Mid(strDevModeExtra, 1, 94) = DevString.RGB
rpt.PrtDevMode = strDevModeExtra
End If
DoCmd.Save acReport, strName
Set rpt = Nothing
On Error GoTo err1
DoCmd.OpenReport strName, acViewPreview
err1:
End Sub