【问题标题】:Why Does It Make A Difference If A Macro is Run When Slide Show Has Started为什么在幻灯片放映开始时运行宏会有所不同
【发布时间】:2020-10-22 12:09:37
【问题描述】:

抱歉,如果这是一个基本问题,但我是 VBA 的新手,过去两周一直在学习这门语言。我正在尝试使用 PowerPoint 制作学习资源。

我制作了一个很好的宏,当它在幻灯片放映之外运行时,它似乎可以完美运行。 (我通常只是在我的 VBA 编辑器中按 F5 来查看代码在做什么。)但是,当我在幻灯片放映运行时(使用操作按钮)尝试运行宏时,它不会按预期工作。更准确地说,它似乎停止运行并且没有显示任何错误或提供宏停止的原因。

我尝试对此主题进行 Google 搜索,但找不到任何内容。如果有人能提供任何理由或建议任何信息来源,说明为什么在幻灯片放映运行时宏的工作方式与不运行时不同,我将不胜感激?是否有任何标准做法可确保宏在幻灯片放映时有效?

感谢您的任何cmets!

编辑 02/07/20 - 回复评论我附上了下面的代码,并解释了我在下面的 cmets 中尝试做什么

Sub DataInputMacro()
'This procedure attempts to group (and do a check) of all 20 answer tiles.
Dim oSh As Shape
Dim i As Integer 'This is for the For statement
Dim iix As Integer 'This is to store the x position of the tiles.
Dim iiy As Integer 'This is to store the y position of the tiles.

For i = 1 To 20
If i = 1 Or i = 3 Or i = 5 Or i = 7 Or i = 9 Then
iix = 9
ElseIf i = 2 Or i = 4 Or i = 6 Or i = 8 Or i = 10 Then
iix = 198
ElseIf i = 11 Or i = 13 Or i = 15 Or i = 17 Or i = 19 Then
iix = 587
Else
iix = 774
End If

If i = 1 Or i = 2 Or i = 11 Or i = 12 Then
iiy = 9
ElseIf i = 3 Or i = 4 Or i = 13 Or i = 14 Then
iiy = 113
ElseIf i = 5 Or i = 6 Or i = 15 Or i = 16 Then
iiy = 218
ElseIf i = 7 Or i = 8 Or i = 17 Or i = 18 Then
iiy = 323
Else
iiy = 428
End If

ActiveWindow.Selection.Unselect 'This ensures that nothing has already been selected on the slide (which could then get grouped with the first rectangle!)

    For Each oSh In ActivePresentation.Slides("DataInput").Shapes
        If IsWithinRange(oSh, iix - 1, iiy - 1, iix + 179, iiy + 97) Then
        'MsgBox oSh.Name & " " & oSh.Left
                    oSh.Select (msoFalse)
        End If
    Next
    If ActiveWindow.Selection.ShapeRange.Count > 1 Then
    With ActiveWindow.Selection.ShapeRange.Group
    .Name = "GroupAnswer" & i
    .Select
    End With
    
    ElseIf ActiveWindow.Selection.ShapeRange.Type = msoGroup Then
    ActiveWindow.Selection.ShapeRange.Name = "GroupAnswer" & i & "a"
    
    ElseIf ActiveWindow.Selection.ShapeRange.Count = 1 Then
    MsgBox "Sorry, but there is a set up issues of your answer boxes. Either (1) one of your answer boxes does not contain a separate textbox or image or (2) there is a missing yellow rectangle! I note for point (1) the text must be placed in a separate text box to the yellow rectangle!"
    End
    End If
Next i

UpdateTitle 'This is a separate function that updates the title on the front slide.
CheckFor20Groups 'This is a function that checks that there are 20 groups for the rest of the program to use!

End Sub

【问题讨论】:

  • 您能否提供更多有关这方面的信息?例如,您的宏在正常运行时会做什么?如果可能,请发布代码(或您怀疑可能存在问题的地方的片段)
  • 我一定会提供更多信息和代码!
  • 目前我正在设计一个幻灯片,让用户手动创建自己的答案对,供玩家匹配。宏会检查答案并将其分组以供程序的其余部分使用。
  • 我通过创建一个黄色矩形来做到这一点,用户在其中添加至少一个文本框或图像或任何其他形状。宏遍历每个黄色矩形并检查其上是否至少有一个其他形状,最后将其分组。这些组被赋予了适当的名称供程序的其余部分使用!
  • 我已将代码包含在原始问题中。我想问题一定源于我如何选择形状来制作每个组?

标签: vba powerpoint slideshow


【解决方案1】:

Shape.Select 方法在幻灯片视图中不起作用 - 请参阅此处的备注:Shape.Select method (PowerPoint) - 因此您将不得不使用另一种方法来处理您的形状 - 尝试使用变量来引用您正在使用的 ShapeRange 而不是比 ActiveWindow.Selection.ShapeRange - 这样您就可以使用它们而无需选择它们。

【讨论】:

  • Joerg - 非常感谢您的回答。您能否举一个使用变量来引用形状范围的示例?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-18
  • 1970-01-01
  • 1970-01-01
  • 2017-04-11
  • 2020-03-18
相关资源
最近更新 更多