Sub command_click Form1.printform End
sub ----
这样即可通过打印窗体FORM1的方法把GRID1的数据打印出来,遗憾的是只能打印GRID1中显示的数据部分,显示不出来的则无法打印,
而且这种打印方法很象屏幕硬拷贝把其他控件也打印出来。也不能灵活的控制字体等。
Function prnt1 (x As Integer, y As Integer, font As Single, txt As
String) printer.CurrentX = x printer.CurrentY =
y printer.FontBold = False printer.FontSize = font printer.Print
txt End Function
Sub command1_click Dim fnt As Single Dim
pp as integer Pp=0'设置开始页码0 Dim stry,strx,strx1,stry1,linw,page1,p As
Integer Static a(8) As Integer'定义打印的列数 ss$ =
"内部结算存入款对帐单"'定义表头 kan = 0 For i = 0 To 8 a(i) = 1500'定义每列宽 kan
= kan + a(i)'计算表格总宽度 Next
dd = prnt1(4000, 700, 18, ss$)'打印标题 printer.Line (strx
- 50, stry - 30) -(strx + kan - 10, stry - 30) For j = 0 To gridrow
- 1'gridrow为所要打印的行数 grid1.row = j strx = strx1 printer.Line (strx
- 50, stry - 30) -(strx + kan - 10, stry - 30) p = p + 1 For i =
0 To 8 grid1.col = i dd = prnt1(strx, stry, fnt, grid1.text) strx
= strx + a(i) Next
If p > page1 Then'next page p =
0 strx = strx1 'line last line printer.Line (strx - 50, stry +
linw) -(strx + kan - 10, stry + linw) stry = stry1 'line
col For n = 0 To 8 printer.Line (strx - 30, stry - 30) -(strx -
30, stry + (page1 + 2) * linw) strx = strx +
a(n) Next printer.Line (strx - 30, stry - 30) -(strx - 30, stry +
(page1 + 2) * linw) pp=pp+1 foot$="第 "+cstr(pp)+"页" dd =
prnt1(strx - 30-1000, stry + (page1 + 2) * linw+100, 10,
foot$)'打印页角码
printer.NewPage'next page dd = prnt1(4000, 700, 18,
ss$) '打印标题 strx = strx1 stry = stry1 printer.Line (strx - 50,
stry - 30)- (strx + kan - 10, stry - 30)' print first
row Else stry = stry + linw End If Next st = stry If p
< page1 Then '在最后页剩余划空行 For o = p To page1 + 1 strx =
strx1 printer.Line (strx - 50, stry - 30) -(strx + kan - 10, stry -
30) stry = stry + linw Next End If stry = stry1 strx =
strx1 stry = stry1 'line col For n = 0 To 8 printer.Line (strx -
30, stry - 30)- (strx - 30, stry + (page1 + 2) * linw) strx = strx +
a(n) Next printer.Line (strx - 30, stry - 30)- (strx - 30, stry +
(page1 + 2) * linw) pp=pp+1 foot$="第 "+cstr(pp)+"页" dd =
prnt1(strx - 30-1000, stry + (page1 + 2) * linw+100, 10,
foot$)'打印页角码
printer.EndDoc'打印结束 Endsub
----
这种方法通过灵活的编程可以方便地调整字体、字型、线形、页面、纸张大小等。可打印出比较满意的效果。如果你的计算机上装有MICROSOFT WORD 和MICRO
EXCEL,最精彩的用法还是把GRID 的表格通过VB发送到MICROSOFT WORD 及MICRO EXCEL。生成MICROSOFT WORD
和MICRO EXCEL 表格。这样就可以充分利用MICROSOFT WORD 和MICRO
EXCEL的打印、编辑功能打印出更理想的效果。下面逐一介绍。
screen.MousePointer = 11 Set
msword = CreateObject("word.basic")
Dim AppID, ReturnValue appID
= Shell("d:\office97\office\WINWORD.EXE", 1) ' Run Microsoft
Word.
msword.AppActivate "Microsoft Word" 'msword.AppActivate
"Microsoft Word", 1 full Screen.MousePointer = 0 End
Sub
---- 2、写入以下过程full()
Sub full() Dim i As Integer, j
As Integer, col As Integer, row As Integer Dim cellcontent As
String Me.Hide cols = 4'表格的列数 row =
gridrow'打印表的行数 msword.filenewdefault msword.MsgBox
"正在建立MS_WORD报表, 请稍候.......", "",
-1 msword.leftpara msword.screenupdating
0 msword.tableinserttable , col, row, , , 16,
167 msword.startofdocument for j=0 to gridrow'
表格的行数 grid1.row=j For i = 1 To cols Gri1d.col=i If
IsNull(grid1.text) Then cellcontent$ = "" Else cellcontent$ =
grid1.text End If msword.Insert
cellcontent$ msword.nextcell Next i Next
j msword.tabledeleterow msword.startofdocument msword.tableselectrow msword.tableheadings
1 msword.centerpara 'msword.startdocument msword.screenrefresh msword.screenupdating
1 msword.MsgBox " 结束", "", -1 Me.Show
Private Sub command3_Click() Dim i As Integer Dim j As
Integer Dim xlApp As Excel.Application Dim xlBook As
Excel.Workbook Dim xlSheet As Excel.Worksheet
Set xlApp =
CreateObject("Excel.Application") xlApp.Visible = True 'Set xlBook =
xlApp.Workbooks.Add 'On Error Resume Next Set xlBook =
xlApp.Workbooks.Add 'Open("d:\text2.xls") Set xlSheet =
xlBook.Worksheets(1) xlSheet.Cells(6, 1) = "i" For i = 0 To
gridrow grid1.Row = i For j = 0 To 6 Grid1.Col = j
If
IsNull(Grid1.Text) = False Then xlSheet.Cells(i + 5, j + 1) =
Grid1.Text End If Next j Next i Exit Sub
没有相关教程