2

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

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

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

4

2 回答 2

5
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。新幻灯片将添加到演示文稿中,而不会出现 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
于 2013-09-08T18:22:44.460 回答
0

打开隐藏的 ppt App 窗口

oPres = oApp.Presentations.Open(ppt_file, , , Microsoft.Office.Core.MsoTriState.msoFalse)
于 2020-05-19T23:21:19.860 回答