你曾今碰到“用VB实现一个打印WORD文档功能。。。”这个问题,并且已经解决,现在我也遇到同样的问题。

2024-11-25 03:03:04
推荐回答(1个)
回答1:

VB.NET(2005)中打印WORD文档(.doc)

有两种方法可以完成,无论哪一种,你都得安装office,呵呵。

Imports Microsoft.Office.Core
Imports Word = Microsoft.Office.Interop.Word

第一种,允许用户更换打印机等设置,这是很重要的。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PrintDialog1.AllowCurrentPage = False ‘不打印当前页
PrintDialog1.AllowSelection = False ’不允许打印部分页等
PrintDialog1.AllowSomePages = False
If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim poApp As Word.Application
Dim poDoc As Word.Document
poApp = New Word.Application
poApp.Visible = False
poApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone
poDoc = poApp.Documents.Open("d:\1.doc")

poApp.WordBasic.FilePrintSetup(Printer:=PrintDialog1.PrinterSettings.PrinterName,
DoNotSetAsSysDefault:=1) ‘DoNotSetAsSysDefault:=0就会修改默认打印机

''poApp.ActivePrinter = PrintDialog1.PrinterSettings.PrinterName

poApp.PrintOut()

poDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
'clean up
poDoc = Nothing

'close
word这里有个极其重要的参数WdSaveOptions,它有三个值:wdDoNotSaveChanges、wdDoNotSaveChanges
和wdPromptToSaveChanges。当选择wdDoNotSaveChanges时,文档就会来不及输出到打印机而关闭,当选择
wdPromptToSaveChanges时,word程序有时就会无法退出!poApp.Quit()语句也是不可取的,因为默认的是
wdPromptToSaveChanges。

poApp.Quit(Word.WdSaveOptions.wdPromptToSaveChanges)
poApp = Nothing
End If
End Sub

第二种,是调用windows打开的方式,很快,但是不幸的是不能更换打印机。

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim MyProcess As New Process
MyProcess.StartInfo.CreateNoWindow = False
MyProcess.StartInfo.Verb = "print"
MyProcess.StartInfo.FileName = "d:\1.doc"
MyProcess.Start()
MyProcess.WaitForExit(10000)
MyProcess.CloseMainWindow()
MyProcess.Close()
End Sub

***********************VB6下使用的方法:

Private Sub Form_Load()
Set Word1 = CreateObject("word.application")
Word1.Documents.Open App.Path & "\1.doc"
Word1.Visible = True
Word1.PrintOut
Word1.Application.Quit
End Sub