2

I have this script, made by another user

It opens the input file, converts it to a .pdf and saves it as the output file.

However, PowerPoint also opens, and I see the actual window load up.

This process is going to be running on a server, so I figure that loading up the GUI every time a user wants to convert something is going to be unnecessarily resource intensive.

Is there any way to programmatically open PowerPoint without the GUI popping up?


I've tried replacing

objPPT.Visible = True with

objPPT.Visible = False

But that throws an error telling me that it cannot be that way.

I've also tried replacing

objPPT.Presentations.Open inputFilewith

objPPT.Presentations.Open inputFile,,,msoFalse

But that gives me an error saying:

Microsoft PowerPoint 2013: Application.ActivePresentation : Invalid request. There is no active presentation.

The error is triggered from the Set objPresentation = objPPT.ActivePresentation line.


From doing some research about the subject, I found that some people have success by using the Open method

The fourth parameter is WithWindow. Theoretically, this should open the presentation without a window if set to false.

But whatever I do to it doesn't seem to work.

WithWindow:= false gives me a syntax error


' Courtesy BillP3rd of superuser.com

Option Explicit

Sub WriteLine ( strLine )
    WScript.Stdout.WriteLine strLine
End Sub

' http://msdn.microsoft.com/en-us/library/office/aa432714(v=office.12).aspx
Const msoFalse = 0   ' False.
Const msoTrue = -1   ' True.

' http://msdn.microsoft.com/en-us/library/office/bb265636(v=office.12).aspx
Const ppFixedFormatIntentScreen = 1 ' Intent is to view exported file on screen.
Const ppFixedFormatIntentPrint = 2  ' Intent is to print exported file.

' http://msdn.microsoft.com/en-us/library/office/ff746754.aspx
Const ppFixedFormatTypeXPS = 1  ' XPS format
Const ppFixedFormatTypePDF = 2  ' PDF format

' http://msdn.microsoft.com/en-us/library/office/ff744564.aspx
Const ppPrintHandoutVerticalFirst = 1   ' Slides are ordered vertically, with the first slide in the upper-left corner and the second slide below it.
Const ppPrintHandoutHorizontalFirst = 2 ' Slides are ordered horizontally, with the first slide in the upper-left corner and the second slide to the right of it.

' http://msdn.microsoft.com/en-us/library/office/ff744185.aspx
Const ppPrintOutputSlides = 1               ' Slides
Const ppPrintOutputTwoSlideHandouts = 2     ' Two Slide Handouts
Const ppPrintOutputThreeSlideHandouts = 3   ' Three Slide Handouts
Const ppPrintOutputSixSlideHandouts = 4     ' Six Slide Handouts
Const ppPrintOutputNotesPages = 5           ' Notes Pages
Const ppPrintOutputOutline = 6              ' Outline
Const ppPrintOutputBuildSlides = 7          ' Build Slides
Const ppPrintOutputFourSlideHandouts = 8    ' Four Slide Handouts
Const ppPrintOutputNineSlideHandouts = 9    ' Nine Slide Handouts
Const ppPrintOutputOneSlideHandouts = 10    ' Single Slide Handouts

' http://msdn.microsoft.com/en-us/library/office/ff745585.aspx
Const ppPrintAll = 1            ' Print all slides in the presentation.
Const ppPrintSelection = 2      ' Print a selection of slides.
Const ppPrintCurrent = 3        ' Print the current slide from the presentation.
Const ppPrintSlideRange = 4     ' Print a range of slides.
Const ppPrintNamedSlideShow = 5 ' Print a named slideshow.

' http://msdn.microsoft.com/en-us/library/office/ff744228.aspx
Const ppShowAll = 1             ' Show all.
Const ppShowNamedSlideShow = 3  ' Show named slideshow.
Const ppShowSlideRange = 2      ' Show slide range.

'
' This is the actual script
'

Dim inputFile
Dim outputFile
Dim objPPT
Dim objPresentation
Dim objPrintOptions
Dim objFso

If WScript.Arguments.Count <> 2 Then
    WriteLine "You need to specify input and output files."
    WScript.Quit
End If

inputFile = WScript.Arguments(0)
outputFile = WScript.Arguments(1)

Set objFso = CreateObject("Scripting.FileSystemObject")

If Not objFso.FileExists( inputFile ) Then
    WriteLine "Unable to find your input file " & inputFile
    WScript.Quit
End If

If objFso.FileExists( outputFile ) Then
    'WriteLine "Your output file (' & outputFile & ') already exists!"
    'WScript.Quit
End If

WriteLine "Input File:  " & inputFile
WriteLine "Output File: " & outputFile

Set objPPT = CreateObject( "PowerPoint.Application" )

objPPT.Visible = True

objPPT.Presentations.Open inputFile
Set objPresentation = objPPT.ActivePresentation
Set objPrintOptions = objPresentation.PrintOptions

objPrintOptions.Ranges.Add 1,objPresentation.Slides.Count
objPrintOptions.RangeType = ppShowAll

' Reference for this at http://msdn.microsoft.com/en-us/library/office/ff746080.aspx
objPresentation.ExportAsFixedFormat outputFile, ppFixedFormatTypePDF, ppFixedFormatIntentScreen, msoTrue, ppPrintHandoutHorizontalFirst, ppPrintOutputSlides, msoFalse, objPrintOptions.Ranges(1), ppPrintAll, "Slideshow Name", False, False, False, False, False

objPresentation.Close
ObjPPT.Quit

I found out about OpenXML, and I'm looking into that.

2 Answers2

6
objPPT.Presentations.Open inputFile,,msoFalse

It needs another comma

objPPT.Presentations.Open inputFile,,,msoFalse

The parms are:

Presentations.Open "filename", boolReadOnly, boolOpenUntitled, boolWithWindow

You're telling it to open the input file, read-only, not untitled and leaving the WithWindow parm at its default value (True) which opens WITH a visible window.

Bear in mind that you can't write any code that selects anything (that requires a visible window), but since you're not doing this, you should be good to go.

[APPENDED EDITS] Ansgar's correct (apologies for my earlier mistaken comment). We're not allowed to invoke PPT invisibly, BUT if you create/open a presentation windowlessly, PPT never appears. I'm not familiar enough with VBS scripting to work out the exact problem you're seeing, but have test this VBA in PPT 2013/Win8 and PPT 2010/Win7. New slides get added to the presentation w/o PPT ever appearing.

' Add a Slide to a Microsoft PowerPoint Presentation
 Const ppLayoutText = 2
 Dim objPPT As Object
 Dim objPresentation As Object
 Dim objSlide As Object

Set objPPT = CreateObject("PowerPoint.Application")

Set objPresentation = objPPT.presentations.Open("c:\temp\something.pptx", , , msoFalse)
Set objSlide = objPresentation.Slides.Add(1, ppLayoutText)

objPresentation.Save
objPPT.Quit
Steve Rindsberg
  • 14,442
  • 1
  • 29
  • 34
  • When I tried that, I get an error from Powerpoint that says `Microsoft PowerPoint 2013: Applicatio n.ActivePresentation : Invalid request. There is no active presentation.` –  Sep 08 '13 at 19:36
  • The error is triggered from the Set `objPresentation = objPPT.ActivePresentation line.` –  Sep 09 '13 at 14:48
  • Please update to: objPresentation = oApp.Presentations.Open(ppt_file, , , Microsoft.Office.Core.MsoTriState.msoFalse) – Alaa Sadik May 19 '20 at 23:19
0

Open with hidden ppt App window

oPres = oApp.Presentations.Open(ppt_file, , , Microsoft.Office.Core.MsoTriState.msoFalse)
Alaa Sadik
  • 1,029
  • 12
  • 19