以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  打印时报错,但是打印内容,打印动作没有问题  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=115707)

--  作者:873773115
--  发布时间:2018/3/13 8:23:00
--  打印时报错,但是打印内容,打印动作没有问题
打印时会报错
图片点击可在新窗口打开查看此主题相关图片如下:微信图片_20180313082141.jpg
图片点击可在新窗口打开查看

--  作者:873773115
--  发布时间:2018/3/13 8:25:00
--  
请老师帮忙看下
--  作者:有点甜
--  发布时间:2018/3/13 8:38:00
--  

贴出你写的代码。如果是office文件的打印,尽量用vba处理

 

http://www.foxtable.com/webhelp/scr/2121.htm

 


--  作者:873773115
--  发布时间:2018/3/13 8:43:00
--  
  Case "送货单"
                Relations.Delete("asdf")
                Dim dt3 As Table = Tables("出库主表_出库主表")
                Dim ids As String
                If dt3.Current IsNot Nothing Then
                    With Tables("出库主表_出库主表")
                        If .TopPosition > -1 Then \'如果选定区域包括数据行
                            For i As Integer = .TopPosition To .BottomPosition
                                ids = ids & ",\'" & dt3.Rows(i)("出库单号") & "\'"
                                .Rows(i)("是否打印") = True
                            Next
                        End If
                    End With
                    ids= ids.Trim(",")
                    
                    Dim cmd As new SQLCommand
                    Dim dt As DataTable
                    cmd.C
                    cmd.CommandText = "select * fro m {出库明细表} where 出库单号 in (" & ids & ")"
                    dt = cmd.ExecuteReader
                    Tables("出库主表_出库子表").DataSource  = dt
                    Tables("出库主表_出库子表").Sort = "出库序号"
                    Relations.Add("asdf",DataTables("出库主表_出库主表").DataCols("出库单号"),DataTables("出库主表_出库子表").DataCols("出库单号"))
                    
                    Dim Book As New XLS.Book(ProjectPath & "Attachments\\送货单.xls") \'打开模板
                    Dim fl As String = ProjectPath & "Reports\\送货单.xls"
                    Dim rt As prt.RenderText
                    Dim Sheet As XLS.Sheet = Book.Sheets(0)
                    With Sheet.PrintSetting
                        .LandScape = False
                        .MarginLeft = 8 \'左右边距设为20毫米
                        .MarginRight = 8
                        .MarginTop = 8 \'上下边距设为15毫米
                        .MarginBottom = 8
                    End With
                    Book.Build() \'生成报表
                    Book.Save(fl)
                    
                    Dim Proc As New Process
                    Proc.File = fl
                    Proc.Verb = "Print" \'指定动作
                    Proc.Start()
                End If


--  作者:873773115
--  发布时间:2018/3/13 8:44:00
--  
甜老师,用VBA该怎么写呢? 很奇怪的是 有的电脑会报错,有的又不会
--  作者:有点甜
--  发布时间:2018/3/13 8:45:00
--  

 

Dim App As New MSExcel.Application
Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open("D:\\Report.xls")
Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(1)
With Ws.PageSetup
    \'设置打印区域
    .PrintArea = "A1:H10"   \'打印工作表的指定区域
    .PrintArea = Ws.UsedRange.Address   \'打印工作表的使用区域
    .PrintTitleColumns = Ws.Columns("A:H").Address   \'打印列标题(在每一页的左边重复出现)
    .PrintTitleRows = Ws.Rows(1).Address \'打印行标题(在每一页的顶部重复出现)
    \'设置页面
    .PaperSize = MSExcel.XlPaperSize.xlPaperA4   \'纸张大小
    .LeftMargin = 30 \'页面左边距
    .RightMargin = 30\'页面右边距
    .TopMargin = 50  \'页面顶部边距
    .BottomMargin = 50   \'页面底部边距
    .HeaderMargin = 40   \'页面顶端到页眉的距离
    .FooterMargin = 40   \'页脚到页面底端的距离
    .CenterHorizontally = True   \'页面水平居中
    .CenterVertically = True \'页面垂直居中
    \'设置页眉
    .LeftHeader = "打印日期: &D" \'左页眉,&D表示日期
    .CenterHeader = "&""隶书,常规""&20 数据分析表"   \'中页眉,并将字体设置为隶书和20号字大小
    .RightHeader = "打印者: " & App.UserName \'右页眉
    \'设置页脚
    .LeftFooter = "文件: &F  &A" \'左页脚,&F表示文件名,&A表示工作表名
    .CenterFooter = ""   \'中页脚为空
    .RightFooter = "第 &P 页  共 &N 页"  \'右页脚
    \'打印模式
    .Orientation = MSExcel.xlPageOrientation.xlPortrait  \'纵向打印
    .Orientation = MSExcel.xlPageOrientation.xlLandscape \'横向打印
    .PrintHeadings = True\'打印行号和列标
    .PrintGridlines = True   \'打印网格线
    \'缩放打印
    .Zoom = False\'以下设置将缩印在一页内
    .FitToPagesWide = 1  \'按照1页的宽度打印
    .FitToPagesTall = 1  \'按照1页的高度打印
End With
App.Visible = True
Ws.PrintPreview
App.Quit


--  作者:873773115
--  发布时间:2018/3/13 8:56:00
--  
VBA的打印代码和EXCEL模板的代码  看起来完全不一样    现在急着用,一时还不会用VBA   甜老师,有其他方法能解决应用程序传输错误吗?
--  作者:有点甜
--  发布时间:2018/3/13 9:02:00
--  

Dim Proc As New Process
Proc.File = fl
Proc.Verb = "Print" \'指定动作
Proc.Start()

 

改成6楼代码即可。


--  作者:873773115
--  发布时间:2018/3/14 10:41:00
--  
Relations.Delete("asdf")
Dim dt3 As Table = Tables("订单主表_订单主表")
Dim ids As String
If dt3.Current IsNot Nothing Then
    With Tables("订单主表_订单主表")
        If .TopPosition > -1 Then \'如果选定区域包括数据行
            For i As Integer = .TopPosition To .BottomPosition
                ids = ids & ",\'" & dt3.Rows(i)("单号") & "\'"
            Next
        End If
    End With
    ids= ids.Trim(",")
    
    Dim cmd As new SQLCommand
    Dim dt As DataTable
    cmd.C
    cmd.CommandText = "select * fro m {订单明细表} where 单号 in (" & ids & ")"
    dt = cmd.ExecuteReader
    Tables("订单主表_订单子表").DataSource  = dt
    Tables("订单主表_订单子表").Sort = "序号"
    Relations.Add("asdf",DataTables("订单主表_订单主表").DataCols("单号"),DataTables("订单主表_订单子表").DataCols("单号"))
    
    Dim Book As New XLS.Book(ProjectPath & "Attachments\\订单表.xls") \'打开模板
    Dim fl As String = ProjectPath & "Reports\\订单表.xls"
    Dim rt As prt.RenderText
    Dim Sheet As XLS.Sheet = Book.Sheets(0)
    With Sheet.PrintSetting
        .LandScape = False
        .MarginLeft = 8 \'左右边距设为20毫米
        .MarginRight = 8
        .MarginTop = 8 \'上下边距设为15毫米
        .MarginBottom = 8
    End With
    Book.Build() \'生成报表
    Book.Save(fl)
    
    Dim App As New MSExcel.Application
    Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open(fl)
    Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(1)
Dim EndRow As Integer = Ws.Range("H65536").End(MSExcel.XlDirection.xlUp).Row
\'对A列从第1行开始向下查找,直到找到最后一个非空单元格为止,并得到其行号.也就是有内容的开始行
Dim FirstRow As Integer = Ws.Range("A1").End(MSExcel.XlDirection.xlDown).Row
\'对A列从第1行开始向下查找,直到找到最后一个非空单元格为止,并得到其行号.也就是有内容的开始行
Ws.Cells.PageBreak = MSExcel.XlPageBreak.xlPageBreakNone\'清除所有分页符
For i As Integer = FirstRow +15 To EndRow Step 18  \'每18行就分页

        Ws.Rows(i).PageBreak = MSExcel.XlPageBreak.xlPageBreakManual  
    
    With Ws.PageSetup
        .PrintArea =  "A" & FirstRow - 3 & ":h" & Endrow  \'打印工作表的指定区域
              \'设置页面
        .LeftMargin = 10 \'页面左边距
        .RightMargin = 10\'页面右边距
        .TopMargin = 10  \'页面顶部边距
        .BottomMargin = 10   \'页面底部边距
        .CenterHorizontally = True   \'页面水平居中
        .CenterVertically = True \'页面垂直居中
        \'设置页
        .Orientation = MSExcel.xlPageOrientation.xlPortrait
        \'\'缩放打印
        \'.Zoom = True \'以下设置将缩印在一页内
        .FitToPagesWide = 1  \'按照1页的宽度打印
        .FitToPagesTall = 1  \'按照1页的高度打印
    End With
Next


    App.Visible = True
    Ws.PrintPreview
    App.Quit
End If



请老师看下  这段代码无法实现18行分页是怎么回事呢

--  作者:有点甜
--  发布时间:2018/3/14 10:58:00
--  

1、弹出两个值看看

 

msgbox(FirstRow)

msgbox(EndRow)

 

2、我单独测试没问题,请上传具体实例测试。