Excel VBA运行时错误-2147221164 (80040154) 类未注册

Excel VBA Run-time error -2147221164 (80040154) Class not registered


问题

你好,提前感谢你的帮助!

我是VBA的新手,我遇到了一个以前从未见过的错误。我在两台电脑上测试(个人和公司),在两台电脑上都得到了这个错误。

我试图用excel vba代码在powerpoint中获取图片的位置,使用即时窗口,我得到的错误是运行时错误-2147221164 (80040154) 类没有注册

对powerpoint的引用,当然还有Microsoft excel的设置。(见下文,不知道图片是否能显示)。
60516-image.png

下面是代码

s ub picsize()

Dim SD As Slide

Dim SH As Shape

Set shp = ActivePresentation.Slides

问题

.Shapes

问题


Debug.Print shp.Top
Debug.Print shp.Left
Debug.Print shp.Width
Debug.Print shp.Height


End Sub

任何帮助都值得感谢!


答案1

你声明了一个变量SH,但用shp代替。
如果你是在Excel中运行这段代码,不清楚ActivePresentation指的是什么。
当你运行这个宏时,PowerPoint应该是打开的吗?如果是的话,它应该是这样的:

 Sub picsize()
     Dim PP As PowerPoint.Application
     Dim PR As PowerPoint.Presentation
     Dim SD As PowerPoint.Slide
     Dim SH As PowerPoint.Shape
    
     On Error Resume Next
    
     Set PP = GetObject(Class:="PowerPoint.Application")
     If PP Is Nothing Then
         MsgBox "PowerPoint is not running!", vbExclamation
         Exit Sub
     End If
    
     Set PR = PP.ActivePresentation
     If PR Is Nothing Then
         MsgBox "There is no active presentation in PowerPoint!", vbExclamation
         Exit Sub
     End If
    
     Set SD = PR.Slides 
答案1

If SD Is Nothing Then MsgBox "The active presentation doesn't contain any slides!", vbExclamation Exit Sub End If Set SH = SD.Shapes

答案1

If SH Is Nothing Then MsgBox "The first slide doesn't contain any shapes!", vbExclamation Exit Sub End If Debug.Print SH.Top Debug.Print SH.Left Debug.Print SH.Width Debug.Print SH.Height End Sub