VBSCRIPT PPT转换脚本

VBSCRIPT PPT转换脚本,vbscript,powerpoint,Vbscript,Powerpoint,我正在尝试使用VBSCRIPT将PPT文件转换为PPTX文件。我已经很长时间没有使用VB了&我对这个框架非常不熟悉。我正试图修改一个将PPTX/PPT转换为PDF的脚本,但是运气不好。这是我到目前为止得到的一个例子 Option Explicit Dim inputFile Dim objPPT Dim objPresentation Dim objPrintOptions Dim objFso Dim pptf If WScript.Arguments.Count <> 1 T

我正在尝试使用VBSCRIPT将PPT文件转换为PPTX文件。我已经很长时间没有使用VB了&我对这个框架非常不熟悉。我正试图修改一个将PPTX/PPT转换为PDF的脚本,但是运气不好。这是我到目前为止得到的一个例子

Option Explicit

Dim inputFile
Dim objPPT
Dim objPresentation
Dim objPrintOptions
Dim objFso
Dim pptf

If WScript.Arguments.Count <> 1 Then
    WriteLine "You need to specify input and output files."
    WScript.Quit
End If

inputFile = WScript.Arguments(0)

Set objFso = CreateObject("Scripting.FileSystemObject")

If Not objFso.FileExists( inputFile ) Then
    WriteLine "Unable to find your input file " & inputFile
    WScript.Quit
End If



WriteLine "Input File:  " & inputFile

Set objPPT = CreateObject( "PowerPoint.Application" )

objPPT.Visible = True
objPPT.Presentations.Open inputFile

Set objPresentation = objPPT.ActivePresentation
objPresentation.SaveAs "out.pptx", Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsOpenXMLPresentation

objPresentation.Close
ObjPPT.Quit
选项显式
Dim输入文件
模糊对象
模糊表示
Dim OBJPRINT选项
Dim objFso
暗淡pptf
如果WScript.Arguments.Count为1,则
WriteLine“您需要指定输入和输出文件。”
WScript.Quit
如果结束
inputFile=WScript.Arguments(0)
设置objFso=CreateObject(“Scripting.FileSystemObject”)
如果不存在objFso.FileExists(inputFile),则
WriteLine“找不到您的输入文件”&inputFile
WScript.Quit
如果结束
WriteLine“输入文件:”&输入文件
设置objPPT=CreateObject(“PowerPoint.Application”)
objPPT.Visible=True
objPPT.Presentations.Open inputFile
设置objPresentation=objPPT.ActivePresentation
objPresentation.SaveAs“out.pptx”,Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppsaveasopenxmlspresentation
对象表示。关闭
退出
围绕objPresentation.SaveAs行,事情变成了梨形;显然,它的语法是非法的——但是我不确定这里的最佳路线。任何帮助都将不胜感激。另外,如果有其他变量(或api文档链接)用于转换doc->docx和xls->xlsx。 提前谢谢

编辑: 我自己找到了解决办法;很抱歉,我在发布后几天就停止了对该线程的检查。我找到了这段代码的文档页,特别注意到一个函数(convert2):

我将下面的答案标记为答案;因为它是第一位的(尽管我还没有测试过)。如果你感兴趣-这是我的代码;AFAIK它只能在各种PowerPoint格式之间转换(任意方向)。另外,仅供参考,我修改了另一个关于该主题的流行谷歌脚本;我唯一改变的一行是最后一行(convert2方法)。无论如何。。。(另外-这需要office 2010;根据文档)

用法: CSCRIPT scriptName.vbs C:\inputfileName.ppt C:\outputFileName.pptx

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.convert2(output)

objPresentation.Close
ObjPPT.Quit
选项显式
子写入线(strLine)
WScript.Stdout.WriteLine strLine
端接头
' http://msdn.microsoft.com/en-us/library/office/aa432714(v=办公室12)aspx
常量msoFalse=0'错误。
常量msoTrue=-1'为真。
' http://msdn.microsoft.com/en-us/library/office/bb265636(v=办公室12)aspx
Const ppFixedFormatIntentScreen=1'目的是在屏幕上查看导出的文件。
Const ppFixedFormatIntentPrint=2'目的是打印导出的文件。
' http://msdn.microsoft.com/en-us/library/office/ff746754.aspx
常量ppFixedFormatTypeXPS=1'XPS格式
常量ppFixedFormatTypePDF=2'PDF格式
' http://msdn.microsoft.com/en-us/library/office/ff744564.aspx
Const ppPrintHandoutVerticalFirst=1'幻灯片垂直排列,第一张幻灯片在左上角,第二张幻灯片在左下角。
Const ppPrintHandAuthorizontalFirst=2'幻灯片水平排列,第一张幻灯片在左上角,第二张幻灯片在右上角。
' http://msdn.microsoft.com/en-us/library/office/ff744185.aspx
常量ppPrintOutputSlides=1'幻灯片
Const ppPrintOutputWOSlideHandouts=2'两张幻灯片讲义
常量PPPrintOutputThreeSlidesHandouts=3'三张幻灯片讲义
常量ppPrintOutputSixSlideHandouts=4'六张幻灯片讲义
常量ppPrintOutputNotesPages=5'注释页
常量ppPrintOutputOutline=6'轮廓
Const ppPrintOutputBuildSlides=7'生成幻灯片
常量ppPrintOutputFourSlideHandouts=8'四张幻灯片讲义
常量PPPrintOutputNinesSlide讲义=9'九张幻灯片讲义
常量PPPrintOutputneslideHandouts=10'单幻灯片讲义
' http://msdn.microsoft.com/en-us/library/office/ff745585.aspx
Const ppPrintAll=1'打印演示文稿中的所有幻灯片。
Const ppPrintSelection=2'打印幻灯片选择。
Const ppPrintCurrent=3'打印演示文稿中的当前幻灯片。
Const ppPrintSlideRange=4'打印一系列幻灯片。
Const ppPrintNamedSlideShow=5'打印命名幻灯片。
' http://msdn.microsoft.com/en-us/library/office/ff744228.aspx
常量ppShowAll=1'全部显示。
Const ppShowNamedSlideShow=3'显示名为slideshow的内容。
常量ppShowSlideRange=2'显示幻灯片范围。
'
'这是实际的脚本
'
Dim输入文件
Dim输出文件
模糊对象
模糊表示
Dim OBJPRINT选项
Dim objFso
如果WScript.Arguments.Count为2,则
WriteLine“您需要指定输入和输出文件。”
WScript.Quit
如果结束
inputFile=WScript.Arguments(0)
outputFile=WScript.Arguments(1)
设置objFso=CreateObject(“Scripting.FileSystemObject”)
如果不存在objFso.FileExists(inputFile),则
WriteLine“找不到您的输入文件”&inputFile
WScript.Quit
如果结束
如果存在objFso.files(outputFile),则
WriteLine“您的输出文件(“&outputFile&”)已存在!”
WScript.Quit
如果结束
WriteLine“输入文件:”&输入文件
WriteLine“输出文件:”&输出文件
设置objPPT=CreateObject(“PowerPoint.Application”)
objPPT.Visible=True
objPPT.Presentations.Open inputFile
设置objPresentation=objPPT.ActivePresentation
设置objPrintOptions=objPresentation.PrintOptions
objPrintOptions.Ranges.Add 1,objPresentation.Slides.Count
objPrintOptions.RangeType=ppShowAll
"这方面的参考http://msdn.microsoft.com/en-us/library/office/ff746080.aspx
objPresentation.convert2(输出)
对象表示。关闭
退出

通常,您可以在PowerPoint中使用
ExportAsFixedFormat(…)
执行此操作。由于选择了VBS,因此必须使用
SaveAs(…)

我假设您也希望能够将ppt/pptx批量转换为pdf,而不是逐个指定完整的文件名

Option Explicit

'http://msdn.microsoft.com/en-us/library/office/bb251061(v=office.12).aspx
Const ppSaveAsPDF = 32

Dim oFSO ' Public reference to FileSystemObject
Dim oPPT ' Public reference to PowerPoint App

Main

Sub Main()
    Dim sInput

    If wscript.Arguments.Count <> 1 Then
        Wscript.Echo "You need to specify input and output files."
        wscript.Quit
    End If

    ' PowerPoint version must be 12 or later (PowerPoint 2007 or later)
    Set oPPT = CreateObject("PowerPoint.Application")
    If CDbl(oPPT.Version) < 12 Then
        Wscript.Echo "PowerPoint version must be 2007 or later!"
        oPPT.Visible = True
        oPPT.Quit
        Set oPPT = Nothing
        wscript.Quit
    End If
    ' Store Input Argument and detect execute mode (single file / Folder batch mode)
    sInput = wscript.Arguments(0)
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    If IsPptFile(sInput) Then
        PPT2PDF sInput
    ElseIf oFSO.FolderExists(sInput) Then
        Wscript.Echo "Batch Start: " & Now
        Wscript.Echo "Root Folder: " & sInput
        BatchPPT2PDF sInput
    Else
        Wscript.Echo """" & sInput & """ is not a PPT file or Folder!"
    End If
    ' Close PowerPoint app if no other presentations are opened
    If oPPT.Presentations.Count = 0 Then oPPT.Quit
    Set oPPT = Nothing
    Set oFSO = Nothing
End Sub

Private Sub BatchPPT2PDF(sFDR)
    Dim oFDR, oFile
    Wscript.Echo String(50, Chr(151))
    Wscript.Echo "Processing Folder: " & sFDR
    For Each oFile In oFSO.GetFolder(sFDR).Files
        If IsPptFile(oFile.Name) Then
            PPT2PDF(oFile)
        End If
    Next
    For Each oFDR In oFSO.GetFolder(sFDR).SubFolders
        BatchPPT2PDF oFDR
    Next
End Sub

Private Function IsPptFile(sFile)
    IsPptFile = (InStr(1, Right(sFile, InStrRev(sFile, ".")), "ppt") > 0)
End Function

Private Sub PPT2PDF(sFile)
    On Error Resume Next
    Dim sPDF, oPres
    sPDF = Left(sFile,InstrRev(sFile,".")) & "pdf"
    Set oPres = oPPT.Presentations.Open(sFile, True, False, False) ' Read Only, No Title, No Window
    Err.Clear
    oPres.SaveAs sPDF, ppSaveAsPDF
    oPres.Close
    Set oPres = Nothing
    If Err.Number = 0 Then
        Wscript.Echo "OK" & vbTab & sPDF
    Else
        Wscript.Echo "X" & vbTab & sPDF & " [ERR " & Err.Number & ": " & Err.Description & "]"
        Err.Clear
    End If
End Sub
选项显式
'http://msdn.microsoft.com/en-us/library/office/bb251061(v=关