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

时间:2013-09-06 21:57:05

标签: vbscript ms-office powerpoint msdn

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

制作

打开输入文件,将其转换为.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 method

第四个参数是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

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

2 个答案:

答案 0 :(得分:2)

objPPT.Presentations.Open inputFile,,msoFalse

需要另一个逗号

objPPT.Presentations.Open inputFile,,,msoFalse

参数是:

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

你告诉它打开输入文件,只读,没有无标题,并将WithWindow parm保留为默认值(True),打开带有可见窗口。

请记住,你不能编写任何选择任何东西的代码(需要一个可见的窗口),但由于你没有这样做,你应该好好去。

[附录编辑] 安斯加是正确的(为我早先的错误评论道歉)。我们不允许无形地调用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

答案 1 :(得分:0)

使用隐藏的ppt应用程序窗口打开

oPres = oApp.Presentations.Open(ppt_file, , , Microsoft.Office.Core.MsoTriState.msoFalse)