【问题标题】:How to position my picture at the center of the slide in my existing powerpoint slide using VBA?如何使用 VBA 将我的图片放置在现有 powerpoint 幻灯片的幻灯片中心?
【发布时间】:2020-03-29 06:23:21
【问题描述】:

首先,我的图表被复制为图片,当我尝试粘贴时,对象不支持此属性或方法错误发生在 For Each oSh In PPTPres.Slides(28)。但最终,我希望将我的图片粘贴在幻灯片 28 的中心,并且稍微小一些。谁能告诉我我在哪里做错了,我应该如何纠正它?

 Option Explicit

 Sub ExportChartsToPowerPoint_SingleWorksheettesting()

    'Declare PowerPoint Variables
    Dim PPTApp As Object
    Dim PPTPres As Object
    Dim PPTShape As Object
    Dim mySlide As Object
    Dim myslide2 As Object

        Dim i As Long


    'Declare Excel Variables
    Dim Chrt As ChartObject

If PPTApp Is Nothing Then _
Set PPTApp = CreateObject(class:="PowerPoint.Application")

On Error GoTo 0
        PPTApp.Visible = True

    'Create new presentation in the PowerPoint application.
      Set PPTPres = PPTApp.Presentations.Open(Filename:="\\fab2crp-nas1\home22\kkang2\Profile\Desktop\myassignment3\mypresentationsample.pptx")

   Dim ppSlide As PowerPoint.Slide
        Set ppSlide = PPTPres.Slides(28)

        Dim j As Integer
        For j = ppSlide.Shapes.Count To 1 Step -1
            If ppSlide.Shapes(j).Type = msoPicture Then
                ppSlide.Shapes(j).Delete
            End If
        Next j


With PPTPres.Slides(28)
Sheets(4).Range("A1:M34").CopyPicture
            ppSlide.Shapes.Paste
End With


    Dim oSh As Shape

        For Each oSh In PPTPres.Slides(28) '<---object doesn't support this property or method
            With oSh
                If .Type = msoLinkedPicture _
                Or .Type = msoPicture Then

                ' position it to taste
                .Left = 100
                .Top = 100

                End If
            End With
        Next    ' Shape

End Sub

目前

预期

debug.print

【问题讨论】:

  • For Each oSh In PPTPres.Slides(28).Shapes
  • @Tim Williams 我想我以前也尝试过,但我得到了类型不匹配错误

标签: excel vba charts powerpoint


【解决方案1】:

试试这个(示例代码):

Sub Tester()

    Dim PPTApp As Object
    Dim PPTPres As Object
    Dim ppSlide As PowerPoint.Slide
    Dim Chrt As ChartObject
    Dim oSh 'As ShapeRange
    Dim pgSet

    'using already open PPT for testing....
    Set PPTApp = GetObject(, "PowerPoint.Application") 'get open ppt
    Set ppSlide = PPTApp.Presentations(1).Slides(1)    'the open presentation
    Set pgSet = PPTApp.Presentations(1).PageSetup      'for slide width/height

    Sheets(1).Range("A1:M34").CopyPicture
    Set oSh = ppSlide.Shapes.Paste() '<< get the pasted shape

    'center on slide
    With oSh
        .Left = (pgSet.SlideWidth - .Width) / 2
        .Top = (pgSet.SlideHeight - .Height) / 2
    End With

End Sub

【讨论】:

  • 谢谢,它按预期工作。顺便说一句,我可以问为什么在幻灯片上居中图表的过程中,我们使用这个等式 .Left = (pgSet.SlideWidth - .Width) / 2 ?无论如何,现在我试图最小化图片的大小,所以我想我应该使用类似 .Left = (pgSet.SlideWidth - .Width) / 4 的东西?顶部也一样?
  • 而且当我使用 F8 检查 pgSet.SlideWidth 和 .width 的值时,它没有显示任何我不理解的值
  • 试试Debug.Print oSh.Width, oSh.Height, pgSet.SlideWidth,pgSet.SlideHeight 在幻灯片上居中形状的公式假设它比幻灯片小:如果不是,那么您需要在居中之前调整它的大小。至于为什么这个等式:您需要找出幻灯片周围有多少空间,并且每边和顶部/底部各有一半。
  • 对不起,让我困扰的另一件事是,为什么当我设置 oSh2.Width = 600 时,即时窗口显示 509.769(如我的帖子所示)但对于 osh2.height,一切似乎都很好?(与 osh2.height=400 一样,即时窗口显示相同的值)
  • 这有关系吗?我不确定那里有真正的区别。是否锁定了纵横比?
猜你喜欢
  • 2015-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-22
  • 1970-01-01
  • 1970-01-01
  • 2014-07-14
相关资源
最近更新 更多