Search code examples
vbscriptpowerpoint

Cant set OutputType for PowerPoint Print with VBScript


I am able to open a pptx file via vbscript and print it, but I can't for the life of me get the OutputType set. I continuallly get this error no matter my formatting, I even copied direct from Microsoft docs. Any idea whats wrong?>

https://learn.microsoft.com/en-us/office/vba/api/powerpoint.printoptions

Error:

C:\Windows\system32>cscript.exe C:\tmp\print_ppt.vbs
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

C:\tmp\print_ppt.vbs(26, 9) Microsoft PowerPoint 2016: PrintOptions.OutputType : Invalid enumeration value.

Code:

Dim objPPT
Dim objPresentation
Dim objPrintOptions

Set objPPT = CreateObject("PowerPoint.Application")
objPPT.Visible = True

'Presentations.Open "filename", boolReadOnly, boolOpenUntitled, boolWithWindow
Set objPresentation = objPPT.Presentations.Open("C:\tmp\test.pptx",msoTrue,,msoFalse)

Set objPrintOptions = objPresentation.PrintOptions
objPrintOptions.PrintInBackground = False
   'https://learn.microsoft.com/en-us/office/vba/api/powerpoint.printoptions.outputtype
'objPrintOptions.OutputType = ppPrintOutputSixSlideHandouts
'objPresentation.PrintOut


With objPresentation 
    With .PrintOptions 
        .NumberOfCopies = 2 
        .Collate = False 
        .PrintColorType = ppPrintColor 
        .PrintHiddenSlides = True 
        .FitToPage = True 
        .FrameSlides = True 
        .OutputType = ppPrintOutputSlides 
    End With 
    .PrintOut 
End With

objPresentation.PrintOptions.PrintInBackground = True
objPPT.Quit
Set objPPT = nothing

Solution

  • VBA and VBScript are similar obviously, but different. This was a good read for me tonight to start seeing some of the "minor" differences which can give you a headache if you are familiar w/ VBA and expect VBScript to work the same.

    https://www.oreilly.com/library/view/vbscript-in-a/1565927206/ch01s03.html#:~:text=VBScript%20is%20a%20subset%20of%20the%20Visual%20Basic%20for%20Applications%20language.&text=VBScript%20is%20an%20untyped%20language,variables%20in%20VBScript%20are%20variants.

    Specifcally the part about how VBScript does not support named arguments. This is a similar situation where the propertity will work with VBScript, but the sytax in VBA has multiple options and only one in VBScript will work.

    In this case it was using the numerical value.

    Another example is my line w/

        'Presentations.Open "filename", boolReadOnly, boolOpenUntitled, boolWithWindow
        Set objPresentation = objPPT.Presentations.Open("C:\tmp\test.pptx",msoTrue,,msoFalse)
    

    This is using positional arguments vs named value pairs.

    Here is where I found the numerical version for the option, now to script some logic into my script so end users can pass an argument like 4 and expect 4Slides/Page.

    https://learn.microsoft.com/en-us/office/vba/api/PowerPoint.PpPrintOutputType

    With objPresentation 
        With .PrintOptions 
            .PrintInBackground = False
           'https://learn.microsoft.com/en-us/office/vba/api/PowerPoint.PpPrintOutputType
           'https://learn.microsoft.com/en-us/office/vba/api/powerpoint.printoptions.outputtype
           .OutputType = 8 
        End With 
        '.PrintOut 
       .PrintOptions.PrintInBackground = True
    End With