从 PowerPoint 中 VBA 中的文本框中读取信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22160883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:11:04  来源:igfitidea点击:

Reading Information from a textbox in VBA in PowerPoint

vbapowerpoint

提问by Theodore_Kim

I just found out about VBA yesterday and I am having fun playing with it, however I encountered a problem: how do you read the contents of a textbox. Its the only textbox on the slide and I want it to be able to apply to all slides in the PowerPoint. Please ask me to clarify if that does not make sense.

我昨天才发现 VBA 并且我玩得很开心,但是我遇到了一个问题:你如何阅读文本框的内容。它是幻灯片上唯一的文本框,我希望它能够应用于 PowerPoint 中的所有幻灯片。如果这没有意义,请让我澄清。

--EDIT-- Basically, I want to read contents of a text box, simple as that.

--EDIT-- 基本上,我想阅读文本框的内容,就这么简单。

--EDIT-- Here is my current code:

--编辑--这是我当前的代码:

Sub answer()
    Dim lCurrentView As Long
    Dim myInput As String
    Dim sld As Slide
    Set sld = Application.ActiveWindow.View.Slide
    myInput = sld.Shapes(4).TextFrame.TextRange.Text
    A = InputBox(prompt:="Your Answer:")
    MsgBox (myInput)
    If A = myInput Then
        MsgBox ("Correct!")
        ActivePresentation.SlideShowWindow _
        .View.GotoSlide Int(Rnd * _
        ActivePresentation.Slides.Count) + 1
    Else
        MsgBox ("Sorry, try again...")
    End If
End Sub

回答by stenci

Try to run this macro and check the result in the Debug window (press Ctrl+G to open it). Execute it step by step (pressing F8) and put some breakpoints (pressing F9) and check the object browser (select one variable and press Shift+F2)

尝试运行此宏并在调试窗口中检查结果(按 Ctrl+G 打开它)。一步一步执行(按F8)并放置一些断点(按F9)并检查对象浏览器(选择一个变量并按Shift+F2)

Sub Test()
  Dim Sld As Slide, Shp As Shape
  For Each Sld In ActivePresentation.Slides
    For Each Shp In Sld.Shapes
      Select Case Shp.Type
        Case MsoShapeType.msoTextBox
          Debug.Print Sld.Name, Shp.Name, Shp.TextFrame.TextRange.Text
        Case Else
          Debug.Print Sld.Name, Shp.Name, "This is not a text box"
      End Select
    Next Shp
  Next Sld
End Sub

回答by Mike

Type some text in the first textbox on the first slide of your presentation. Then open the VBA editor, right click under "VBAProject", and select "Add Module". In the new module, paste the following code and press the "play" button.

在演示文稿第一张幻灯片的第一个文本框中键入一些文本。然后打开VBA编辑器,在“VBAProject”下右击,选择“Add Module”。在新模块中,粘贴以下代码并按“播放”按钮。

Sub Textbox_reader()
    Dim myInput As String
    myInput = ActivePresentation.Slides(1).Shapes(1).TextFrame.TextRange.Text
    MsgBox (myInput)
End Sub