2015-01-13 132 views
0

打印選定頁面我想從Excel VBA中打印Word文檔選定頁面,頁碼將從輸入消息框中獲得。我得到運行時錯誤5148.運行時錯誤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 
+0

'wdPrintRangeOfPages'是Word對象庫中定義的常量--Excel並不知道它是什麼。要麼添加對Word庫的引用,要麼替換實際值。 –

+0

我是學習者,我不知道如何編碼,你能提示確切的代碼 – Bharath

+0

把'4'代替'wdPrintRangeOfPages' –

回答

0

試試這個:

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

注:PrintOut也有Copies參數,你可以使用,而不是循環。

+0

其工作謝謝......蒂姆 – Bharath