vbs保存并关闭powerpoint演示文稿

时间:2017-03-10 23:19:14

标签: vbscript save powerpoint

我正在尝试保存,然后使用VBScript关闭所有微软应用程序,例如word,excel和powerpoint。

我已经说得很好并且工作出色:

'Word
On Error Resume Next
Set wd = GetObject(, "Word.Application")
On Error Goto 0

If Not IsEmpty(wd) Then
For Each doc In wd.Documents
doc.Save
doc.Close
Next

wd.Quit
End If

'Excel
On Error Resume Next

Set xl = GetObject(, "Excel.Application")
If Err Then
If Err.Number = 429 Then

WScript.Quit 0
Else

CreateObject("WScript.Shell").LogEvent 1, Err.Description & _
  " (0x" & Hex(Err.Number) & ")"
WScript.Quit 1
End If
End If
On Error Goto 0

xl.DisplayAlerts = False  

For Each wb In xl.Workbooks
wb.Save
wb.Close False
Next

xl.Quit
Set xl = Nothing

可悲的是,我还没弄明白如何用powerpoint做同样的事情。我在网上研究过,但还没有找到答案。我找到的所有工作都没有使用VBScript。

这是我正在努力工作的脚本:

'PowerPoint

On Error Resume Next
Set objPPT = GetObject("PowerPoint.Application")
On Error Goto 0

If Not IsEmpty(objPPT) Then
For Each doc In objPPT.Presentation
objPresentation.Save
objPresentation.Close
objPPT.Quit
Next

objPPT.Quit
End If

当我运行此脚本时,没有任何反应。

有人可以帮我修复我的脚本吗?

谢谢!

我很感激花费的时间和精力!

2 个答案:

答案 0 :(得分:0)

这段代码应该通过循环遍历演示文稿集合(使用Office 2010测试)来完成:

On Error Resume Next
Set objPPT = GetObject(,"powerpoint.Application")
On Error Goto 0

If Not IsEmpty(objPPT) Then
For Each doc In objPPT.Presentations
doc.Save
doc.Close

Next

objPPT.Quit
End If

答案 1 :(得分:0)

请注意,VBA<> VBScript中。可能想要更改标签以更正它。 无论如何,你的对象层次结构错误了

objPPT是一个PowerPoint应用程序对象。

Application对象有一个Presentations集合,每个打开的Presentation包含一个Presentation对象。 您想要遍历演示文稿集合。

试试这个。我从VBA大脑表面猜测VBScript语法:

On Error Resume Next
Set objPPT = GetObject("PowerPoint.Application")
On Error Goto 0

If Not IsEmpty(objPPT) Then
For Each Presentation In objPPT.Presentations
Presentation.Save
Presentation.Close
objPPT.Quit
Next

objPPT.Quit
End If
相关问题