Home > Blockchain >  Print specific pages from word file from excel vba
Print specific pages from word file from excel vba

Time:09-05

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

CodePudding user response:

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)
  • Related