VBA Powerpoint参考带有变量

时间:2017-06-15 20:36:57

标签: vba powerpoint

我正在尝试编写一个vba循环,它将检测幻灯片上所有ActiveX文本框的值。但是我在编写"变量"的代码时遇到了问题。在文本框参考中。例如,需要在循环中引用TextBox(i)。我是一个整数,我将值设置为。

Dim i as Integer

For i = 1 to 4

 If IsNull(Slide1.Shapes.("TextBox" & i).Value) = True 
     Then (Slide1.Shapes.("TextBox" & i).Value) = 0
              Else: ...

Next i

但是这个脚本不起作用,我无法找到如何正确编码脚本的这个可变部分的源代码。有一些关于使用Me.Controls的讨论但是我没有创建表单。有人愿意在我的脚本中分享错误吗?

2 个答案:

答案 0 :(得分:2)

这会将i的值放入TextBox i中。我想,应该让你开始。

Sub Example()

    Dim oSh As Shape
    Dim i As Integer

    On Error Resume Next

    For i = 1 To 4

        Set oSh = ActivePresentation.Slides(1).Shapes("TextBox" & CStr(i))
        If Err.Number = 0 Then  ' shape exists
            oSh.OLEFormat.Object.Text = CStr(i)
        End If

    Next i

End Sub

答案 1 :(得分:0)

@Steve Rindsberg你有正确的代码。谢谢。这是获取值的最终脚本,如果为空则设置值。

    For i = 1 To 4

            'set oSh to TextBox1, TextBox2, TextBox3... etc.
            Set oSh = ActivePresentation.Slides(1).Shapes("TextBox" & CStr(i))

                'set myVar to value of this TextBox1, TextBox2...
                myVar = oSh.OLEFormat.Object.Value

                    If myVar = "" Then _
                        ActivePresentation.Slides(1).Shapes("Text" & CStr(i)).OLEFormat.Object.Value = 0 _
                    Else: 'do nothing

                    'clear value of myVar
                    myVar = ""

    'start on next integer of i                
    Next i