下面的打印代码报错:类型错误,请帮忙,谢谢,完整代码如下:
modMain模块代码:
Option Explicit
Public ChangeX As Boolean
Public mvarNew_TiTle As String '局部复制
Public mvarNew_PageSize As Integer '局部复制
Public mvarNew_PageWidth As Long '局部复制
Public mvarNew_PageHeight As Long '局部复制
Public mvarNew_RowHeight As Integer '局部复制
Public mvarNew_Border As Integer '局部复制
Public mvarNew_PageLeft As Integer '局部复制
Public mvarNew_PageTop As Integer '局部复制
Public mvarNew_Cols As String '局部复制
Public mvarNew_Head10 As String
Public mvarNew_Head11 As String
Public mvarNew_Head2 As String
Public Type PageSetting
sngPageLeft As Single
sngPageTop As Single
sngPageWidth As Single
sngPageHeight As Single
End TypelistViewPrint类代码:
Dim PageLeft As Single
Dim PageTop As SinglePrivate Type PrintText
caption As String
X As Single
Y As Single
strfont As String
strsize As Integer
bStrickThought As Boolean
End TypePrivate Type Cell
x1 As Single
y1 As Single
x2 As Single
y2 As Single
LineWidth As Integer
str As PrintText
End Type'要引发该事件,请遵循下列语法使用 RaiseEvent:
'RaiseEvent PrintGrid[(arg1, arg2, ... , argn)]
Public Event PrintPage()
Public Event ShowConfig()
'保持属性值的局部变量
'保持属性值的局部变量
'保持属性值的局部变量
Private mvarN_Head10 As String '局部复制
Private mvarN_Head11 As String '局部复制
Private mvarN_Head2 As String '局部复制
'保持属性值的局部变量
Private mvarGrid As Object '局部复制Public Property Set N_Grid(ByVal vData As Object)
'向属性指派对象时使用,位于 Set 语句的左边。
'Syntax: Set x.Grid = Form1
Set mvarGrid = vData
End PropertyPublic Property Get N_Grid() As Object
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.Grid
Set N_Grid = mvarGrid
End PropertyPublic Property Let N_Head2(ByVal vData As String)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.N_Head2 = 5
mvarN_Head2 = vData
End Property
Public Property Get N_Head2() As String
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.N_Head2
N_Head2 = mvarN_Head2
End PropertyPublic Property Let N_Head11(ByVal vData As String)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.N_Head11 = 5
mvarN_Head11 = vData
End Property
Public Property Get N_Head11() As String
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.N_Head11
N_Head11 = mvarN_Head11
End PropertyPublic Property Let N_Head10(ByVal vData As String)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.N_Head10 = 5
mvarN_Head10 = vData
End Property
Public Property Get N_Head10() As String
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.N_Head10
N_Head10 = mvarN_Head10
End PropertyPublic Property Let SetChange(ByVal vData As Boolean)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.SetChange = 5
ChangeX = vData
End PropertyPublic Property Get SetChange() As Boolean
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.SetChange
SetChange = ChangeX
End PropertyPublic Sub ShowConfig()
End SubPublic Property Let N_Cols(ByVal vData As String)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.New_Cols = 5
mvarNew_Cols = vData
End Property
Public Property Get N_Cols() As String
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.New_Cols
N_Cols = mvarNew_Cols
End PropertyPublic Property Let N_PageTop(ByVal vData As Integer)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.New_PageTop = 5
mvarNew_PageTop = vData
End Property
Public Property Get N_PageTop() As Integer
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.New_PageTop
N_PageTop = mvarNew_PageTop
End PropertyPublic Property Let N_PageLeft(ByVal vData As Integer)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.New_PageLeft = 5
mvarNew_PageLeft = vData
End Property
Public Property Get N_PageLeft() As Integer
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.New_PageLeft
N_PageLeft = mvarNew_PageLeft
End PropertyPublic Property Let N_Border(ByVal vData As Integer)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.New_Border = 5
mvarNew_Border = vData
End Property
Public Property Get N_Border() As Integer
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.New_Border
N_Border = mvarNew_Border
End PropertyPublic Property Let N_RowHeight(ByVal vData As Integer)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.New_RowHeight = 5
mvarNew_RowHeight = vData
End Property
Public Property Get N_RowHeight() As Integer
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.New_RowHeight
N_RowHeight = mvarNew_RowHeight
End PropertyPublic Property Let N_PageHeight(ByVal vData As Long)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.New_PageHeight = 5
mvarNew_PageHeight = vData
End PropertyPublic Property Get N_PageHeight() As Long
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.New_PageHeight
N_PageHeight = mvarNew_PageHeight
End PropertyPublic Property Let N_PageWidth(ByVal vData As Long)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.New_PageWidth = 5
mvarNew_PageWidth = vData
End Property
Public Property Get N_PageWidth() As Long
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.New_PageWidth
N_PageWidth = mvarNew_PageWidth
End Property
modMain模块代码:
Option Explicit
Public ChangeX As Boolean
Public mvarNew_TiTle As String '局部复制
Public mvarNew_PageSize As Integer '局部复制
Public mvarNew_PageWidth As Long '局部复制
Public mvarNew_PageHeight As Long '局部复制
Public mvarNew_RowHeight As Integer '局部复制
Public mvarNew_Border As Integer '局部复制
Public mvarNew_PageLeft As Integer '局部复制
Public mvarNew_PageTop As Integer '局部复制
Public mvarNew_Cols As String '局部复制
Public mvarNew_Head10 As String
Public mvarNew_Head11 As String
Public mvarNew_Head2 As String
Public Type PageSetting
sngPageLeft As Single
sngPageTop As Single
sngPageWidth As Single
sngPageHeight As Single
End TypelistViewPrint类代码:
Dim PageLeft As Single
Dim PageTop As SinglePrivate Type PrintText
caption As String
X As Single
Y As Single
strfont As String
strsize As Integer
bStrickThought As Boolean
End TypePrivate Type Cell
x1 As Single
y1 As Single
x2 As Single
y2 As Single
LineWidth As Integer
str As PrintText
End Type'要引发该事件,请遵循下列语法使用 RaiseEvent:
'RaiseEvent PrintGrid[(arg1, arg2, ... , argn)]
Public Event PrintPage()
Public Event ShowConfig()
'保持属性值的局部变量
'保持属性值的局部变量
'保持属性值的局部变量
Private mvarN_Head10 As String '局部复制
Private mvarN_Head11 As String '局部复制
Private mvarN_Head2 As String '局部复制
'保持属性值的局部变量
Private mvarGrid As Object '局部复制Public Property Set N_Grid(ByVal vData As Object)
'向属性指派对象时使用,位于 Set 语句的左边。
'Syntax: Set x.Grid = Form1
Set mvarGrid = vData
End PropertyPublic Property Get N_Grid() As Object
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.Grid
Set N_Grid = mvarGrid
End PropertyPublic Property Let N_Head2(ByVal vData As String)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.N_Head2 = 5
mvarN_Head2 = vData
End Property
Public Property Get N_Head2() As String
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.N_Head2
N_Head2 = mvarN_Head2
End PropertyPublic Property Let N_Head11(ByVal vData As String)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.N_Head11 = 5
mvarN_Head11 = vData
End Property
Public Property Get N_Head11() As String
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.N_Head11
N_Head11 = mvarN_Head11
End PropertyPublic Property Let N_Head10(ByVal vData As String)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.N_Head10 = 5
mvarN_Head10 = vData
End Property
Public Property Get N_Head10() As String
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.N_Head10
N_Head10 = mvarN_Head10
End PropertyPublic Property Let SetChange(ByVal vData As Boolean)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.SetChange = 5
ChangeX = vData
End PropertyPublic Property Get SetChange() As Boolean
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.SetChange
SetChange = ChangeX
End PropertyPublic Sub ShowConfig()
End SubPublic Property Let N_Cols(ByVal vData As String)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.New_Cols = 5
mvarNew_Cols = vData
End Property
Public Property Get N_Cols() As String
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.New_Cols
N_Cols = mvarNew_Cols
End PropertyPublic Property Let N_PageTop(ByVal vData As Integer)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.New_PageTop = 5
mvarNew_PageTop = vData
End Property
Public Property Get N_PageTop() As Integer
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.New_PageTop
N_PageTop = mvarNew_PageTop
End PropertyPublic Property Let N_PageLeft(ByVal vData As Integer)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.New_PageLeft = 5
mvarNew_PageLeft = vData
End Property
Public Property Get N_PageLeft() As Integer
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.New_PageLeft
N_PageLeft = mvarNew_PageLeft
End PropertyPublic Property Let N_Border(ByVal vData As Integer)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.New_Border = 5
mvarNew_Border = vData
End Property
Public Property Get N_Border() As Integer
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.New_Border
N_Border = mvarNew_Border
End PropertyPublic Property Let N_RowHeight(ByVal vData As Integer)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.New_RowHeight = 5
mvarNew_RowHeight = vData
End Property
Public Property Get N_RowHeight() As Integer
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.New_RowHeight
N_RowHeight = mvarNew_RowHeight
End PropertyPublic Property Let N_PageHeight(ByVal vData As Long)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.New_PageHeight = 5
mvarNew_PageHeight = vData
End PropertyPublic Property Get N_PageHeight() As Long
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.New_PageHeight
N_PageHeight = mvarNew_PageHeight
End PropertyPublic Property Let N_PageWidth(ByVal vData As Long)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.New_PageWidth = 5
mvarNew_PageWidth = vData
End Property
Public Property Get N_PageWidth() As Long
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.New_PageWidth
N_PageWidth = mvarNew_PageWidth
End Property
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.New_PageSize = 5
mvarNew_PageSize = vData
End PropertyPublic Property Get N_PageSize() As Integer
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.New_PageSize
N_PageSize = mvarNew_PageSize
End PropertyPublic Property Let N_TiTle(ByVal vData As String)
'向属性指派值时使用,位于赋值语句的左边。
'Syntax: X.New_TiTle = 5
mvarNew_TiTle = vData
End PropertyPublic Property Get N_TiTle() As String
'检索属性值时使用,位于赋值语句的右边。
'Syntax: Debug.Print X.New_TiTle
N_TiTle = mvarNew_TiTle
End PropertyPublic Sub PrintPage()
On Error GoTo Print_Err
Dim MyPage As PageSetting
MyPage.sngPageHeight = mvarNew_PageHeight - mvarNew_PageTop
MyPage.sngPageLeft = mvarNew_PageLeft
MyPage.sngPageTop = mvarNew_PageTop
MyPage.sngPageWidth = mvarNew_PageWidth - mvarNew_PageLeft - 18
Dim strHead1 As String
Dim strHead2 As String
Dim strHead3 As String
Dim Grid As ListView
Dim GridCols As String
Dim RowsHeight As Single
Dim LineWidth As Integer
Dim strTitle As String
strTitle = N_TiTle
strHead1 = N_Head10
strHead2 = N_Head11
strHead3 = N_Head2
Set Grid = N_Grid GridCols = N_Cols
RowsHeight = N_RowHeight
Const HeadHeight = 6
Printer.ScaleMode = 6
PageLeft = MyPage.sngPageLeft
PageTop = MyPage.sngPageTop
Dim AllPages As Long '总页数
Dim RowsPerPage As Long '每页表格行的数量
Dim PerPages As Long '每页的循环变量
Const GridLeft = 0
Const GridTop = 15 + HeadHeight * 2
RowsPerPage = Int((MyPage.sngPageHeight - GridTop - RowsHeight - 35) / RowsHeight) '计算每页的表格行数不包括列头 '给出行数
Dim sGridRow As Long
sGridRow = Grid.ListItems.Count
AllPages = Int((sGridRow + 0.1) / RowsPerPage) + 1
'--计算列宽
Dim lngScaleWidth As Long '表格总宽 计算比例时用
Dim Mycols() As String '存储要打印的列的一维数组 Mycols = Split(GridCols, ",") Dim MyColX(20) As Single '每一列左右坐标,第0列是mycolx(0)-mycolx(1)
MyColX(0) = 0
For i = 0 To UBound(Mycols) '获取每个需要打印的列宽
lngScaleWidth = lngScaleWidth + Grid.ColumnHeaders.Item(CInt(Mycols(i))).Width
MyColX(i + 1) = lngScaleWidth
Next i
LineWidth = N_Border
For PerPages = 1 To AllPages '每页循环
'--计算标题的左边
Dim titleLonger As Long '-标题共长多少字节
Dim titleLeft As Single
titleLonger = LenB(strTitle)
titleLeft = (MyPage.sngPageWidth - titleLonger * 4) / 2
'--打印标题
printCellOut 0, 0, 0, 0, 0, titleLeft, 0, strTitle, "宋体", 16, False
'--打印头1
printCellOut 0, 0, 0, 0, 0, 0, 15, strHead1, "", 9, False
'--打印头2
printCellOut 0, 0, 0, 0, 0, 0, 15 + HeadHeight, strHead3, "", 9, False
'--计算右对齐的左边
Dim HeadLeft3 As Single
HeadLeft3 = MyPage.sngPageWidth - (LenB(strHead2) * 2)
'--打印头3
printCellOut 0, 0, 0, 0, 0, HeadLeft3, 15 + HeadHeight, strHead2, "", 9, False
'--打印表格(0,28)
Dim NowCol, NowRow As Long
'-打印列头+++++++++++++++++++++++++++++++++++++ 列 头 ++++++++++++++++++++++++++++++++++++++
NowRow = 0
For NowCol = 0 To UBound(Mycols) '一共有几列
printCellOut GridLeft + ((MyColX(NowCol) / lngScaleWidth) * MyPage.sngPageWidth), GridTop + RowsHeight * NowRow, _
GridLeft + ((MyColX(NowCol + 1) / lngScaleWidth) * MyPage.sngPageWidth), GridTop + RowsHeight * (NowRow + 1), _
LineWidth, GridLeft + ((MyColX(NowCol) / lngScaleWidth) * MyPage.sngPageWidth) + 1, GridTop + RowsHeight * NowRow + 1, _
Grid.ColumnHeaders.Item(CInt(Mycols(NowCol))), "宋体", 9, False
Next NowCol
'-打印列头+++++++++++++++++++++++++++++++++++++ 列 头 ++++++++++++++++++++++++++++++++++++++
'-打印表格主体
For NowRow = 1 To RowsPerPage
If Not (NowRow + (PerPages - 1) * RowsPerPage) > sGridRow Then
For NowCol = 0 To UBound(Mycols) '所有列
If Mycols(NowCol) = "1" Then
printCellOut GridLeft + ((MyColX(NowCol) / lngScaleWidth) * MyPage.sngPageWidth), GridTop + RowsHeight * NowRow, _
GridLeft + ((MyColX(NowCol + 1) / lngScaleWidth) * MyPage.sngPageWidth), GridTop + RowsHeight * (NowRow + 1), _
LineWidth, GridLeft + ((MyColX(NowCol) / lngScaleWidth) * MyPage.sngPageWidth) + 1, GridTop + RowsHeight * NowRow + 1, _
Grid.ListItems.Item(NowRow + (PerPages - 1) * RowsPerPage).Text, "宋体", 9, False
Else
printCellOut GridLeft + ((MyColX(NowCol) / lngScaleWidth) * MyPage.sngPageWidth), GridTop + RowsHeight * NowRow, _
GridLeft + ((MyColX(NowCol + 1) / lngScaleWidth) * MyPage.sngPageWidth), GridTop + RowsHeight * (NowRow + 1), _
LineWidth, GridLeft + ((MyColX(NowCol) / lngScaleWidth) * MyPage.sngPageWidth) + 1, GridTop + RowsHeight * NowRow + 1, _
Grid.ListItems.Item(NowRow + (PerPages - 1) * RowsPerPage).SubItems(CInt(Mycols(NowCol)) - 1), "宋体", 9, False
End If
Next NowCol
End If
Next NowRow
'打印页码
printCellOut 0, 0, 0, 0, 0, (MyPage.sngPageWidth - 12) / 2, GridTop + RowsHeight * (NowRow + 1) + 2, "第" + CStr(PerPages) + "页", "", 9, False
Printer.EndDoc
Next PerPages
MsgBox "打印完成! ", vbInformation
Exit Sub
Print_Err:
'清除打印事件
Printer.KillDoc
MsgBox "对不起,打印发生错误,请与供应商联系。 " & vbCrLf _
& Err.Description, vbExclamation
Exit Sub
End Sub
Private Sub PrintCell(prnCell As Cell) 'On Error GoTo err1
Printer.ScaleMode = 6
If Not prnCell.LineWidth = 0 Then
Printer.DrawWidth = prnCell.LineWidth
End If
If Not Printer.FillColor = 0 Then
Printer.Line (prnCell.x1, prnCell.y1)-(prnCell.x2, prnCell.y2), , BF
Else
Printer.FillStyle = 1
Printer.Line (prnCell.x1, prnCell.y1)-(prnCell.x2, prnCell.y2), , B
End If
If prnCell.str.strfont = "" Then
prnCell.str.strfont = "宋体"
End If
Printer.Font = prnCell.str.strfont
If prnCell.str.strsize = 0 Then
prnCell.str.strsize = 12
End If
Printer.FontSize = prnCell.str.strsize
Printer.FontStrikethru = prnCell.str.bStrickThought
Printer.CurrentX = prnCell.str.X
Printer.CurrentY = prnCell.str.Y
Printer.Print prnCell.str.caption
Exit Sub
'err1:
' MsgBox Err.Description
End SubPrivate Sub printCellOut(x1 As Single, y1 As Single, x2 As Single, y2 As Single _
, LineWidth As Integer, _
strx As Single, stry As Single, _
strcaption As String, strfont As String, _
strsize As Integer, bThought As Boolean)
Dim printWords As Cell
printWords.x1 = x1 + PageLeft
printWords.y1 = y1 + PageTop
printWords.x2 = x2 + PageLeft
printWords.y2 = y2 + PageTop
printWords.LineWidth = LineWidth
printWords.str.X = strx + PageLeft
printWords.str.Y = stry + PageTop
printWords.str.caption = strcaption
printWords.str.strfont = strfont
printWords.str.strsize = strsize
printWords.str.bStrickThought = bThought
If printWords.x2 < 0 Then
printWords.x2 = 0
End If
If printWords.x1 < 0 Then
printWords.x1 = 0
End If
If printWords.y1 < 0 Then
printWords.y1 = 0
End If
If printWords.y2 < 0 Then
printWords.y2 = 0
End If
If printWords.str.X < 0 Then
printWords.str.X = 0
End If
If printWords.str.Y < 0 Then
printWords.str.Y = 0
End If
PrintCell printWords
End Sub
If lstPro.ListItems.Count = 0 Then Exit Sub'打印列表
If MsgBox("真的要打印【预订单】列表吗?(Y/N) " & vbCrLf _
& "请设置打印机的纸张:A4 纵向 ", vbInformation + vbYesNo) = vbNo Then
Exit Sub
End If
Dim ptGrid As listViewPrint
'建立打印对象
On Error GoTo Err1
Dim strPageLeft As String
Dim strPageTop As String
Dim PageTop As Long
Dim PageLeft As Long
Set ptGrid = New listViewPrint
ptGrid.N_Border = 1
ptGrid.N_Cols = "1,2,3,4,5,6,7,8,9,10"
Set ptGrid.N_Grid = lstPro
ptGrid.N_TiTle = "【预订单】"
ptGrid.N_Head10 = "制表人:" & UserText
ptGrid.N_Head2 = "制表时间:" & Now
ptGrid.N_PageLeft = XLeft
ptGrid.N_PageTop = XTop
ptGrid.N_PageHeight = 290
ptGrid.N_PageWidth = 200
ptGrid.N_RowHeight = 6
ptGrid.PrintPage
Set ptGrid = Nothing
Exit Sub
Err1:
MsgBox "对不起,打印列表错误。 " & vbCrLf & vbCrLf & Err.Description, vbInformation
Exit Sub
ieoa.net/Listprint.rar
调用时代码见上面我最后一个代码贴