简体   繁体   中英

Print specific pages from word file from excel vba

I'm trying to print specific pages from a word file using Excel VBA, but I couldn't do that, I was only able to print the whole file and not the pages I wanted (changes according the value of LastPage). This is the code I used. Please help with that, thanks.

Sub PrintFile()
Dim objWord As Object
Dim objDoc  As Object

Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("D:file.docx")
objWord.Visible = False
objDoc.PrintOut from:="1", To:="LastPage".value
objWord.Quit
End Sub

Assuming you are getting an error on the line starting objDoc.PrintOut then replace that line with

objDoc.PrintOut Range:=wdPrintFromTo, From:="1", To:=CStr(LastPage)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM