-2

I can't seem to get Words PrintOut to accept/honor the parameter for PAGES when run in VBScript. Weirdly, it honors COPIES just fine. Any ideas?

Code:

Dim ObjWord
Set ObjWord = CreateObject("Word.Application")
ObjWord.Visible = True

'Open Document
Dim ObjDoc
'https://learn.microsoft.com/en-us/office/vba/api/word.documents.open
'.Open (FileName, ConfirmConversions, ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate, Revert, WritePasswordDocument, WritePasswordTemplate, Format, Encoding, Visible, OpenConflictDocument, OpenAndRepair, DocumentDirection, NoEncodingDialog)
Set ObjDoc = ObjWord.Documents.Open("C:\tmp\test.docx", ,TRUE, , , , , , , , ,TRUE)  
       
'PageRange
'https://learn.microsoft.com/en-us/office/vba/api/word.application.printout
'.PrintOut (Background, Append, Range, OutputFileName, From, To, Item, Copies, Pages, PageType, PrintToFile, Collate, FileName, ActivePrinterMacGX, ManualDuplexPrint, PrintZoomColumn, PrintZoomRow, PrintZoomPaperWidth, PrintZoomPaperHeight)  
Dim ObjPrint
ObjPrint = ObjDoc.PrintOut(FALSE, , , , , , , ,"1", , ,TRUE)   ' No Error, but Pages not honored
'ObjPrint = ObjDoc.PrintOut(FALSE, , , , , , ,"2", , , ,TRUE)    ' Corretly Printes Two Copies
      
objDoc.Saved = TRUE
objWord.Quit

Set ObjDoc = Nothing
Set objWord = Nothing
braX
  • 11,506
  • 5
  • 20
  • 33
FreeSoftwareServers
  • 2,271
  • 1
  • 33
  • 57
  • As stated in your previous questions, read [the documentation](https://learn.microsoft.com/en-us/office/vba/api/word.document.printout). – user692942 Aug 17 '20 at 08:54
  • It's not biased, the question has been answered years ago. The fact you're using the same thing in VBScript is irrelevant and is just repeating old topics, you want to add value add the answer to the existing question and maybe even add the [tag:vbscript] tag as well. – user692942 Aug 17 '20 at 14:28
  • It is RUDE to say `As stated in your previous questions`. I did read the documentation, I did search SO for related topics. The linked duplicate has a horrible title and is tagged `VBA` and `MS-WORD-2010` neither of which I am using. You don't see me complain that mods didn't update the tags to be more relevant. Ironically, I have tagged some of my `VBScript` posts w/ `VBA` and had mods remove it. Why are you pushing people away from SO?? – FreeSoftwareServers Aug 17 '20 at 16:14
  • Tags age, that doesn't mean the information is any less relevant. – user692942 Aug 17 '20 at 16:21
  • My intention is not to push you away, just trying to maintain what we have without oodles of duplicates. A lot of your questions are self answered, which is fine but make sure you are not going over old ground with them. – user692942 Aug 17 '20 at 16:22

1 Answers1

-1

So I had to pass an additional param for it to honour the Pages value which was Range.

https://learn.microsoft.com/en-us/office/vba/api/word.document.printout https://documentation.help/MS-Office-Word-VB/womthPrintOut1.htm

It required a CONSTANT declaration. Eg:

https://learn.microsoft.com/en-us/office/vba/api/word.wdprintoutrange

Specifies a range to print.

WDPRINTOUTRANGE ENUMERATION (WORD)
Name              Value Description
wdPrintAllDocument  0   The entire document.
wdPrintCurrentPage  2   The current page.
wdPrintFromTo       3   A specified range.
wdPrintRangeOfPages 4   A specified range of pages.
wdPrintSelection    1   The current selection.

In my case, I believe it will always be 4.

Therefore this worked as expected. (Print Only Page 2)

ObjPrint = ObjDoc.PrintOut(FALSE, ,"4", , , , , ,"2", , ,TRUE)

Further Reading on Printing Word via VBScript. Disclaimer: This is my personal blog --> https://www.freesoftwareservers.com/display/FREES/Print+Microsoft+Word+via+Batch+-+WINWORD.EXE+Switches+-+VBScript+Print+Specific+Pages

FreeSoftwareServers
  • 2,271
  • 1
  • 33
  • 57
  • If it required constant declaration, why are you not using them in the code sample? Also, `PrintOut()` method doesn't return an object so `ObjPrint` is completely unnecessary, plus if it was an object reference you would expect `Set ObjPrint = ...`. – user692942 Aug 17 '20 at 09:26
  • Incorrect information? What does `ObjPrint` return?, because I guarantee it isn't an object reference. Where are your declared constants in the code sample? – user692942 Aug 17 '20 at 14:25
  • Try using it without `VAR = PrintOut` and VB complains `Can't Call Sub w/ Parantheses`. Try with `SET` and it doesn't work! I may not know exactly the terms, but I do post an MVCE and EXPECT users to try before rudely and incorrectly stating code is `completely unnecessary` – FreeSoftwareServers Aug 17 '20 at 16:10
  • If you see in my full code example, I post the entire positional array of arguments and since my answer will always utilize `4` or `wdPrintRangeOfPages` I don't feel the extra line of code is worth the value. Basically, when it's something that doesn't change I tend to use the `Enumeration` vs declaring a constant and using that, but there is definite debate on best practices for that which I may regret/be wrong by not using the "extra code" for readability in this case. – FreeSoftwareServers Aug 17 '20 at 16:20
  • 1
    In which case you either 1. Remove the brackets and it will work `ObjDoc.PrintOut False, , "4", , , , , , "2", , , True` or 2. use `Call ObjDoc.PrintOut(False, , "4", , , , , , "2", , ,True)`. Either of those will work. – user692942 Aug 17 '20 at 16:20