Vba 在我的代码运行过程中,运行我的代码会给出对象“选择”失败的“方法”shaperage错误消息

Vba 在我的代码运行过程中,运行我的代码会给出对象“选择”失败的“方法”shaperage错误消息,vba,excel,powerpoint,Vba,Excel,Powerpoint,单步执行我的代码很有效,它完全符合我的要求,可以从Excel中的各种表格、文本框和图像创建powerpoint幻灯片。但是运行代码不起作用。我不认为这与时间有关,因为我在粘贴后的范围中包含了时间暂停。当代码到达newpowerpoint.activewindow.selection时。。。第I行获取对象“选择”的方法“ShapeRange”失败错误消息 任何帮助都将不胜感激 谢谢 Sub CreatePowerPoint() Worksheets("2 Source of Change

单步执行我的代码很有效,它完全符合我的要求,可以从Excel中的各种表格、文本框和图像创建powerpoint幻灯片。但是运行代码不起作用。我不认为这与时间有关,因为我在粘贴后的范围中包含了时间暂停。当代码到达newpowerpoint.activewindow.selection时。。。第I行获取对象“选择”的方法“ShapeRange”失败错误消息

任何帮助都将不胜感激

谢谢

Sub CreatePowerPoint()

    Worksheets("2 Source of Change and Switchi").Select

    Dim newPowerPoint As PowerPoint.Application
    Dim activeSlide As PowerPoint.Slide
    Dim cht As Excel.ChartObject

    On Error Resume Next
    Set newPowerPoint = GetObject(, "PowerPoint.Application")
    On Error GoTo 0

    newPowerPoint.Visible = True

    newPowerPoint.ActiveWindow.View.GotoSlidenewPowerPoint.ActivePresentation.Slides.count 
    Set activeSlide = newPowerPoint.ActivePresentation.Slides(newPowerPoint.ActivePresentation.Slides.count)


 '######################The Code below Copies and Pastes in My Values#####################

        Worksheets("3 Switching details – Actual v").Select


        ActiveSheet.Range(Range("D51"), Range("D51").End(xlDown)).Copy
        newPowerPoint.CommandBars.ExecuteMso ("PasteExcelTableSourceFormatting")
        Application.Wait (Now + TimeValue("0:00:01"))

        newPowerPoint.ActiveWindow.Selection.ShapeRange.Left = 640

end sub

最终使用一个单独的基于powerpoint的宏来定位表格

最终使用一个单独的基于powerpoint的宏来定位表格

解决了这个问题,我使用了这段代码来解决这个问题。似乎需要某种中断来正确运行代码

    With Application.ActiveWindow.Selection
        MsgBox "Click ok"
    End With

我已经使用了这个代码来解决这个问题。似乎需要某种中断来正确运行代码

    With Application.ActiveWindow.Selection
        MsgBox "Click ok"
    End With
Application.Wait不可靠,但由于它是一个时间问题,所以您的做法是正确的

ExecuteMso是一项异步任务,出现问题的原因是,在ExcecuteMso方法完成从剪贴板复制/粘贴之前,试图执行的下一行代码经常会遇到类似的错误

我建议在循环中使用Sleep函数来最小化CPU使用,只需在模块顶部声明它,如:

Private Declare Sub Sleep Lib "kernel32" (ByVal lngMilliSeconds As Long)
然后你的DoEvents循环如下:

ActiveSheet.Range(Range("D51"), Range("D51").End(xlDown)).Copy
activeSlide.Select  ' <~~ This may not be necessary, but added just in case.

Dim shapeCount as Long: shapeCount = activeSlide.Shapes.Count
newPowerPoint.CommandBars.ExecuteMso ("PasteExcelTableSourceFormatting")

Do
    Sleep 100
    DoEvents
Loop Until (activeSlide.Shapes.Count = shapeCount + 1)

' get rid of this next line and work with the slide/shape directly:
' XXX newPowerPoint.ActiveWindow.Selection.ShapeRange.Left = 640

activeSlide.Shapes(shapeCount+1).Left = 640
Application.Wait不可靠,但由于它是一个时间问题,所以您的做法是正确的

ExecuteMso是一项异步任务,出现问题的原因是,在ExcecuteMso方法完成从剪贴板复制/粘贴之前,试图执行的下一行代码经常会遇到类似的错误

我建议在循环中使用Sleep函数来最小化CPU使用,只需在模块顶部声明它,如:

Private Declare Sub Sleep Lib "kernel32" (ByVal lngMilliSeconds As Long)
然后你的DoEvents循环如下:

ActiveSheet.Range(Range("D51"), Range("D51").End(xlDown)).Copy
activeSlide.Select  ' <~~ This may not be necessary, but added just in case.

Dim shapeCount as Long: shapeCount = activeSlide.Shapes.Count
newPowerPoint.CommandBars.ExecuteMso ("PasteExcelTableSourceFormatting")

Do
    Sleep 100
    DoEvents
Loop Until (activeSlide.Shapes.Count = shapeCount + 1)

' get rid of this next line and work with the slide/shape directly:
' XXX newPowerPoint.ActiveWindow.Selection.ShapeRange.Left = 640

activeSlide.Shapes(shapeCount+1).Left = 640

是否可以调试.打印所选对象的类型名称?同时阅读本页的备注你好。对不起,我不知道怎么做,我有另一个宏,它说我的范围叫什么,它说它叫表1。这是从Excel工作表复制的范围(如果有帮助)。我阅读了备注,但我不明白为什么在逐步模式下可以正常工作,但在正常运行模式下不行。debug.printtypenamenewPowerPoint.ActiveWindow.Selection在打印的即时框中Selection@Chris考虑发布你的解决方案并接受它作为答案,这样对这个帖子的未来读者来说是很清楚的。找到。是否可以调试。打印所选对象的类型名称?同时阅读本页的备注你好。对不起,我不知道怎么做,我有另一个宏,它说我的范围叫什么,它说它叫表1。这是从Excel工作表复制的范围(如果有帮助)。我阅读了备注,但我不明白为什么在逐步模式下可以正常工作,但在正常运行模式下不行。debug.printtypenamenewPowerPoint.ActiveWindow.Selection在打印的即时框中Selection@Chris考虑发布你的解决方案并接受它作为答案,这样对这个帖子的未来读者来说是很清楚的。找到。您是否可以发布您使用的解决方案代码,因为我有相同的问题。您是否可以发布您使用的解决方案代码,因为我有相同的问题。