看到一个VB打印模块,保存一下

合集下载

使用 VBA 实现自动打印功能

使用 VBA 实现自动打印功能

使用 VBA 实现自动打印功能自动打印功能是一项非常方便的功能,可以使我们的工作更高效。

VBA(Visual Basic for Applications)是一种用于自动化和自定义Microsoft Office应用程序的编程语言。

在本文中,我们将探讨如何使用VBA实现自动打印功能。

首先,我们需要打开Microsoft Office应用程序,例如Microsoft Word或Microsoft Excel。

接下来,我们将编写一段简单的VBA代码,以便在特定条件下自动执行打印操作。

在VBA编辑器中,我们可以在需要实现打印功能的模块内编写以下代码:```Sub AutoPrint()Dim ws As WorksheetSet ws = ThisWorkbook.ActiveSheet' 定义打印的区域Dim printRange As RangeSet printRange = ws.Range("A1:E10") ' 设置打印属性With ws.PageSetup.PrintArea = printRange.Address.Orientation = xlPortrait.FitToPagesWide = 1.FitToPagesTall = FalseEnd With' 打印ws.PrintOut' 清除打印设置ws.PageSetup.PrintArea = ""End Sub```以上代码使用了Excel的对象模型来实现自动打印功能。

首先,我们在代码中定义了一个名为`AutoPrint`的子程序。

在这个子程序中,我们首先设置了当前活动的工作表对象,使用`ThisWorkbook.ActiveSheet`来获取当前工作表。

接下来,我们设置了需要打印的区域,以A1到E10作为示例。

您可以根据实际需求更改打印区域。

在代码的第11行,我们使用`ws.Range("A1:E10")`将要打印的区域赋值给了`printRange`对象。

VBA 中的文件打印与打印设置技巧

VBA 中的文件打印与打印设置技巧

VBA 中的文件打印与打印设置技巧在使用 Visual Basic for Applications(VBA)编程时,我们经常会遇到需要打印文件或设置打印参数的需求。

本文将介绍一些在 VBA 中实现文件打印和打印设置的技巧,帮助你更高效地处理打印任务。

一、文件打印技巧1. 打印当前活动工作簿如果你需要打印当前活动的工作簿,可以使用`ActiveWorkbook.PrintOut`方法。

该方法会直接打印整个工作簿,默认使用默认打印机和设置。

示例代码:```vbaSub PrintActiveSheet()ActiveWorkbook.PrintOutEnd Sub```2. 打印指定工作表如果你只想打印工作簿中的某个特定工作表,可以使用`Worksheets.PrintOut`方法。

该方法允许你指定打印范围、打印份数等参数。

示例代码:```vbaSub PrintSpecificSheet()Worksheets("Sheet1").PrintOut Copies:=2, Collate:=TrueEnd Sub```3. 打印指定区域有时候,你可能只需要打印工作表中的某个特定区域。

在这种情况下,可以使用`Range.PrintOut`方法。

该方法可以打印指定的区域,并允许你设置打印份数、打印方向等参数。

示例代码:```vbaSub PrintSpecificRange()Range("A1:D10").PrintOut Copies:=3, PrintOrientation:=xlLandscapeEnd Sub```二、打印设置技巧1. 设置打印区域通过设置打印区域,你可以控制打印的范围,避免不必要的打印内容。

在VBA 中,我们可以使用`PageSetup.PrintArea`属性来设置打印区域。

示例代码:```vbaSub SetPrintArea()ActiveSheet.PageSetup.PrintArea = "$A$1:$D$10"End Sub```2. 设置打印标题打印标题可以帮助你更好地标识打印的内容。

vb窗体数据写入模版打印

vb窗体数据写入模版打印

vb窗体数据写入模版打印在VB窗体数据写入模板打印的标题下,我们来探讨一下如何利用VB窗体实现数据的写入和模板打印。

我们需要明确一下VB窗体是什么。

VB(Visual Basic)窗体是一种用于开发Windows应用程序的编程语言和开发环境。

它提供了一种可视化的方式来创建用户界面,使得开发者可以通过简单的拖拽和编写代码的方式来构建应用程序。

在VB窗体中,我们可以添加各种控件,如文本框、按钮、标签等,来实现各种功能。

在实现数据的写入和模板打印之前,我们需要先创建一个VB窗体应用程序,并添加相应的控件。

在本例中,我们可以添加一个文本框控件用于输入数据,一个按钮控件用于触发写入和打印操作。

我们来实现数据的写入功能。

当用户在文本框中输入数据后,点击按钮,程序将获取文本框中的数据,并将其写入到一个文件中。

我们可以使用VB的文件操作函数来实现这个功能。

具体步骤如下:1. 在窗体的代码区域,找到按钮的Click事件,并添加相应的处理代码。

代码如下:```Private Sub btnWrite_Click(sender As Object, e As EventArgs) Handles btnWrite.ClickDim data As String = txtData.Text '获取文本框中的数据Dim filePath As String = "data.txt" '指定写入文件的路径'将数据写入文件Using writer As New StreamWriter(filePath, True)writer.WriteLine(data)End UsingMessageBox.Show("数据写入成功!")End Sub```2. 在窗体的设计区域,双击按钮控件,进入按钮的Click事件处理代码编辑界面。

在代码编辑界面中,添加上述代码。

通过上述代码,当用户点击按钮时,程序将获取文本框中的数据,并将其写入到名为"data.txt"的文本文件中。

vb窗体数据写入模版打印

vb窗体数据写入模版打印

vb窗体数据写入模版打印窗体数据写入模版打印是一项在VB窗体应用程序中常见的任务。

通过将窗体中的数据写入模版,可以生成符合特定格式和布局要求的打印文件。

本文将介绍如何使用VB窗体实现数据写入模版打印,并探讨该技术的实际应用。

我们需要创建一个VB窗体应用程序,并添加必要的控件和代码。

假设我们的窗体中包含一些文本框和标签控件,用于输入和显示数据。

我们的目标是将这些数据写入到一个预定义的模版中,并将模版打印输出。

在VB窗体应用程序中,可以使用PrintDocument对象来实现打印功能。

首先,我们需要在窗体中添加一个PrintDocument控件,并设置其相关属性,如打印机名称、页面大小和页面边距等。

然后,在代码中编写PrintDocument的PrintPage事件处理程序,用于绘制打印页面的内容。

在PrintPage事件处理程序中,我们可以使用Graphics对象的DrawString方法来绘制文本。

可以根据需要设置文本的字体、大小、颜色和位置等属性。

此外,还可以使用Graphics对象的其他方法和属性来实现更复杂的绘图效果,如绘制线条、矩形和图形等。

对于数据写入模版的实现,我们可以将模版设计为一个文本文件,其中包含预留的位置用于插入数据。

在PrintPage事件处理程序中,可以读取模版文件的内容,并使用String.Format方法将数据插入到相应的位置。

例如,可以使用{0}、{1}等占位符表示数据应插入的位置,然后使用String.Format方法将实际数据替换占位符。

除了基本的数据插入功能,还可以根据实际需求扩展模版的功能。

例如,可以设计模版包含表格、图表或其他复杂的布局。

在PrintPage事件处理程序中,可以根据模版的结构和布局来绘制相应的内容。

在实际应用中,窗体数据写入模版打印可以应用于各种场景。

例如,可以使用该技术生成报告、发票、合同等文档。

通过将数据写入模版,可以确保生成的文档符合特定的格式和布局要求,提高文档的可读性和专业性。

vb 打印 实例-概述说明以及解释

vb 打印 实例-概述说明以及解释

vb 打印实例-概述说明以及解释1.引言1.1 概述在VB中,打印是一项常见的功能需求,通过打印可以将软件中的数据输出到纸张上,使得用户能够方便地查看和保存信息。

本文将介绍VB 中的打印功能,并通过实例演示和分析来帮助读者更好地理解和应用这一功能。

在VB中,打印功能的实现主要通过使用PrintDocument组件来完成,PrintDocument组件可以方便地控制打印输出的内容和样式。

通过设置PrintDocument的PageSettings属性和PrintPage事件来指定打印页面的设置和内容。

另外,也可以使用PrintPreviewDialog组件来提供打印预览的功能,使得用户可以在打印前预览输出的效果,从而调整需要打印的内容和格式。

本文将通过实例演示和分析来介绍如何在VB中实现打印功能,希望能帮助读者更加深入地了解和掌握这一重要的功能。

文章结构部分主要包括文章的大致组织和内容安排,旨在帮助读者更好地理解整篇文章的逻辑结构。

本文的文章结构如下:1. 引言1.1 概述:介绍VB打印功能的重要性和应用背景1.2 文章结构:介绍本文的大纲和各部分内容安排1.3 目的:阐明撰写本文的意义和目的2. 正文2.1 VB打印功能介绍:详细介绍VB语言中的打印功能的基本原理和特点2.2 VB打印实例演示:通过具体的实例演示,展示VB打印功能的具体应用和使用方法2.3 VB打印实例分析:对演示的实例进行分析和总结,深入探讨其应用场景和优势特点3. 结论3.1 总结:对整篇文章进行总结,强调VB打印功能的重要性和价值3.2 应用推广:探讨VB打印功能在实际应用中的推广和发展前景3.3 展望:展望未来VB打印功能的发展方向和潜力通过以上结构,读者可以清晰地了解本文的逻辑结构和内容安排,有助于他们更好地理解和消化文章所传达的信息。

1.3 目的:本文的目的在于介绍如何在VB(Visual Basic)中使用打印功能,并通过实例演示和分析,帮助读者更好地了解如何在自己的程序中实现打印功能。

vb打印技巧

vb打印技巧

ResetPrinterOrientation Me '设为默认值Printer.Orientation=VbPRORPortrait'纵向打印Printer.Orientation=VbPRORLandscape'横向打印Printer.PaperSize = vbPRPSA4 '定义纸张大小为A4设置打印页边距'你可以使用打印机的Scale属性来设置打印页边距。

下面的代码设置左边距为1/2英寸。

右边距为3/4英寸。

乘以1440是将英尺转换成twips。

Printer.ScaleLeft = -0.75 * 1440Printer.ScaleTop = -0.5 * 1440Printer.CurrentX = 0Printer.CurrentY = 0打印多页Rich Text打印对象在处理RichTextBox时由于忽视了Rich Text代码将不会工作得很好。

为了打印RichTextBox,要使用SelPrint命令,但是SelPrint不是非常得友好,它按照自己的方式打印然后产生退纸动作。

为了打印多页Rich Text,要找到每一页的开始和结束点,然后依此设置SelStart和SelLength,最后使用SelPrint打印出每一页。

以下是工作的每一个步骤:1、使用SaveFile命令保存编辑版本的文件。

2、转换编辑版本为打印版本:遍历这个文档,修改SelIndent和SelRightIndent的数值,使之适合于打印机(注意:编辑时的缩进数值对于屏幕是合适的,但不适用于打印机)。

做些其他的修改,比如:添加页眉,消除行号。

3、使用SelPrint循环一次打印一个页面。

这里,计算行数是一个技巧,找到LFCRs 是容易的,但是,也必须要计算一下行缠绕,它也算在了行统计中,以及绝对不允许发生的页面溢出情况。

4、使用SelRTF=""删除打印版本。

VB打印程序

VB打印程序
Label3.Enabled = True
Else
Label3.Caption = " 打印证书 "
Call anliu_on
Label3.Enabled = True
Exit Sub
End If
Call getdywzx '获取毕业证打印时的微调位置
End Sub
'-------------------------------------------------------------
'获取打新毕业证时的微调位置
Sub getdywzx()
Dim sjkm As String
On Error Resume Next
sjkm = App.Path & "\zyzgz.mdb"
End If
.MoveNext
Loop
End With
'------
End Sub
'---------------------------------------------------------
'打印新毕业证 ("+"号后面的变量是一个打印位置的微调值)
Printer.CurrentY = 68 + sxzy_s
Printer.CurrentX = 220 + sxzy_z
Printer.Print sxzy '所学专业
'------------第 4 行
Printer.CurrentY = 162 + sfzh_s
'------

vb控制打印机自动打印表格

vb控制打印机自动打印表格

VB控制EXCLE自动打印表格VB是常用的应用软件开发工具之一,由于VB的报表功能有限,而且一但报表格式发生变化,就得相应修改程序,给应用软件的维护工作带来极大的不便。

因此充分利用EXECL的强大报表功来实现报表功能。

但由于VB与EXCEL由于分别属于不同的应用系统,如何把它们有机地结合在一起,是一个值得我们研究的课题。

一、VB读写EXCEL表:VB本身提自动化功能可以读写EXCEL表,其方法如下:1、在工程中引用Microsoft Excel类型库:从"工程"菜单中选择"引用"栏;选择Microsoft Excel 9.0 Object Library(EXCEL2003),然后选择"确定"。

表示在工程中要引用EXCEL类型库。

2、在通用对象的声明过程中定义EXCEL对象:Dim excel As ObjectDim workbook As ObjectDim sheet As Object3、在程序中操作EXCEL表常用命令:Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象Set xlBook = xlApp.Workbooks.Open("文件名") '打开已经存在的EXCEL工件簿文件xlApp.Visible = True '设置EXCEL对象可见(或不可见)Set xlSheet = xlBook.Worksheets("表名") '设置活动工作表xlSheet.Cells(row, col) =值'给单元格(row,col)赋值xlSheet.PrintOut '打印工作表xlBook.Close (True) '关闭工作簿xlApp.Quit '结束EXCEL对象Set xlApp = Nothing '释放xlApp对象xlBook.RunAutoMacros (xlAutoOpen) '运行EXCEL启动宏xlBook.RunAutoMacros (xlAutoClose) '运行EXCEL关闭宏4、在运用以上VB命令操作EXCEL表时,除非设置EXCEL 对象不可见,否则VB程序可继续执行其它操作,也能够关闭EXCEL,同时也可对EXCEL进行操作。

vb窗体数据写入模版打印

vb窗体数据写入模版打印

vb窗体数据写入模版打印VB窗体数据写入模板打印是一种常见的应用场景,通过将窗体中的数据按照一定的格式写入到打印模板中,可以方便地生成符合需求的打印文件。

下面将介绍如何使用VB窗体实现数据写入模板打印的方法。

我们需要创建一个VB窗体,窗体上放置需要打印的控件,例如文本框、标签等。

在窗体的代码中,我们可以使用VB的数据操作方法,将窗体中的数据获取并处理。

在窗体加载的事件中,我们可以通过控件的属性或方法获取窗体中的数据。

例如,可以使用文本框的Text属性获取文本框中的内容,使用标签的Caption属性获取标签中的文本。

接下来,我们需要创建一个打印模板,模板可以是一个Word文档、Excel表格或其他格式的文件。

模板中可以预先定义好需要填充数据的位置,例如使用特定的标记或占位符表示需要填充的数据位置。

在代码中,我们可以使用VB的文件操作方法打开模板文件,并将窗体中的数据按照需要的格式写入到模板中。

例如,可以使用VB的文件读写方法读取模板文件的内容,并使用字符串替换方法将占位符替换为窗体中的数据。

我们可以使用VB的打印方法将填充好数据的模板文件进行打印。

例如,可以使用VB的打印操作方法将模板文件发送到打印机进行打印。

需要注意的是,在实现数据写入模板打印的过程中,我们需要遵循一些规范和要求。

首先,我们需要确保输出的内容不包含http地址,以避免泄露敏感信息或引起安全问题。

其次,我们需要避免输出公式,以免造成歧义或错误信息。

此外,我们还需要注意内容的整体格式,确保整洁规范,可以使用恰当的段落和标题来组织文章结构,使其易于阅读。

另外,我们应该避免使用图片链接,以免在文章中引起链接失效的问题。

同时,我们还需要避免重复问题的出现,以免降低文章的质量。

最后,我们需要确保文章内容准确严谨,避免歧义或错误信息的出现。

VB窗体数据写入模板打印是一种常见的应用场景,通过将窗体中的数据按照一定的格式写入到打印模板中,可以方便地生成符合需求的打印文件。

VB打印和打印预览功能

VB打印和打印预览功能

打印和打印预览功能今天,我们将制作一个能实现打印和打印预览功能的应用程序。

程序运行结果如图所示。

运行结果技术要点●打印预览功能的实现●打印功能的实现实现过程■新建项目打开Visual ,选择“新建项目”,在项目类型窗口中选择“Visual Basic项目”,在模板窗口中选择“Windows应用程序”,在名称域中输入“PrintExample”,然后选择保存路径。

单击“确认”。

■添加控件和设置属性向当前窗体上添加三个Button控件,将他们的Text属性改为和界面一致。

■添加组件类和要打印的文件通过菜单“项目|添加组件”为当前项目添加一个组件类,并添加一个需要打印的文件。

■添加代码'组件类中的代码Imports SystemImports ponentModelImports System.Windows.FormsImports System.DrawingImports System.Drawing.PrintingImports System.IONamespace Microsoft.Samples.WinForms.VB.PrintingExample5Public Class TextFilePrintDocumentInherits PrintDocumentPrivate printFont As FontPrivate streamToPrint As StreamReaderPublic Sub New(streamToPrint As StreamReader)MyBase.NewMe.streamToPrint = streamToPrintEnd SubOverrides Protected Sub OnBeginPrint(ev As PrintEventArgs)MyBase.OnBeginPrint(ev)printFont = new Font("Arial", 10)End Sub'Override the OnPrintPage to provide the printing logic for the documentOverrides Protected Sub OnPrintPage(ev As PrintPageEventArgs)MyBase.OnPrintPage(ev)Dim lpp As Single = 0Dim yPos As Single = 0Dim count As Integer = 0Dim leftMargin As Single = ev.MarginBounds.LeftDim topMargin As Single = ev.MarginBounds.TopDim line as String'Work out the number of lines per page'Use the MarginBounds on the event to do thislpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)'Check count first so that we don't read line that we won't printline=streamToPrint.ReadLine()While ((count < lpp) And Not(line Is Nothing))yPos = topMargin + (count * printFont.GetHeight(ev.Graphics))ev.Graphics.DrawString (line, printFont, Brushes.Black, leftMargin,yPos, new StringFormat())count = count + 1if (count < lpp) thenline=streamToPrint.ReadLine()end ifEnd WhileIf (line <> Nothing) Thenev.HasMorePages = TrueElseev.HasMorePages = FalseEnd IfEnd SubEnd ClassEnd Namespace'主窗体中的代码Imports SystemImports ponentModelImports System.Windows.FormsImports System.DrawingImports System.Drawing.PrintingImports System.IONamespace Microsoft.Samples.WinForms.VB.PrintingExample5Public Class PrintFormInherits System.Windows.Forms.FormPrivate storedPageSettings As PageSettingsPublic Sub New ()MyBase.NewPrintForm = MeInitializeComponent()AddHandler printButton.Click, AddressOf printButton_ClickAddHandler pageSetupButton.Click, AddressOf pageSetupButton_ClickAddHandler printPreviewButton.Click, AddressOfprintPreviewButton_ClickEnd Sub'页面设置Private Sub pageSetupButton_Click(sender As object, e As System.EventArgs)Dim psDlg As New PageSetupDialogIf (storedPageSettings Is Nothing) ThenstoredPageSettings = new PageSettings()End IfpsDlg.PageSettings = storedPageSettingspsDlg.ShowDialogCatch ex As ExceptionMessageBox.Show("An error occurred - " + ex.Message) End TryEnd Sub'开始打印Private Sub printButton_Click(sender As object, e As System.EventArgs) TryDim streamToPrint As StreamReader = new StreamReader("PrintMe.Txt")Try'使用缺省打印机Dim pd As TextFilePrintDocument = newTextFilePrintDocument(streamToPrint)If Not (storedPageSettings Is Nothing) Thenpd.DefaultPageSettings = storedPageSettingsEnd IfDim dlg As New PrintDialog()dlg.Document = pdDim result As DialogResult = dlg.ShowDialog()If (result = System.Windows.Forms.DialogResult.OK) Thenpd.Print()End IfFinallystreamToPrint.Close()End TryCatch ex As ExceptionMessageBox.Show("An error occurred printing the file - " +ex.Message)End TryEnd Sub'打印预览Private Sub printPreviewButton_Click(sender As object, e AsSystem.EventArgs)TryDim streamToPrint As StreamReader = new StreamReader("PrintMe.Txt")TryDim pd As TextFilePrintDocument = newTextFilePrintDocument(streamToPrint)If Not (storedPageSettings Is Nothing) Thenpd.DefaultPageSettings = storedPageSettingsEnd IfDim dlg As New PrintPreviewDialog()dlg.Document = pddlg.ShowDialog()FinallystreamToPrint.Close()End TryCatch ex As ExceptionMessageBox.Show("An error occurred - " + ex.Message) End TryPublic Overloads Overrides Sub Dispose()MyBase.Dispose()components.Dispose()End Sub<STAThread()> Shared Sub Main()System.Windows.Forms.Application.Run(New PrintForm())End SubEnd ClassEnd Namespace■运行程序单击菜单“调试|启动”或单击图标运行程序。

VB报表打印

VB报表打印

VB在打印中的应用我是个80后,当时接触的编程语音,第一个是basic,第二个是C语音,第三个是VB,当初看VB的时候,很清晰的记得Bill Gates介绍VB的一句话,使用VB,你可以最快的达到你想要的效果,学着学着,一眨眼,就十多年过去啦。

VB & SQL基本上,可以满足基本的要求,当然了,如果,你想要什么换皮肤,很大工作量,多线程之类功能,VB是很难做到的,而且插件是VB最痛的地方。

好了,说到这里,我介绍一下,我使用VB在打印上面的一些经验吧。

VB本身是带了一个设计器功能的,但里面的功能不敢恭维,不方便,功能小。

在网上找了一段时间,测试了很多插件,终于找到了一个叫ACReport的控件,有一个插件和一个注册文件,ACRptEngineX.ocx,注册ACReport 引擎.bat。

这个插件可以利用SQL数据库内容打印操作,或者自定义打印的内容。

下面介绍一下控件的安装:首先双击ACRptEngineX.ocx,进行安装,,注意这两个文件,必须放在和程序开发的文件一齐,安装完ACRptEngineX.ocx,然后双击,注册ACReport 引擎.bat进行注册,见到*.bat结尾的文件,大家都知道,会弹出command窗口,类似DOS环境的界面,看到这个界面,就知道,为什么两个文件都要和开发的程序放在一齐了。

安装成功后,打开程序主体软件,添加部件,选择ACRptEngineX Library ,会出现ACRptEngineX这个插件,好了,在窗体上一划,就出现。

现在介绍一下属性:属性里面没有什么好修改的地方,一般我会将visible设置为flase,隐藏控件。

好了,可以进入里面去慢慢设计啦!在VB里面调用ACRptEngineX,必须写命令,好吧,我来介绍以下命令的语法吧。

Dim UserNameS As String …定义字符串变量UserNamesUserNameS = "fadsfa"‟赋值ACRptEngine1.AddSysVariable "Userns", UserNameS …这个是在打印文件中添加一个字符串变量Userns,内容是变量UserNameS的内容ACRptEngine1.AddSysVariable "开票日期", Format(Date, "yyyy") & "年" & Format(Date, "mm") & "月" & Format(Date, "dd") & "日" …也可以直接写内容ACRptEngine1.Init …初始化sql1 = "select * from 发票打印where 用户编号= '" & userp & "' order by 卡号"Dim mrc As ADODB.Recordset ‟在使用这句语句的时候,必须在工程->引用,添加Microsoft ActiveX Data objects Recordset 2.8 librarySet mrc = ExecuteSQL(sql1)‟这个函数,是我自己写的,后面补上代码,大家可以自己写,就是一般的SQL操作,add, delete,rename之类的。

VB的几种打印方法

VB的几种打印方法

VB的几种打印方法VB的几种打印方法1. 采用Visual Basic提供的简单打印函数PrintForm方法应用程序窗体的PrintForm方法时Visual Basic把当前窗体的位图送到打印机。

该方法的优点在于它几乎不需要任何编程但也有很大缺陷,如当低分辨率图形在高分辨率打印机上打印时,会产生锯齿。

代码如下:Private Sub Command1_Click '用PrintForm打印Me.PrintForm '打印窗体的可见区域End Sub2. 用Printer对象Printer对象代表系统缺省的打印机。

打印机以twips来测量距离,每英寸有1440个 twips。

实例如下:打印直线:Printer.Line2144021440-Step14401440B打印文本:Printer.Print "Hello China ComputerWorld" '打印字符串NewPage告诉打印机程序对当前输出页的发送已经结束Printer对象应开始新的一页。

EndDoc告诉VB程序创建文档结束VB应将它发送到物理打印机上打印。

KillDoc取消当前打印作业。

应用程序应该终止由EndDoc和KillDoc所设定的每个打印作业。

Zoom属性用于定义打印输出的缩放因子。

Copies属性用于定义打印的副本数目。

3. 用RichTextBox控件的SelPrint方法来打印代码如下:RTF1.SelPrint Printer.hDC '打印RTF1控件的可见区域一般结合RichT extBox控件和通用对话框--打印对话框实现,先进行打进设置再进行打印,如下。

Private Sub Command3_Click 'SelPrint方法CommonDialog1.Flags=cdlPDReturnDC+cdlPDNoPageNum s'设置通用对话框-打印对话框属性。

vb 打印实例

vb 打印实例

vb 打印实例
在VB(Visual Basic)中,打印通常涉及到使用Windows的打印组件。

以下是一个简单的示例,演示如何打印文本:
```vb
Imports
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
' 创建一个新的打印作业
Dim printJob As New PrintDocument()
' 添加一个打印事件处理器
AddHandler , AddressOf printJob_PrintPage
' 开始打印
()
End Sub
' 打印事件处理器
Private Sub printJob_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
' 在页面上绘制文本
("Hello, World!", New Font("Arial", 12), , New PointF(100, 100)) End Sub
End Class
```
这个示例中,我们创建了一个新的`PrintDocument`对象,并添加了一个打印事件处理器。

当用户点击按钮时,我们调用`Print`方法开始打印。

在打印事件处理器中,我们使用`Graphics`对象在页面上绘制文本。

请注意,这只是一个简单的示例。

在实际应用中,您可能需要处理更多的打印选项和设置,例如纸张大小、边距、多页打印等。

vb打印输出程序

vb打印输出程序
Case "dwg", "pdf", "doc", "docx"
jj = jj + 1
rs.MoveNext
Case Else
'WordTemps.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
'WordTemps.Selection.Cells.VerticalAlignment = wdCellAlignVerticalCenter
End If
MyTable.Cell((PicIndex \ 2 + 1) * 2 - 1, PicIndex Mod 2 + 1).Range.text = PicItems(I)
MyTable.Cell((PicIndex \ 2 + 1) * 2 - 1, PicIndex Mod 2 + 1).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
If (PicIndex \ 2 + 1) * 2 > MyTable.Rows.Count Then
MyTable.Rows.Add
MyTable.Rows.Add
End If
WordTemps.Selection.GoTo wdGoToBookmark, , , "古桥保护规划"
'WordTemps.Selection.TypeText "" & vbCrLf
WordTemps.Selection.TypeText "1、古桥保护范围及规划说明" & vbCrLf

VBA中的打印设置与批量打印技巧

VBA中的打印设置与批量打印技巧

VBA中的打印设置与批量打印技巧在Excel中,我们常常需要对工作表进行打印,打印设置的灵活性和批量打印的效率对于提高工作效率非常重要。

VBA (Visual Basic for Applications) 是一种可以在Microsoft Office套件中的各个应用程序中使用的编程语言,能够帮助我们自动化处理任务,包括打印设置和批量打印。

本文将介绍VBA中常用的打印设置以及一些实用的批量打印技巧,帮助您提高工作效率。

1. 打印设置VBA中的打印设置功能丰富,可以根据需求自定义打印页面、页眉页脚、打印区域等。

1.1 打印页面设置使用VBA,您可以轻松地对打印页面进行设置。

例如,您可以设置纸张大小、方向、边距等。

```vbaSub 页面设置()With ActiveSheet.PageSetup.PaperSize = xlPaperA4 ' 设置纸张大小为A4.Orientation = xlLandscape ' 设置横向打印.LeftMargin = Application.InchesToPoints(0.5) ' 设置左边距为0.5英寸.RightMargin = Application.InchesToPoints(0.5) ' 设置右边距为0.5英寸.TopMargin = Application.InchesToPoints(0.75) ' 设置上边距为0.75英寸.BottomMargin = Application.InchesToPoints(0.75) ' 设置下边距为0.75英寸 End WithEnd Sub```1.2 设置页眉页脚通过VBA,您可以在打印时添加页眉页脚,为打印内容提供更多信息。

```vbaSub 页眉页脚设置()With ActiveSheet.PageSetup.LeftHeader = "左侧页眉" ' 设置左侧页眉.CenterHeader = "中间页眉" ' 设置中间页眉.RightHeader = "右侧页眉" ' 设置右侧页眉.LeftFooter = "左侧页脚" ' 设置左侧页脚.CenterFooter = "中间页脚" ' 设置中间页脚.RightFooter = "右侧页脚" ' 设置右侧页脚End WithEnd Sub```1.3 打印区域设置有时候,我们不需要将整个工作表都打印出来,而只希望打印某个特定的区域。

VBA中的文件打印和批量打印方法介绍

VBA中的文件打印和批量打印方法介绍

VBA中的文件打印和批量打印方法介绍VBA是一种用于Microsoft Office应用程序的编程语言,它可以帮助用户更有效地处理和管理数据。

在VBA中,文件打印和批量打印是常见的任务,本文将介绍如何使用VBA中的方法来实现这些功能。

一、文件打印方法介绍VBA中的文件打印方法允许用户通过编程方式控制打印操作,可以选择性地打印特定的文档,设置打印选项以及指定打印份数等。

1. 使用PrintOut方法打印当前活动文档在VBA中,可以使用PrintOut方法来打印当前活动的文档。

以下是一个示例代码:```Sub PrintActiveDocument()ActiveDocument.PrintOutEnd Sub```通过运行上述代码,VBA会自动打印当前活动的文档。

2. 使用PrintOut方法打印指定的文档如果想要打印指定的文档,可以使用PrintOut方法,并指定要打印的文档对象。

以下是一个示例代码:```Sub PrintSpecificDocument()Dim doc As ObjectSet doc = Documents.Open("C:\Documents\Sample.docx")doc.PrintOutdoc.Close SaveChanges:=FalseEnd Sub```上述代码中,首先通过Documents.Open方法打开要打印的文档,然后使用doc.PrintOut方法打印该文档。

最后,使用doc.Close方法关闭文档,并选择不保存更改。

3. 设置打印选项VBA中的文件打印方法还支持设置打印选项,如打印份数、打印范围、纸张类型等。

以下是一个示例代码:```Sub SetPrintOptions()ActiveDocument.PrintOut Copies:=2, Range:=wdPrintFromTo, Pages:="1-3", _Item:=wdPrintDocumentWithMarkup, PaperSize:=wdPaperA4 End Sub```上述代码中,通过传递不同的参数值给PrintOut方法,可以设置打印选项。

VB打印功能全代码

VB打印功能全代码

VB打印功能全代码Private Sub Image5_Click() '打印报表Dim i As IntegerDim pageleft As IntegerDim pageheader As IntegerDim pagefooter As IntegerDim pageright As IntegerDim startx As IntegerDim starty As IntegerDim startline As IntegerDim endline As IntegerDim usewidth As IntegerDim useheight As Integer pageheader = 25pagefooter = 25pageleft = 20pageright = 20'定义纸张类型A4Printer.FontSize = 20Printer.PaperSize = 9Printer.ScaleMode = 6Printer.FontBold = FalsePrinter.ScaleLeft = -20Printer.ScaleTop = -25Printer.ScaleWidth = 210Printer.ScaleHeight = 297 usewidth = Printer.ScaleWidth - 40 useheight = Printer.ScaleHeight - 50Printer.CurrentX = 0Printer.CurrentY = 0Printer.DrawWidth = 2'打印主标题Printer.FontSize = 20Printer.CurrentX = (usewidth - Printer.TextWidth(Me.T ext1 .Text & "经济情况表")) / 2Printer.CurrentY = Printer.ScaleT opPrinter.Print Me.Text1.Text & "经济情况表"'打印空白行Printer.FontSize = 8Printer.CurrentX = (usewidth - Printer.TextWidth("")) / 2 Printer.CurrentY = Printer.CurrentY + 1Printer.Print ""'打印副标题Printer.FontSize = 15Printer.CurrentX = (usewidth - Printer.TextWidth(common th & "月份经济情况报表")) / 2Printer.CurrentY = Printer.CurrentY + 1Printer.Print commonth & "月份经济情况报表"'打印空白行Printer.FontSize = 8Printer.CurrentX = (usewidth - Printer.TextWidth("")) / 2 Printer.CurrentY = Printer.CurrentY + 1Printer.Print ""'打印表的内容'首先打印第一条横线'打印最上边的第一条横线Printer.CurrentX = pageleft + Printer.ScaleLeftstartline = Printer.CurrentY + 1Printer.Line -((Printer.ScaleLeft + Printer.ScaleWidth - pageleft), Printer.Cur rentY)Printer.FontSize = 12Printer.CurrentY = Printer.CurrentY + 1starty = Printer.CurrentYPrinter.CurrentX = ((Printer.ScaleWidth - 40) / 3 - Printe r.TextWidth("")) / 2Printer.Print ""Printer.FontSize = 12Printer.CurrentX = usewidth / 3 + ((usewidth / 3 - Prin ter.TextWidth("今年经济情况")) / 2)Printer.CurrentY = startyPrinter.Print "今年经济情况(万元)"Printer.FontSize = 12Printer.CurrentX = (usewidth / 3) * 2 + ((usewidth / 3 - Printer.TextWidth("去年经济情况")) / 2)Printer.CurrentY = startyPrinter.Print "去年经济情况(万元)"Printer.CurrentX = pageleft + Printer.ScaleLeftPrinter.CurrentY = Printer.CurrentY + 1Printer.Line -((Printer.ScaleLeft + Printer.ScaleWidth - pageleft), Printer.Cur rentY)Printer.CurrentY = Printer.CurrentY + 1For i = 0 To 10'判断是否该页已打满Me.MSFlexGrid1.Row = iIf Printer.CurrentY >= useheight Then'打印横线Printer.CurrentX = Printer.ScaleLeft + pageleftPrinter.Line -((Printer.ScaleLeft + Printer.ScaleWidth - pageleft), Printer.Cur rentY)Printer.CurrentY = Printer.CurrentY + 1'打印四条竖线endline = Printer.CurrentYPrinter.Line (0, startline)-(0, endline)Printer.Line (usewidth / 3, startline)-(usewidth / 3, endline)Printer.Line ((usewidth / 3) * 2, startline)-((usewidth / 3) * 2, endline)Printer.Line (usewidth, startline)-(usewidth, endline)'打印页号Printer.CurrentX = (Printer.ScaleWidth - Printer.TextWidth( Printer.Page)) / 3 - pageleftPrinter.CurrentY = useheight + 3Printer.Print Printer.PagePrinter.NewPage 'NewPage告诉打印机,程序对当前输出页的发送已经结束。

用VB实现多文档打印VB-电脑资料

用VB实现多文档打印VB-电脑资料

用VB实现多文档打印VB-电脑资料用 VB 实现多文档打印作者:盛放通常,一般情况下,我们打印文档都是直接从应用程序中打印,例如WORD,这个方法对于单个文件打印比较方便,但是对于文秘等经常需要打印大批量文件的工作人员,如果仍然用WORD一个一个进行打印就比较麻烦了,。

应我公司文管用VB实现多文档打印作者:盛放通常,一般情况下,我们打印文档都是直接从应用程序中打印,例如WORD,这个方法对于单个文件打印比较方便,但是对于文秘等经常需要打印大批量文件的工作人员,如果仍然用WORD一个一个进行打印就比较麻烦了。

应我公司文管中心的要求,我制作了一个多文档打印程序,方便了文秘人员,现介绍如下:运行VB,新建一个应用程序工程,在Form中添加一个DriveListBox 控件、DirListBox 控件和FileListBox 控件,分别取名为:Drive1、Dir1和File1。

设置File1的Pattern 属性为*.doc,设置FileListBox 控件中显示的文件名为DOC文档。

在Form_Load()事件中添加一行代码:Drive1.Drive = "c:",用来设置运行时所选择的驱动器。

在Drive1_Change()事件中添加一行代码:Dir1.Path = Drive1.Drive,这样当驱动器改变时,使目录路径随之改变。

同样在Dir1_Change()事件中添加一行代码:File1.Path = Dir1.Path,当目录改变时,使文件列表同时改变。

因为一般来说,文档都会保存在统一的目录下,所以没有使用CommonDialog 控件进行文件名选择,使操作更为简单方便。

在Form中添加一个CommandButton 控件取名为Print,用来进行打印操作。

在Command1_Click()中添加如下代码:Dim i As IntegerDim strfile As StringDim word As ObjectSet word = CreateObject("word.Basic")word.appshowFor i = 0 To File1.ListCount - 1If Right(Dir1.Path, 1) <> "" Thenstrfile = Dir1.Path + "" + File1.List(i)Elsestrfile = Dir1.Path + File1.List(i)End Ifword.fileopen strfileword.fileprintword.filecloseNextword.appcloseSet word = Nothing代码说明:定义一个对象变量word使用CreateObject创建一个WORD对象并赋值给word以FileListBox 控件的列表部分项目的个数作为最大数进行循环打印判断目录名的最后一个字母是否为“”,如果不是,则添加“”进行修正通过目录名和文件名获得完整的文件名使用word对象的fileopen方法打开文件使用word对象的fileprint方法进行文件打印使用word对象的fileclose方法关闭文件最后调用word对象的appclose方法关闭word程序,以及释放对象资源Set word = Nothing,电脑资料《用VB实现多文档打印VB》(https://www.)。

VBNET如何实现打印

VBNET如何实现打印

VBNET如何实现打印兄弟,下面的代码你可以参考,是......兄弟,下面的代码你可以参考,是鄙人的一个项目摘录,希望能给你提供帮助。

Imports ExcelPublic Class PinholeReportPrivate pnhExcel As Excel.Application ' 针孔报表 EXCEL 对象Private pnhWorkBook As Excel.WorkbookPrivate pnhWorkSheet As Excel.WorksheetPublic PinholeReortExcelFileName As String = Environment.CurrentDirectory & "\针孔报表.XLS"Public ExcelVisible As Boolean = FalsePublic Sub New()pnhExcel = New Excel.Application()pnhExcel.WindowState = Excel.XlWindowState.xlMinimized pnhExcel.Visible = ExcelVisiblepnhWorkBook = pnhExcel.Workbooks.Open(PinholeReortExcelFileName) pnhWorkSheet = pnhWorkBook.Sheets("针孔报表")End SubProtected Overrides Sub Finalize()If Not pnhExcel Is Nothing ThenpnhWorkBook.Close(False)pnhExcel.Quit()pnhWorkSheet = Nothing : pnhWorkBook = Nothing : pnhExcel = NothingEnd IfMyBase.Finalize()End Sub' 针孔报表打印Public Function PinholeReport_Print(ByVal thePinhole As clsGroupPinholeData) As StringDim strError As String = Nothing ' 错误串'====================================== ======================================== TryDim i, j As Integer' 刷新针孔报表pnhWorkSheet.Cells(1, 4) = thePinhole.CoilIDpnhWorkSheet.Cells(2, 4) = thePinhole.WeightpnhWorkSheet.Cells(3, 4) = thePinhole.ThickpnhWorkSheet.Cells(4, 4) = thePinhole.WidthpnhWorkSheet.Cells(5, 4) = thePinhole.DesignedTopCoatWeightpnhWorkSheet.Cells(6, 4) = thePinhole.DesignedBotCoatWeightpnhWorkSheet.Cells(7, 4) = thePinhole.StartTimepnhWorkSheet.Cells(8, 4) = thePinhole.EndTimeFor i = 0 To 49pnhWorkSheet.Cells((i \ 5) + 12, ((i Mod 5) * 2 + 1) + 1) = thePinhole.StartPos(i + 1)pnhWorkSheet.Cells((i \ 5) + 12, ((i Mod 5) * 2 + 2) + 1) = thePinhole.EndPos(i + 1)Next' 报表打印pnhWorkSheet.PrintOut()'pnhWorkSheet.PrintPreview()Catch ex As ExceptionstrError = "针孔报表打印错误!" & vbCrLf & ex.Message & vbCrLf & ex.StackTrace End TryReturn strErrorEnd FunctionEnd Class上面,鄙人使用了一个内部类clsGroupPinholeData以提供数据,当然你必须修改以使用你的Datatable数据。

看到一个VB打印模块,保存一下

看到一个VB打印模块,保存一下

看到一个VB打印模块,保存一下''''模块modPrintOption ExplicitPublic Declare Function EnumForms Lib "winspool.drv" Alias "EnumFormsA" _(ByVal hPrinter As Long, ByVal Level As Long, ByRef pForm As Any, _ByVal cbBuf As Long, ByRef pcbNeeded As Long, _ByRef pcReturned As Long) As LongPublic Declare Function AddForm Lib "winspool.drv" Alias "AddFormA" _(ByVal hPrinter As Long, ByVal Level As Long, pForm As Byte) As LongPublic Declare Function DeleteForm Lib "winspool.drv" Alias "DeleteFormA" _(ByVal hPrinter As Long, ByVal pFormName As String) As LongPublic Declare Function OpenPrinter Lib "winspool.drv" _ Alias "OpenPrinterA" (ByVal pPrinterName As String, _phPrinter As Long, ByVal pDefault As Long) As LongPublic Declare Function ClosePrinter Lib "winspool.drv" _ (ByVal hPrinter As Long) As LongPublic Declare Function DocumentProperties Lib "winspool.drv" _Alias "DocumentPropertiesA" (ByVal hwnd As Long, _ ByVal hPrinter As Long, ByVal pDeviceName As String, _pDevModeOutput As Any, pDevModeInput As Any, ByVal fMode As Long) _As LongPublic Declare Function ResetDC Lib "gdi32" Alias "ResetDCA" _(ByVal hdc As Long, lpInitData As Any) As Long Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _(hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)Public Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" _(ByVal lpString1 As String, ByRef lpString2 As Long) As Long'''' Optional functions not used in this sample, but may beuseful.Public Declare Function GetForm Lib "winspool.drv" Alias "GetFormA" _(ByVal hPrinter As Long, ByVal pFormName As String, _ByVal Level As Long, pForm As Byte, ByVal cbBuf As Long, _pcbNeeded As Long) As LongPublic Declare Function SetForm Lib "winspool.drv" Alias "SetFormA" _(ByVal hPrinter As Long, ByVal pFormName As String, _ByVal Level As Long, pForm As Byte) As Long'''' Constants for DEVMODEPublic Const CCHFORMNAME = 32Public Const CCHDEVICENAME = 32Public Const DM_FORMNAME As Long = &amp;H10000 Public Const DM_ORIENTATION = &amp;H1&amp;'''' Constants for PRINTER_DEFAULTS.DesiredAccess Public Const PRINTER_ACCESS_ADMINISTER =&amp;H4Public Const PRINTER_ACCESS_USE = &amp;H8Public Const STANDARD_RIGHTS_REQUIRED = &amp;HF0000Public Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _ PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)'''' Constants for DocumentProperties() callPublic Const DM_MODIFY = 8Public Const DM_IN_BUFFER = DM_MODIFY Public Const DM_COPY = 2Public Const DM_OUT_BUFFER = DM_COPY'''' Custom constants for this sample''''s SelectForm functionPublic Const FORM_NOT_SELECTED = 0Public Const FORM_SELECTED = 1Public Const FORM_ADDED = 2Public Type RECTLLeft As LongTop As LongRight As LongBottom As LongEnd TypePublic Type SIZELcx As Longcy As LongEnd TypePublic Type SECURITY_DESCRIPTORRevision As ByteSbz1 As ByteControl As LongOwner As LongGroup As LongSacl As Long '''' ACLDacl As Long '''' ACLEnd Type'''' The two definitions for FORM_INFO_1 make the coding easier.Public Type FORM_INFO_1Flags As LongpName As Long '''' StringSize As SIZELImageableArea As RECTLEnd TypePublic Type sFORM_INFO_1Flags As LongpName As StringSize As SIZELImageableArea As RECTLEnd TypePublic Type DEVMODEdmDeviceName As String * CCHDEVICENAMEdmSpecVersion As IntegerdmDriverVersion As IntegerdmSize As IntegerdmDriverExtra As IntegerdmFields As LongdmOrientation As IntegerdmPaperSize As IntegerdmPaperLength As IntegerdmPaperWidth As IntegerdmScale As IntegerdmCopies As IntegerdmDefaultSource As IntegerdmPrintQuality As IntegerdmColor As IntegerdmDuplex As IntegerdmYResolution As IntegerdmTTOption As IntegerdmCollate As IntegerdmFormName As String * CCHFORMNAMEdmUnusedPadding As IntegerdmBitsPerPel As LongdmPelsWidth As LongdmPelsHeight As LongdmDisplayFlags As LongdmDisplayFrequency As LongEnd TypePublic Type PRINTER_DEFAULTSpDatatype As StringpDevMode As Long '''' DEVMODEDesiredAccess As LongEnd TypePublic Type PRINTER_INFO_2pServerName As StringpPrinterName As StringpShareName As StringpPortName As StringpDriverName As StringpComment As StringpLocation As StringpDevMode As DEVMODEpSepFile As StringpPrintProcessor As StringpDatatype As StringpParameters As StringpSecurityDescriptor AsSECURITY_DESCRIPTORAttributes As LongPriority As LongDefaultPriority As LongStartTime As LongUntilTime As LongStatus As LongcJobs As LongAveragePPM As LongEnd Type''''判断系统是否为NT系统Public Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" _(lpVersionInformation AsOSVERSIONINFO) As LongPublic Type OSVERSIONINFOdwOSVersionInfoSize As LongdwMajorVersion As LongdwMinorVersion As LongdwBuildNumber As LongdwPlatformId As LongszCSDVersion As String * 128 '''' Maintenance string for PSS usageEnd TypePublic Function SelectForm(FormName As String, ByVal MyhWnd As Long) _As IntegerDim nSize As Long '''' Size of DEVMODE Dim pDevMode As DEVMODEDim PrinterHandle As Long '''' Handle to printerDim hPrtDC As Long '''' Handle to Printer DC Dim PrinterName As StringDim aDevMode() As Byte '''' Working DEVMODE Dim FormSize As SIZELDim tmpFormName As StringDim isNT As BooleanDim PaperWidth As Long, PaperHeight As Long''''在这里统一添加自定义纸张Select Case FormNameCase "广东省发票"With FormSize '''' Desired page size.cx = 186000.cy = 102000End WithPaperWidth = 10544 ''''15.6cmPaperHeight = 5784 ''''10.2cmCase "80列报表"With FormSize '''' Desired page size.cx = 210000.cy = 280000End WithPaperWidth = 11904 ''''21cmPaperHeight = 15872 ''''28cmCase "40列报表"With FormSize '''' Desired page size.cx = 210000.cy = 140000End WithPaperWidth = 11904 ''''21cmPaperHeight = 7936 ''''14cmCase "20列报表"With FormSize '''' Desired page size.cx = 210000.cy = 70000End WithPaperWidth = 11904 ''''21cmPaperHeight = 4008 ''''7cmEnd Select''''不是NT直接给Printer赋值得了If Not IsNtOs ThenPrinter.Width = PaperWidthPrinter.Height = PaperHeight SelectForm = FORM_SELECTEDExit FunctionEnd IfPrinterName = Printer.DeviceName '''' Current printer hPrtDC = Printer.hdc '''' hDC for current PrinterSelectForm = FORM_NOT_SELECTED '''' Set for failure unless reset in code.'''' Get a handle to the printer.If OpenPrinter(PrinterName, PrinterHandle, 0&amp;) Then'''' Retrieve the size of the DEVMODE.nSize = DocumentProperties(MyhWnd, PrinterHandle, PrinterName, 0&amp;, _0&amp;, 0&amp;)'''' Reserve memory for the actual size of the DEVMODE.ReDim aDevMode(1 To nSize)'''' Fill the DEVMODE from the printer.nSize = DocumentProperties(MyhWnd, PrinterHandle, PrinterName, _aDevMode(1), 0&amp;,DM_OUT_BUFFER)'''' Copy the Public (predefined) portion of the DEVMODE.Call CopyMemory(pDevMode, aDevMode(1),Len(pDevMode))'''' If FormName is "MyCustomForm", we must make sure it exists'''' before using it. Otherwise, it came from our EnumForms list,'''' and we do not need to check first. Note that we could have'''' passed in a Flag instead of checking for a literal name.tmpFormName = FormNameIf GetFormName(PrinterHandle, FormSize, tmpFormName) = 0 Then'''' Form not found - Either of the next 2 lines will work.''''FormName = AddNewForm(PrinterHandle, FormSize, "MyCustomForm")AddNewForm PrinterHandle, FormSize, FormNameIf GetFormName(PrinterHandle, FormSize, FormName) = 0 ThenClosePrinter (PrinterHandle)SelectForm = FORM_NOT_SELECTED'''' Selection Failed!Exit FunctionElseSelectForm = FORM_ADDED '''' Form Added, Selection succeeded!End IfEnd If'''' Change the appropriate member in the DevMode.'''' In this case, you want to change the form name.pDevMode.dmFormName = FormName &amp;Chr(0) '''' Must be NULL terminated!'''' Set the dmFields bit flag to indicate what you are changing.pDevMode.dmFields = DM_FORMNAME'''' Copy your changes back, then update DEVMODE.Call CopyMemory(aDevMode(1), pDevMode, Len(pDevMode))nSize = DocumentProperties(MyhWnd, PrinterHandle, PrinterName, _aDevMode(1), aDevMode(1),DM_IN_BUFFER Or DM_OUT_BUFFER)nSize = ResetDC(hPrtDC, aDevMode(1)) '''' Reset the DEVMODE for the DC.'''' Close the handle when you are finished with it.ClosePrinter (PrinterHandle)'''' Selection Succeeded! But was Form Added?If SelectForm &lt;&gt; FORM_ADDED Then SelectForm = FORM_SELECTEDElseSelectForm = FORM_NOT_SELECTED '''' Selection Failed!End IfEnd FunctionPublic Function GetFormName(ByVal PrinterHandle As Long, _FormSize As SIZEL, FormName As String) As IntegerDim NumForms As Long, I As LongDim FI1 As FORM_INFO_1Dim aFI1() As FORM_INFO_1 '''' WorkingFI1 arrayDim Temp() As Byte '''' Temp FI1 arrayDim FormIndex As IntegerDim BytesNeeded As LongDim RetVal As LongFormName = vbNullStringFormIndex = 0ReDim aFI1(1)'''' First call retrieves the BytesNeeded.RetVal = EnumForms(PrinterHandle, 1, aFI1(0), 0&amp;, BytesNeeded, NumForms)ReDim Temp(BytesNeeded)ReDim aFI1(BytesNeeded / Len(FI1))'''' Second call actually enumerates the supported forms. RetVal = EnumForms(PrinterHandle, 1, Temp(0), BytesNeeded, BytesNeeded, _NumForms)Call CopyMemory(aFI1(0), Temp(0), BytesNeeded)For I = 0 To NumForms - 1With aFI1(I)If .Size.cx = FormSize.cx And .Size.cy = FormSize.cy Then'''' Found the desired formFormName = PtrCtoVbString(.pName)FormIndex = I + 1Exit ForEnd IfEnd WithNext IGetFormName = FormIndex '''' Returns non-zero when form is found.End FunctionPublic Function AddNewForm(PrinterHandle As Long, FormSize As SIZEL, _FormName As String) AsStringDim FI1 As sFORM_INFO_1Dim aFI1() As ByteDim RetVal As LongWith FI1.Flags = 0.pName = FormNameWith .Size.cx = FormSize.cx.cy = FormSize.cyEnd WithWith .ImageableArea.Left = 0.Top = 0.Right = FI1.Size.cx.Bottom = FI1.Size.cyEnd WithEnd WithReDim aFI1(Len(FI1))Call CopyMemory(aFI1(0), FI1, Len(FI1)) RetVal = AddForm(PrinterHandle, 1, aFI1(0))If RetVal = 0 ThenIf stDllError = 5 ThenMsgBox "You do not have permissions to add a form to " &amp; _Printer.DeviceName, vbExclamation, "Access Denied!"ElseMsgBox "Error: " &amp; stDllError, , "Error Adding Form"''''MsgBox "Error: " &amp; stDllError, "Error Adding Form"End IfAddNewForm = "none"ElseAddNewForm = FI1.pNameEnd IfEnd FunctionPublic Sub PrintTest()'''' Print two test pages to confirm the page size. Printer.Print "Top of Page 1."Printer.NewPage'''' Spacing between lines should reflect the chosen pageheight.Printer.Print "Top of Page 2. - Check the page Height (Length.)"Printer.EndDocMsgBox "Check Printer " &amp; Printer.DeviceName, vbInformation, "Done!"End SubPublic Function PtrCtoVbString(ByVal Add As Long) As StringDim sTemp As String * 512, X As LongX = lstrcpy(sTemp, ByVal Add)If (InStr(1, sTemp, Chr(0)) = 0) ThenPtrCtoVbString = ""ElsePtrCtoVbString = Left(sTemp, InStr(1, sTemp, Chr(0)) - 1)End IfEnd FunctionPublic Function IsNtOs() As BooleanDim ver As OSVERSIONINFOver.dwOSVersionInfoSize = 148GetVersionEx verWith verSelect Case .dwPlatformIdCase 0, 1IsNtOs = FalseCase 2IsNtOs = TrueEnd SelectEnd WithEnd FunctionPublic Function UseForm(FormName As String, sHwnd As Long) As IntegerDim RetVal As IntegerDim FormSize As SIZEL '''' Size of desired formRetVal = SelectForm(FormName, sHwnd) ''''选择纸张类型UseForm = RetValSelect Case RetValCase FORM_NOT_SELECTED '''' 0'''' 选择纸张失败!''''MsgBox "Unable to retrieve From name", vbExclamation, _''''"Operation halted!"Case FORM_SELECTED '''' 1'''' 选择成功!''''PrintTest '''' Comment this line to avoid printingCase FORM_ADDED '''' 2'''' 添加并选择成功.''''PrintTest '''' Comment this line to avoid printingEnd SelectEnd Function''''调用,在Form1上放置Command1、Command2,复制下列代码:Option ExplicitPrivate Sub Command1_Click()Dim FormName As String, RetValue As Integer''''自定义打印纸张FormName = "广东省发票"RetValue = UseForm(FormName, Me.hwnd)''''If RetValue = 0 Then GoTo errPrintPrintTestEnd SubPrivate Sub Command2_Click()Dim FormName As String, RetValue As Integer''''自定义打印纸张FormName = "80列报表"RetValue = UseForm(FormName, Me.hwnd)''''If RetValue = 0 Then GoTo errPrint PrintTestEnd SubPrivate Sub Form_Load()Command1.Caption = "打印发票"Command2.Caption = "打印报表"End Sub。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

看到一个VB打印模块,保存一下''''模块modPrintOption ExplicitPublic Declare Function EnumForms Lib "winspool.drv" Alias "EnumFormsA" _(ByVal hPrinter As Long, ByVal Level As Long, ByRef pForm As Any, _ByVal cbBuf As Long, ByRef pcbNeeded As Long, _ByRef pcReturned As Long) As LongPublic Declare Function AddForm Lib "winspool.drv" Alias "AddFormA" _(ByVal hPrinter As Long, ByVal Level As Long, pForm As Byte) As LongPublic Declare Function DeleteForm Lib "winspool.drv" Alias "DeleteFormA" _(ByVal hPrinter As Long, ByVal pFormName As String) As LongPublic Declare Function OpenPrinter Lib "winspool.drv" _ Alias "OpenPrinterA" (ByVal pPrinterName As String, _phPrinter As Long, ByVal pDefault As Long) As LongPublic Declare Function ClosePrinter Lib "winspool.drv" _ (ByVal hPrinter As Long) As LongPublic Declare Function DocumentProperties Lib "winspool.drv" _Alias "DocumentPropertiesA" (ByVal hwnd As Long, _ ByVal hPrinter As Long, ByVal pDeviceName As String, _pDevModeOutput As Any, pDevModeInput As Any, ByVal fMode As Long) _As LongPublic Declare Function ResetDC Lib "gdi32" Alias "ResetDCA" _(ByVal hdc As Long, lpInitData As Any) As Long Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _(hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)Public Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" _(ByVal lpString1 As String, ByRef lpString2 As Long) As Long'''' Optional functions not used in this sample, but may beuseful.Public Declare Function GetForm Lib "winspool.drv" Alias "GetFormA" _(ByVal hPrinter As Long, ByVal pFormName As String, _ByVal Level As Long, pForm As Byte, ByVal cbBuf As Long, _pcbNeeded As Long) As LongPublic Declare Function SetForm Lib "winspool.drv" Alias "SetFormA" _(ByVal hPrinter As Long, ByVal pFormName As String, _ByVal Level As Long, pForm As Byte) As Long'''' Constants for DEVMODEPublic Const CCHFORMNAME = 32Public Const CCHDEVICENAME = 32Public Const DM_FORMNAME As Long = &amp;H10000 Public Const DM_ORIENTATION = &amp;H1&amp;'''' Constants for PRINTER_DEFAULTS.DesiredAccess Public Const PRINTER_ACCESS_ADMINISTER =&amp;H4Public Const PRINTER_ACCESS_USE = &amp;H8Public Const STANDARD_RIGHTS_REQUIRED = &amp;HF0000Public Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _ PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)'''' Constants for DocumentProperties() callPublic Const DM_MODIFY = 8Public Const DM_IN_BUFFER = DM_MODIFY Public Const DM_COPY = 2Public Const DM_OUT_BUFFER = DM_COPY'''' Custom constants for this sample''''s SelectForm functionPublic Const FORM_NOT_SELECTED = 0Public Const FORM_SELECTED = 1Public Const FORM_ADDED = 2Public Type RECTLLeft As LongTop As LongRight As LongBottom As LongEnd TypePublic Type SIZELcx As Longcy As LongEnd TypePublic Type SECURITY_DESCRIPTORRevision As ByteSbz1 As ByteControl As LongOwner As LongGroup As LongSacl As Long '''' ACLDacl As Long '''' ACLEnd Type'''' The two definitions for FORM_INFO_1 make the coding easier.Public Type FORM_INFO_1Flags As LongpName As Long '''' StringSize As SIZELImageableArea As RECTLEnd TypePublic Type sFORM_INFO_1Flags As LongpName As StringSize As SIZELImageableArea As RECTLEnd TypePublic Type DEVMODEdmDeviceName As String * CCHDEVICENAMEdmSpecVersion As IntegerdmDriverVersion As IntegerdmSize As IntegerdmDriverExtra As IntegerdmFields As LongdmOrientation As IntegerdmPaperSize As IntegerdmPaperLength As IntegerdmPaperWidth As IntegerdmScale As IntegerdmCopies As IntegerdmDefaultSource As IntegerdmPrintQuality As IntegerdmColor As IntegerdmDuplex As IntegerdmYResolution As IntegerdmTTOption As IntegerdmCollate As IntegerdmFormName As String * CCHFORMNAMEdmUnusedPadding As IntegerdmBitsPerPel As LongdmPelsWidth As LongdmPelsHeight As LongdmDisplayFlags As LongdmDisplayFrequency As LongEnd TypePublic Type PRINTER_DEFAULTSpDatatype As StringpDevMode As Long '''' DEVMODEDesiredAccess As LongEnd TypePublic Type PRINTER_INFO_2pServerName As StringpPrinterName As StringpShareName As StringpPortName As StringpDriverName As StringpComment As StringpLocation As StringpDevMode As DEVMODEpSepFile As StringpPrintProcessor As StringpDatatype As StringpParameters As StringpSecurityDescriptor AsSECURITY_DESCRIPTORAttributes As LongPriority As LongDefaultPriority As LongStartTime As LongUntilTime As LongStatus As LongcJobs As LongAveragePPM As LongEnd Type''''判断系统是否为NT系统Public Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" _(lpVersionInformation AsOSVERSIONINFO) As LongPublic Type OSVERSIONINFOdwOSVersionInfoSize As LongdwMajorVersion As LongdwMinorVersion As LongdwBuildNumber As LongdwPlatformId As LongszCSDVersion As String * 128 '''' Maintenance string for PSS usageEnd TypePublic Function SelectForm(FormName As String, ByVal MyhWnd As Long) _As IntegerDim nSize As Long '''' Size of DEVMODE Dim pDevMode As DEVMODEDim PrinterHandle As Long '''' Handle to printerDim hPrtDC As Long '''' Handle to Printer DC Dim PrinterName As StringDim aDevMode() As Byte '''' Working DEVMODE Dim FormSize As SIZELDim tmpFormName As StringDim isNT As BooleanDim PaperWidth As Long, PaperHeight As Long''''在这里统一添加自定义纸张Select Case FormNameCase "广东省发票"With FormSize '''' Desired page size.cx = 186000.cy = 102000End WithPaperWidth = 10544 ''''15.6cmPaperHeight = 5784 ''''10.2cmCase "80列报表"With FormSize '''' Desired page size.cx = 210000.cy = 280000End WithPaperWidth = 11904 ''''21cmPaperHeight = 15872 ''''28cmCase "40列报表"With FormSize '''' Desired page size.cx = 210000.cy = 140000End WithPaperWidth = 11904 ''''21cmPaperHeight = 7936 ''''14cmCase "20列报表"With FormSize '''' Desired page size.cx = 210000.cy = 70000End WithPaperWidth = 11904 ''''21cmPaperHeight = 4008 ''''7cmEnd Select''''不是NT直接给Printer赋值得了If Not IsNtOs ThenPrinter.Width = PaperWidthPrinter.Height = PaperHeight SelectForm = FORM_SELECTEDExit FunctionEnd IfPrinterName = Printer.DeviceName '''' Current printer hPrtDC = Printer.hdc '''' hDC for current PrinterSelectForm = FORM_NOT_SELECTED '''' Set for failure unless reset in code.'''' Get a handle to the printer.If OpenPrinter(PrinterName, PrinterHandle, 0&amp;) Then'''' Retrieve the size of the DEVMODE.nSize = DocumentProperties(MyhWnd, PrinterHandle, PrinterName, 0&amp;, _0&amp;, 0&amp;)'''' Reserve memory for the actual size of the DEVMODE.ReDim aDevMode(1 To nSize)'''' Fill the DEVMODE from the printer.nSize = DocumentProperties(MyhWnd, PrinterHandle, PrinterName, _aDevMode(1), 0&amp;,DM_OUT_BUFFER)'''' Copy the Public (predefined) portion of the DEVMODE.Call CopyMemory(pDevMode, aDevMode(1),Len(pDevMode))'''' If FormName is "MyCustomForm", we must make sure it exists'''' before using it. Otherwise, it came from our EnumForms list,'''' and we do not need to check first. Note that we could have'''' passed in a Flag instead of checking for a literal name.tmpFormName = FormNameIf GetFormName(PrinterHandle, FormSize, tmpFormName) = 0 Then'''' Form not found - Either of the next 2 lines will work.''''FormName = AddNewForm(PrinterHandle, FormSize, "MyCustomForm")AddNewForm PrinterHandle, FormSize, FormNameIf GetFormName(PrinterHandle, FormSize, FormName) = 0 ThenClosePrinter (PrinterHandle)SelectForm = FORM_NOT_SELECTED'''' Selection Failed!Exit FunctionElseSelectForm = FORM_ADDED '''' Form Added, Selection succeeded!End IfEnd If'''' Change the appropriate member in the DevMode.'''' In this case, you want to change the form name.pDevMode.dmFormName = FormName &amp;Chr(0) '''' Must be NULL terminated!'''' Set the dmFields bit flag to indicate what you are changing.pDevMode.dmFields = DM_FORMNAME'''' Copy your changes back, then update DEVMODE.Call CopyMemory(aDevMode(1), pDevMode, Len(pDevMode))nSize = DocumentProperties(MyhWnd, PrinterHandle, PrinterName, _aDevMode(1), aDevMode(1),DM_IN_BUFFER Or DM_OUT_BUFFER)nSize = ResetDC(hPrtDC, aDevMode(1)) '''' Reset the DEVMODE for the DC.'''' Close the handle when you are finished with it.ClosePrinter (PrinterHandle)'''' Selection Succeeded! But was Form Added?If SelectForm &lt;&gt; FORM_ADDED Then SelectForm = FORM_SELECTEDElseSelectForm = FORM_NOT_SELECTED '''' Selection Failed!End IfEnd FunctionPublic Function GetFormName(ByVal PrinterHandle As Long, _FormSize As SIZEL, FormName As String) As IntegerDim NumForms As Long, I As LongDim FI1 As FORM_INFO_1Dim aFI1() As FORM_INFO_1 '''' WorkingFI1 arrayDim Temp() As Byte '''' Temp FI1 arrayDim FormIndex As IntegerDim BytesNeeded As LongDim RetVal As LongFormName = vbNullStringFormIndex = 0ReDim aFI1(1)'''' First call retrieves the BytesNeeded.RetVal = EnumForms(PrinterHandle, 1, aFI1(0), 0&amp;, BytesNeeded, NumForms)ReDim Temp(BytesNeeded)ReDim aFI1(BytesNeeded / Len(FI1))'''' Second call actually enumerates the supported forms. RetVal = EnumForms(PrinterHandle, 1, Temp(0), BytesNeeded, BytesNeeded, _NumForms)Call CopyMemory(aFI1(0), Temp(0), BytesNeeded)For I = 0 To NumForms - 1With aFI1(I)If .Size.cx = FormSize.cx And .Size.cy = FormSize.cy Then'''' Found the desired formFormName = PtrCtoVbString(.pName)FormIndex = I + 1Exit ForEnd IfEnd WithNext IGetFormName = FormIndex '''' Returns non-zero when form is found.End FunctionPublic Function AddNewForm(PrinterHandle As Long, FormSize As SIZEL, _FormName As String) AsStringDim FI1 As sFORM_INFO_1Dim aFI1() As ByteDim RetVal As LongWith FI1.Flags = 0.pName = FormNameWith .Size.cx = FormSize.cx.cy = FormSize.cyEnd WithWith .ImageableArea.Left = 0.Top = 0.Right = FI1.Size.cx.Bottom = FI1.Size.cyEnd WithEnd WithReDim aFI1(Len(FI1))Call CopyMemory(aFI1(0), FI1, Len(FI1)) RetVal = AddForm(PrinterHandle, 1, aFI1(0))If RetVal = 0 ThenIf stDllError = 5 ThenMsgBox "You do not have permissions to add a form to " &amp; _Printer.DeviceName, vbExclamation, "Access Denied!"ElseMsgBox "Error: " &amp; stDllError, , "Error Adding Form"''''MsgBox "Error: " &amp; stDllError, "Error Adding Form"End IfAddNewForm = "none"ElseAddNewForm = FI1.pNameEnd IfEnd FunctionPublic Sub PrintTest()'''' Print two test pages to confirm the page size. Printer.Print "Top of Page 1."Printer.NewPage'''' Spacing between lines should reflect the chosen pageheight.Printer.Print "Top of Page 2. - Check the page Height (Length.)"Printer.EndDocMsgBox "Check Printer " &amp; Printer.DeviceName, vbInformation, "Done!"End SubPublic Function PtrCtoVbString(ByVal Add As Long) As StringDim sTemp As String * 512, X As LongX = lstrcpy(sTemp, ByVal Add)If (InStr(1, sTemp, Chr(0)) = 0) ThenPtrCtoVbString = ""ElsePtrCtoVbString = Left(sTemp, InStr(1, sTemp, Chr(0)) - 1)End IfEnd FunctionPublic Function IsNtOs() As BooleanDim ver As OSVERSIONINFOver.dwOSVersionInfoSize = 148GetVersionEx verWith verSelect Case .dwPlatformIdCase 0, 1IsNtOs = FalseCase 2IsNtOs = TrueEnd SelectEnd WithEnd FunctionPublic Function UseForm(FormName As String, sHwnd As Long) As IntegerDim RetVal As IntegerDim FormSize As SIZEL '''' Size of desired formRetVal = SelectForm(FormName, sHwnd) ''''选择纸张类型UseForm = RetValSelect Case RetValCase FORM_NOT_SELECTED '''' 0'''' 选择纸张失败!''''MsgBox "Unable to retrieve From name", vbExclamation, _''''"Operation halted!"Case FORM_SELECTED '''' 1'''' 选择成功!''''PrintTest '''' Comment this line to avoid printingCase FORM_ADDED '''' 2'''' 添加并选择成功.''''PrintTest '''' Comment this line to avoid printingEnd SelectEnd Function''''调用,在Form1上放置Command1、Command2,复制下列代码:Option ExplicitPrivate Sub Command1_Click()Dim FormName As String, RetValue As Integer''''自定义打印纸张FormName = "广东省发票"RetValue = UseForm(FormName, Me.hwnd)''''If RetValue = 0 Then GoTo errPrintPrintTestEnd SubPrivate Sub Command2_Click()Dim FormName As String, RetValue As Integer''''自定义打印纸张FormName = "80列报表"RetValue = UseForm(FormName, Me.hwnd)''''If RetValue = 0 Then GoTo errPrint PrintTestEnd SubPrivate Sub Form_Load()Command1.Caption = "打印发票"Command2.Caption = "打印报表"End Sub。

相关文档
最新文档