Excel 如何使用VBA将我的图片放置在现有powerpoint幻灯片的幻灯片中心?

Excel 如何使用VBA将我的图片放置在现有powerpoint幻灯片的幻灯片中心?,excel,vba,charts,powerpoint,Excel,Vba,Charts,Powerpoint,首先,我的图表被复制为图片,当我尝试粘贴它时,对象不支持此属性或方法。PPTPres.Slides28中每个oSh的行出现错误。但最终,我希望我的照片粘贴在幻灯片28的中心,稍微小一点。有谁能告诉我我哪里做错了,我该如何纠正 Option Explicit Sub ExportChartsToPowerPoint_SingleWorksheettesting() 'Declare PowerPoint Variables Dim PPTApp As Object

首先,我的图表被复制为图片,当我尝试粘贴它时,对象不支持此属性或方法。PPTPres.Slides28中每个oSh的行出现错误。但最终,我希望我的照片粘贴在幻灯片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
目前

期望

调试打印

请尝试以下示例代码:

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
请尝试以下示例代码:

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

对于PPTPres.Slides28中的每个oSh。Shapes@TimWilliams我想我以前也试过,我在PPTPres.Slides28中得到了每个oSh的类型不匹配错误。Shapes@Tim威廉姆斯,我想我以前也试过了,我得到了类型不匹配的错误。谢谢,它按预期工作。顺便问一下,为什么在将图表置于幻灯片中心的过程中,我们使用这个公式。Left=pgSet.SlideWidth-.Width/2?不管怎样,现在我正在尝试最小化图片的大小,所以我想我应该使用类似.Left=pgSet.SlideWidth-.Width/4的东西?同样,对于顶部?以及当我使用F8检查pgSet.SlideWidth和.width的值时,它不会显示任何我不了解的值。请尝试调试。打印oSh.width,oSh.Height,pgSet.SlideWidth,pgSet.slidehHeight滑块上形状居中的公式假设它比滑块小:如果它不是,则需要在居中之前调整其大小。至于为什么会这样:你需要知道幻灯片周围有多少空间,两边各有一半,上下各有一半。对不起,另一件让我困扰的事是,为什么当我设置oSh2.Width=600时,即时窗口显示509.769,如我的帖子所示,但对于oSh2.height,一切似乎都很好?比如oSh2.height=400,即时窗口显示相同的值这有关系吗?我不确定这是否有真正的区别。您锁定了纵横比了吗?谢谢,它可以正常工作。顺便问一下,为什么在将图表置于幻灯片中心的过程中,我们使用这个公式。Left=pgSet.SlideWidth-.Width/2?不管怎样,现在我正在尝试最小化图片的大小,所以我想我应该使用类似.Left=pgSet.SlideWidth-.Width/4的东西?同样,对于顶部?以及当我使用F8检查pgSet.SlideWidth和.width的值时,它不会显示任何我不了解的值。请尝试调试。打印oSh.width,oSh.Height,pgSet.SlideWidth,pgSet.slidehHeight滑块上形状居中的公式假设它比滑块小:如果它不是,则需要在居中之前调整其大小。至于为什么会这样:你需要知道幻灯片周围有多少空间,两边各有一半,上下各有一半。对不起,另一件让我困扰的事是,为什么当我设置oSh2.Width=600时,即时窗口显示509.769,如我的帖子所示,但对于oSh2.height,一切似乎都很好?比如oSh2.height=400,即时窗口显示相同的值这有关系吗?我不确定这是否有真正的区别。您是否锁定了纵横比?