在PPTA 2010中的VBA中向文本框添加多行

时间:2016-01-23 21:22:35

标签: vba powerpoint powerpoint-vba

我正在尝试引用一个文本框(名为WarningText1)并根据用户在GUI中选择的内容输出多行文本。这就是我想要的:

Private Sub WarningInfo()
    Call Dictionary.WarningInfo 
    Call Dictionary.WindInfo 

    'Sets the font for the warning information text.
    With ActiveWindow.Selection.SlideRange.Shapes("WarningText1").TextFrame2.TextRange.Font 
        .Size = 24 
        .Name = "Aharoni" 
        .Shadow.Visible = True 
    End With 

    ComboBoxList = Array(CStr(ComboBox2), CStr(ComboBox3), CStr(ComboBox4), CStr(ComboBox5), CStr(ComboBox6)) 

    For Each Ky In ComboBoxList 
        On Error Resume Next 
         'If nothing is selected in all dropdowns, do nothing and exit this sub.
        If ComboBox2 = "" And ComboBox3 = "" And ComboBox4 = "" And ComboBox5 = "" And ComboBox6 = "" Then 
            Exit Sub 
             'Otherwise, if even one dropdown has a selection, insert selected text.
        Else 
            ActiveWindow.Selection.SlideRange.Shapes("WarningText1").TextFrame2.TextRange.Text = dict2.Item(Ky)(0) & dict3.Item(Ky)(0) 
        End If 
    Next 

    Set dict2 = Nothing 

End Sub 

当我使用EITHER dict2或dict3行运行该行代码时,它运行良好。但它似乎无法以这种方式将它们组合在一起。

运行时,我收到错误:"输入不匹配"。

另外,当我把dict2.Item(Ky)(0) & dict2.Item(Ky)(0)(同样的东西两次)放进去时,它就可以了。

我认为有一个简单的解决方案,但我似乎无法在网上找到它。感谢任何帮助!

***** EDIT *****

我添加了代码来帮助调试。这是在不起作用的行之前添加的:

Debug.Print dict2.Item(Ky)(0)
Debug.Print dict3.Item(Ky)(0)

这两个值都打印出了他们应该的样子。它只是不会将它们都添加到同一个文本框中。

****编辑2 ****

正如您在上面的代码中所看到的,我将字典调用到此Sub中。这是我的词典的代码:

Option Private Module 'This is necessary so that these modules do not show up in the PPT Macro window.
Public dict, dict2, dict3 As Object, Key, val 'Makes the dictionaries public so they can be accessed by other Modules.

Sub WarningInfo()

'This is the dictionary for the maximum expected hail size.

Set dict2 = CreateObject("Scripting.Dictionary")

Key = "No Hail": val = Array("No hail expected")
dict2.Add Key, val
Key = "0.25""": val = Array("Hail: Up to 0.25""")
dict2.Add Key, val
Key = "0.50""": val = Array("Hail: Up to 0.50""")
dict2.Add Key, val

End Sub

Sub WindInfo()

'This is the dictionary for the maximum expected wind speed.

Set dict3 = CreateObject("Scripting.Dictionary")

Key = "35 mph": val = Array("Wind: Up to 35 mph")
dict3.Add Key, val
Key = "40 mph": val = Array("Wind: Up to 40 mph")
dict3.Add Key, val
Key = "45 mph": val = Array("Wind: Up to 45 mph")
dict3.Add Key, val

End Sub

字典是我设置变量的地方,dict2和dict3。

谢谢,

科里

0 个答案:

没有答案