【发布时间】:2022-03-01 21:30:15
【问题描述】:
我的目标是通过 VBA 创建 ppt。我的桌面中已经有我需要使用的模板。这部分代码没问题。
但是我没有找到如何在 ppt 中选择幻灯片。我尝试了很多方法,但总是出错。
如果有人可以帮助我。
Option Explicit
Sub CreatePowerPoint()
Dim mySlide As PowerPoint.Slide
Dim myShapeRange As PowerPoint.Shape
Dim oPA As PowerPoint.Application
Dim oPP As PowerPoint.Presentation
Dim oPS As PowerPoint.SlideRange
Dim strTemplate As String
Dim rng As Range
strTemplate = "C:\Users\290866\Desktop\vba\PPT\Template.potx"
Set oPA = New PowerPoint.Application
oPA.Visible = msoTrue
oPA.Presentations.Open strTemplate, untitled:=msoTrue
If Not oPS Is Nothing Then Set oPS = Nothing
If Not oPP Is Nothing Then Set oPP = Nothing
If Not oPA Is Nothing Then Set oPA = Nothing
Err_PPT:
If Err <> 0 Then
MsgBox Err.Description
Err.Clear
Resume Next
End If
Set rng = ThisWorkbook.Sheets("Credit Recommendation").Range("B2:N59")
ActivePresentation.Slides (1)
rng.Copy
mySlide.Shapes.PasteSpecial (ppPasteBitmap)
Set myShapeRange = mySlide.Shapes(mySlide.Shapes.Count)
myShapeRange.LockAspectRatio = msoFalse
myShapeRange.Left = 20
myShapeRange.Top = 80
myShapeRange.Height = 400
myShapeRange.Width = 680
Application.CutCopyMode = False
End Sub
谢谢!!!
【问题讨论】:
-
您没有将幻灯片绑定到 varribale。将
ActivePresentation.Slides (1)替换为set mySlide = ActivePresentation.Slides(1) -
你好埃文,谢谢你的回答。我替换了该行,但现在出现错误:“ActiveX 组件无法创建对象”
-
也许您可以通过添加一行来帮助我...如何为所有幻灯片添加搜索和替换?
标签: vba powerpoint