为所有打开的演示文稿执行宏

时间:2014-02-03 12:33:54

标签: vba powerpoint-vba

我使用以下代码在一个我的powerpoint的第一张幻灯片上放置一个形状。

Sub WriteToTextBox()
Dim tb As Shape
Dim sld As Slide
Dim pres As Presentation
Dim var1 As String


var1 = InputBox("Var1")
Set pres = ActivePresentation
Set sld = pres.Slides(1) 'Modify as needed

Set tb = sld.Shapes.AddTextbox(msoTextOrientationHorizontal, 400, 400, 100, 50) 
tb.Name = "InsertShape"  
tb.TextFrame.TextRange.Text = var1

End Sub

我知道要为我的所有公开演示文稿执行此操作。我试着这样做

Sub WriteToTextBoxALL()

Dim i as integer
Dim pptcount as integer

Dim tb As Shape
Dim sld As Slide
Dim pres As Presentation
Dim var1 As String

For i = 1 to pptcount

var1 = InputBox("Var1")
Set pres = ActivePresentation
Set sld = pres.Slides(1) 'Modify as needed

Set tb = sld.Shapes.AddTextbox(msoTextOrientationHorizontal, 400, 400, 100, 50) 
tb.Name = "InsertShape"  
tb.TextFrame.TextRange.Text = var1

Next
End Sub

但是我收到了一个错误。有人输入我做错了吗?

亲爱的,

Marc

1 个答案:

答案 0 :(得分:1)

您需要设置pptcount。我将您的代码更改为:

Sub WriteToTextBoxALL()

    Dim i As Integer
    Dim pptcount As Integer

    Dim tb As Shape
    Dim sld As Slide
    Dim pres As Presentation
    Dim var1 As String

    pptcount = Application.Presentations.Count

    For i = 1 To pptcount
        Set pres = Application.Presentations(i)

        var1 = InputBox("Var1")
        Set sld = pres.Slides(1)

        Set tb = sld.Shapes.AddTextbox(msoTextOrientationHorizontal, 400, 400, 100, 50)
        tb.Name = "InsertShape"
        tb.TextFrame.TextRange.Text = var1

    Next
End Sub

修改:

如果只是为您的形状绘制一个普通边框,请添加

tb.Line.Visible = True  

<小时/> Explanation to with-statements