gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:42:44 28 4
gpt4 key购买 nike

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

它打开输入文件,将其转换为 .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 : Invalid request. There is no active presentation.

错误是从 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。新幻灯片将添加到演示文稿中,而不会出现 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

关于vbscript - 如何在看不到窗口的情况下以编程方式打开 Powerpoint?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18667195/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com