如何在不看到窗口的情况下以编程方式打开 Powerpoint?

2 vbscript msdn powerpoint ms-office

我有这个脚本,由另一个用户制作

它打开输入文件,将其转换为 .pdf 并将其另存为输出文件。

但是,PowerPoint 也会打开,我看到实际的窗口已加载。

这个过程将在服务器上运行,所以我认为每次用户想要转换某些东西时加载 GUI 将是不必要的资源密集型。

有没有办法在不弹出 GUI 的情况下以编程方式打开 PowerPoint?


我试过更换

objPPT.Visible = True

objPPT.Visible = False

但这会引发一个错误,告诉我它不能那样。

我也试过更换

objPPT.Presentations.Open inputFile

objPPT.Presentations.Open inputFile,,,msoFalse

但这给了我一个错误说:

Microsoft PowerPoint 2013:Application.ActivePresentation:请求无效。没有活动的演示文稿。

错误是从Set objPresentation = objPPT.ActivePresentation线路触发的 。


通过对该主题的一些研究,我发现有些人通过使用Open 方法取得了成功

第四个参数是WithWindow。理论上,如果设置为 false,这应该打开没有窗口的演示文稿。

但我对它所做的一切似乎都不起作用。

WithWindow:= false 给我一个语法错误


' 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
Run Code Online (Sandbox Code Playgroud)

我发现了 OpenXML,我正在研究它。

Ste*_*erg 5

objPPT.Presentations.Open inputFile,,msoFalse
Run Code Online (Sandbox Code Playgroud)

它需要另一个逗号

objPPT.Presentations.Open inputFile,,,msoFalse
Run Code Online (Sandbox Code Playgroud)

参数是:

Presentations.Open "filename", boolReadOnly, boolOpenUntitled, boolWithWindow
Run Code Online (Sandbox Code Playgroud)

您告诉它以只读方式打开输入文件,而不是无标题,并将 WithWindow parm 保留为其默认值 (True),它会打开一个可见窗口。

请记住,您不能编写任何选择任何内容的代码(需要一个可见的窗口),但既然您没有这样做,那么您应该很高兴。

[附加编辑] Ansgar 是正确的(为我之前的错误评论道歉)。我们不允许在无形中调用 PPT,但是如果您无窗口地创建/打开演示文稿,则 PPT 永远不会出现。我对 VBS 脚本不够熟悉,无法解决您所看到的确切问题,但已在 PPT 2013/Win8 和 PPT 2010/Win7 中测试了此 VBA。新幻灯片被添加到演示文稿中,而没有 PPT 出现。

' 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
Run Code Online (Sandbox Code Playgroud)