简体   繁体   English

vba function 参数问题

[英]vba function argument issue

Sub macro1()
showSlides (ActivePresentation)

End Sub


Function showSlides(ActivePresentation As Integer)
Dim ViewSlides As Integer
ViewSlides = ActivePresentation.Slides.Count
MsgBox ViewSlides

End Function

I've tried to remove the "as integer" from the argument, I still run into the same issue我试图从参数中删除“as integer”,我仍然遇到同样的问题

I want to build a function inside of PowerPoint that returns the amount of slides I have in my current presentation.我想在 PowerPoint 中构建一个 function 来返回我当前演示文稿中的幻灯片数量。 I am having an error when trying to use the activedocument object as an argument in my function.在我的 function 中尝试使用 activedocument object 作为参数时出现错误。

Do not use parenthesis when calling Subroutines, those are only for calling Functions that return something.调用子程序时不要使用括号,它们仅用于调用返回某些内容的函数。 Yours was defined as a Function, but since it never returned anything, and you werent assigning that to anything, there is no need for it to be a function. You did this just fine with your MsgBox call (no parenthesis)你的被定义为 Function,但由于它从未返回任何东西,而且你没有将它分配给任何东西,所以它没有必要成为 function。你在MsgBox调用中做得很好(没有括号)

And dim ActivePresentation As Presentation - Not Integer和昏暗的ActivePresentation As Presentation - 不是Integer

And it's better to not use a reserved word as a variable name, so I changed it to MyPresentation而且变量名最好不要用保留字,所以我改成了MyPresentation

Sub macro1()
  showSlides ActivePresentation
End Sub

Sub showSlides(MyPresentation As Presentation)
  Dim ViewSlides As Long
  ViewSlides = MyPresentation.Slides.Count
  MsgBox ViewSlides
End Sub

Alternatively, here it is written a little differently, using a Function. Now that it returns something, you can use that in the calling Subroutine.或者,这里它的写法有点不同,使用 Function。现在它返回了一些东西,您可以在调用子例程中使用它。

Sub macro1()
  MsgBox showSlides(ActivePresentation)
End Sub

Function showSlides(MyPresentation As Presentation) As Long
  showSlides = MyPresentation.Slides.Count
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM