运行时错误5148 - 从代码打印所选页面

时间:2015-01-13 23:21:53

标签: excel-vba ms-word word-vba vba excel

我正在尝试从excel VBA打印word文档选择页面,页面编号将从输入消息框中获取。我得到了Runtime ERROR 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

1 个答案:

答案 0 :(得分:0)

试试这个:

objWord.PrintOut Range:=4, Pages:=CStr(intpage) '4=wdPrintRangeOfPages

注意:PrintOut也有一个Copies参数,您可以使用该参数代替循环。