以下代码,是我的程序中使用word打印的函数。程序在我的电脑内win2000,win98下安装,都能正确打印。在别的电脑就不正常了,提示非法操作。
请教各位高手,不胜感激!!!!
Public Function fun打印(strFileName As String, strData() As String, n As Integer)
Dim wrdApp As New Word.Application
With wrdApp
.Visible = False
.Documents.Add (App.Path & "\文件管理\发证模版\" & strFileName)
End With
Dim i As Integer
For i = 0 To n
With wrdApp.Selection.Find .ClearFormatting
.Text = strData(i, 0)
.Replacement.ClearFormatting
.Replacement.Text = strData(i, 1)
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With
Next i
On Error GoTo CheckError
wrdApp.Documents(1).PrintOut Range:=wdPrintCurrentPage
CheckError:
Resume Next
wrdApp.Documents(1).Close SaveChanges:=wdDoNotSaveChanges Set wrdApp = Nothing
End Function
请教各位高手,不胜感激!!!!
Public Function fun打印(strFileName As String, strData() As String, n As Integer)
Dim wrdApp As New Word.Application
With wrdApp
.Visible = False
.Documents.Add (App.Path & "\文件管理\发证模版\" & strFileName)
End With
Dim i As Integer
For i = 0 To n
With wrdApp.Selection.Find .ClearFormatting
.Text = strData(i, 0)
.Replacement.ClearFormatting
.Replacement.Text = strData(i, 1)
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With
Next i
On Error GoTo CheckError
wrdApp.Documents(1).PrintOut Range:=wdPrintCurrentPage
CheckError:
Resume Next
wrdApp.Documents(1).Close SaveChanges:=wdDoNotSaveChanges Set wrdApp = Nothing
End Function
如果程序可以运行,但无法调用Word,检查目标机器有没有安装Word,或者Word有没有激活。
还有一种情况,就是目标Word的版本比你开发环境的Word新,也会造成这种情况。
在同一程序中,用一下代码,不隐藏word文档,在word中点击“打印”菜单,打印是正常的Public Function funWordPrint(strFileName As String, strData() As String, n As Integer)
Dim wrdApp As New Word.Application
With wrdApp
.Visible = True
.Documents.Add (App.Path & "\文件管理\发证模版\" & strFileName)
End With
Dim i As Integer
For i = 0 To n
With wrdApp.Selection.Find .ClearFormatting
.Text = strData(i, 0)
.Replacement.ClearFormatting
.Replacement.Text = strData(i, 1)
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With
Next i
Set wrdApp = Nothing
End Function