VBA中的PowerPoint幻灯片计数变量

3

我正在用VBA创建Powerpoint幻灯片。我的计划是设置代码,使每个子程序都创建特定的幻灯片。但是,我需要将幻灯片编号传递给每个子程序,以便它可以在正确位置创建正确的幻灯片。我无法定义在Excel VBA中的 .Slides.Count。我知道它从技术上讲是Long,但是在另一个不再工作的代码片段中,我成功地将其传递为Integer。

我的问题如下:

  1. .Slides.Count函数。这从技术上讲是Long还是Integer?如果它是Long,为什么要这样定义呢?是因为整数有限制而长整型没有吗?
  2. 我该如何通过子程序将 .Slides.Count 变量传递到我的新幻灯片?作为Integer还是Long?

以下是一些示例代码:

Sub CreatePres()

Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dim ppSlide As PowerPoint.Slide
Dim ppTextbox As PowerPoint.Shape

Set ppApp = New PowerPoint.Application
ppApp.Visible = True
ppApp.Activate

Set ppPres = ppApp.Presentations.Add
slidesCount = ppPres.Slides.Count
Set ppSlide = ppPres.Slides.Add(slidesCount + 1, ppLayoutTitle)
ppSlide.Shapes(1).TextFrame.TextRange.Text = "Hello world"
ppSlide.Shapes(2).TextFrame.TextRange.Text = Date
slidesCount = slidesCount + 1

Call slide2(slidesCount)

End Sub
Sub slide2(i As Integer)
Set ppSlide = ppPres.Slides.Add(i + 1, ppLayoutTitle)
ppSlide.Select
ppSlide.Shapes(1).TextFrame.TextRange.Text = "Hello world"
ppSlide.Shapes(2).TextFrame.TextRange.Text = Date

End Sub

Slides.Count 是一个 Long 类型,但是你在代码中没有声明 slidesCount 的类型,所以 VBA 可能会将其视为 Variant 类型。在任何 VB/VBA 模块的第一行始终包括 Option Explicit;这将强制你在使用变量之前正确地声明它们的类型。为什么要使用 Long?据我所知,这是因为 Long 比 Integer 更有效率。另外,请注意,在 Sub Slide2 中不需要 ppSlide.Select。 - Steve Rindsberg
1个回答

2

http://vba.relief.jp/powerpoint-vba-get-active-slide-number/

设置当前幻灯片变量的方法是:
foo = ActiveWindow.Selection.SlideRange.SlideIndex

然后用 Call slide2(slidesCount) 调用函数。

请尝试以下操作:

Sub CreatePres()
    Dim ppApp As PowerPoint.Application
    Dim ppPres As PowerPoint.Presentation
    Dim ppSlide As PowerPoint.Slide
    Dim ppTextbox As PowerPoint.Shape

    Set ppApp = New PowerPoint.Application
    ppApp.Visible = True
    ppApp.Activate

    Set ppPres = ppApp.Presentations.Add
    slidesCount = ppPres.Slides.Count
    Set ppSlide = ppPres.Slides.Add(slidesCount + 1, ppLayoutTitle)
    ppSlide.Shapes(1).TextFrame.TextRange.Text = "Hello world"
    ppSlide.Shapes(2).TextFrame.TextRange.Text = Date
    slidesCount = ActiveWindow.Selection.SlideRange.SlideIndex
    Call slide2(slidesCount)
End Sub

Sub slide2(i As Integer)
    Set ppSlide = ppPres.Slides.Add(i + 1, ppLayoutTitle)
    ppSlide.Select
    ppSlide.Shapes(1).TextFrame.TextRange.Text = "Hello world"
    ppSlide.Shapes(2).TextFrame.TextRange.Text = Date
End Sub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接