【问题标题】:PowerPoint VBA select slidePowerPoint VBA 选择幻灯片
【发布时间】: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


【解决方案1】:

这是您修改后的代码。我解释下面的修改

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
    'changed this line to assign the new presentation to your variable
    Set oPP = 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("sheet1").Range("B2:N59")
    
    Set mySlide = oPP.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

您声明了变量,但从未将它们设置为等于任何值。我仍然没有看到 oPS 曾经在哪里使用过。

您收到 ActiveX 错误,因为 PowerPoint 没有活动的演示文稿。在 Office 中使用自己的对象而不是 ActiveAnything 总是更安全。所以我将oPP 设置为等于您的新演示文稿,然后使用oPP 而不是ActivePresentation

此外,除非您对发生的顺序很挑剔,否则您永远不需要将事物设置为等于无。 Sub 中声明的所有内容在 sub 末尾都设置为空。

希望这会有所帮助!

编辑:搜索和替换

This 是我获取代码的地方,但我将其修改为可调用的 Sub,因为我多次从不同的地方调用它:

'Find and Replace function
Sub FindAndReplace(sFind As String, sReplace As String, ByRef ppPres As PowerPoint.Presentation)
    Dim osld As PowerPoint.Slide
    Dim oshp As PowerPoint.Shape
    Dim otemp As PowerPoint.TextRange
    Dim otext As PowerPoint.TextRange
    Dim Inewstart As Integer
    
    For Each osld In ppPres.Slides
        For Each oshp In osld.Shapes
            If oshp.HasTextFrame Then
                If oshp.TextFrame.HasText Then
                    Set otext = oshp.TextFrame.TextRange
                    Set otemp = otext.Replace(sFind, sReplace, , msoFalse, msoFalse)
                    Do While Not otemp Is Nothing
                        Inewstart = otemp.Start + otemp.Length
                        Set otemp = otext.Replace(sFind, sReplace, Inewstart, msoFalse, msoFalse)
                    Loop
                End If
            End If
        Next oshp
    Next osld
End Sub

您必须将 2 个字符串和 Presentation 对象传递给它。它在你的 Sub 中看起来像这样

FindAndReplace("FindMe","ReplaceWithThis", oPP)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多