如何在不查看应用程序的情况下使用 Excel VBA 创建 PowerPoint

问题描述 投票:0回答:2

我想知道如何从 Excel VBA 创建新的 PPT(我已经有代码),但在创建时却看不到应用程序。我发现了一些见解,但它仅在打开现有 PPT 文件时才有效,但我正在创建一个新文件。

Dim pptApp As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptSlide As PowerPoint.Slide
Dim pptShape As PowerPoint.Shape
Dim excelTable As Excel.Range
Dim SlideTitle As String
Dim SlideText As String
Dim SlideObject As Object
Dim pptTextbox As PowerPoint.Shape
Dim SlideNumber As String

On Error Resume Next
Set pptApp = New PowerPoint.Application
Err.Clear

Set pptPres = pptApp.Presentations.Add
pptPres.PageSetup.SlideSize = ppSlideSizeOnScreen
vba excel powerpoint
2个回答
2
投票

.Active
上调用
PowerPoint.Application
就是这样做的 - 它会激活它,从而使窗口可见:

Dim ppt As PowerPoint.Application
Set ppt = New PowerPoint.Application
Debug.Print ppt.Visible  '<--Prints 0 (msoFalse)
ppt.Activate             '<--THIS SHOWS THE WINDOW.
Debug.Print ppt.Visible  '<--Prints -1 (msoTrue)

只需完全删除

pptApp.Activate
线即可。

正如评论中提到的,您还需要修复错误处理程序。在这种情况下,最好的解决方法是将其完全删除。

GetObject
返回现有实例(如果存在)。我假设当你说“创建一个新的 PPT”时,你的意思是“附加到正在运行的 PowerPoint 实例(如果存在),否则创建一个新实例”。这就是您的代码当前所做的。

另外,正如评论中提到的,如果您引用了 Microsoft PowerPoint X.X 对象库(如

Dim pptApp As PowerPoint.Application
所证明),您也不应该使用
CreateObject
。那是为了后期绑定。如果您有参考,请使用早期绑定。

最后,当您创建 PowerPoint.Application 时,它默认情况下不可见。您可以通过将代码减少到这一行来“修复”您的代码:

Set pptApp = New PowerPoint.Application

0
投票

子CreateDobriChintulovPresentation() Dim pptApp 作为对象 将 pptPresentation 变暗为对象 变暗 pptSlide 作为对象

' Create a new PowerPoint application
Set pptApp = CreateObject("PowerPoint.Application")

' Add a new presentation
Set pptPresentation = pptApp.Presentations.Add

' Add 5 slides
For i = 1 To 5
    Set pptSlide = pptPresentation.Slides.Add(i, ppLayoutText)
    pptSlide.Shapes(1).TextFrame.TextRange.Text = "Slide " & i
Next i

' Save the presentation (change the file path as needed)
pptPresentation.SaveAs "C:\Path\to\YourPresentation.pptx"

' Clean up
pptPresentation.Close
pptApp.Quit
Set pptSlide = Nothing
Set pptPresentation = Nothing
Set pptApp = Nothing

MsgBox "Presentation created successfully!", vbInformation

结束子

© www.soinside.com 2019 - 2024. All rights reserved.