我有这个脚本,由另一个用户制作
打开输入文件,将其转换为.pdf并将其另存为输出文件。
但是,PowerPoint也打开,并且我看到了实际的窗口加载。
此过程将在服务器上运行,所以我认为每次用户要转换内容时加载GUI都将不必要地占用资源。
有什么方法可以以编程方式打开PowerPoint而不会弹出GUI?
我尝试替换
objPPT.Visible = True
with
objPPT.Visible = False
但是这引发了一个错误,告诉我不可能那样做。
我也尝试过替换
objPPT.Presentations.Open inputFile
with
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的内容,我正在研究它。
objPPT.Presentations.Open inputFile,,msoFalse
需要另一个逗号
objPPT.Presentations.Open inputFile,,,msoFalse
这些参数是:
Presentations.Open "filename", boolReadOnly, boolOpenUntitled, boolWithWindow
您告诉它打开输入文件,只读,没有标题,并且将WithWindow参数保留为其默认值(True),该参数将打开可见窗口。
请记住,您不能编写任何选择任何内容的代码(需要可见的窗口),但是由于您没有这样做,因此应该会做的很好。
[附加的编辑内容]安斯加(Ansgar)的正确(对我之前的错误评论表示歉意)。我们不允许无形地调用PPT,但是如果您无窗口创建/打开演示文稿,则PPT永远不会出现。我对VBS脚本还不太熟悉,无法解决您遇到的确切问题,但是已经在PPT 2013 / Win8和PPT 2010 / Win7中测试了此VBA。没有幻灯片的新幻灯片被添加到演示文稿中。
' 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
使用隐藏的ppt应用程序窗口打开
oPres = oApp.Presentations.Open(ppt_file, , , Microsoft.Office.Core.MsoTriState.msoFalse)