运行时错误 5148 - 从代码打印选定的页面
Runtime Error 5148 - To print selected page from code
我正在尝试打印从 excel VBA 中选择的 word 文档页面,页码将从输入消息框中获取。我收到运行时错误 5148。
请帮助我。
Dim directory As String, fileName As String, ans As String, i As Integer
Dim objWord As Object
Dim intpage As Integer, intcopies As Integer
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
' path to the folder
directory = "E:\print\"
fileName = Dir(directory & "*.doc*") ' Open Multiple Word Docs Both .doc and .docx
Do While fileName <> ""
objWord.Documents.Open (directory & fileName)
On Error Resume Next
intpage = CInt(InputBox("Which page to print?"))
intcopies = CInt(InputBox("How many copies?"))
On Error GoTo 0
If intpage * intcopies <> 0 Then
For i = 1 To intcopies ' Loop to print next page of uer Choice Note: simplex is not working in my Office, Default Duplex
'### HERE IS THE PROBLEM###
objWord.PrintOut Range:=wdPrintRangeOfPages, Pages:=intpage
Next
Else
MsgBox "sorry, wrong page or copies, try again"
End If
'Next
objWord.Documents.Close
'set file to next in Dir
fileName = Dir()
Loop
试试这个:
objWord.PrintOut Range:=4, Pages:=CStr(intpage) '4=wdPrintRangeOfPages
注意:PrintOut
还有一个 Copies
参数,您可以使用它来代替循环。
我正在尝试打印从 excel VBA 中选择的 word 文档页面,页码将从输入消息框中获取。我收到运行时错误 5148。
请帮助我。
Dim directory As String, fileName As String, ans As String, i As Integer
Dim objWord As Object
Dim intpage As Integer, intcopies As Integer
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
' path to the folder
directory = "E:\print\"
fileName = Dir(directory & "*.doc*") ' Open Multiple Word Docs Both .doc and .docx
Do While fileName <> ""
objWord.Documents.Open (directory & fileName)
On Error Resume Next
intpage = CInt(InputBox("Which page to print?"))
intcopies = CInt(InputBox("How many copies?"))
On Error GoTo 0
If intpage * intcopies <> 0 Then
For i = 1 To intcopies ' Loop to print next page of uer Choice Note: simplex is not working in my Office, Default Duplex
'### HERE IS THE PROBLEM###
objWord.PrintOut Range:=wdPrintRangeOfPages, Pages:=intpage
Next
Else
MsgBox "sorry, wrong page or copies, try again"
End If
'Next
objWord.Documents.Close
'set file to next in Dir
fileName = Dir()
Loop
试试这个:
objWord.PrintOut Range:=4, Pages:=CStr(intpage) '4=wdPrintRangeOfPages
注意:PrintOut
还有一个 Copies
参数,您可以使用它来代替循环。