将文本附加到形状而不覆盖VBA中的现有文本

时间:2016-01-24 22:22:01

标签: vba powerpoint

我在VBA中有一个脚本,它将某些用户选择的变量打印到PPT模板。在这个子目录中:

Private Sub WarningInfo()
Call Dictionary.WarningInfo

  'Sets the font for the warning information text.

   With ActiveWindow.Selection.SlideRange.Shapes("WarningText1").TextFrame2.TextRange.Font

    .Size = 24
    .Name = "Calibri"
    .Shadow.Visible = True

   End With

ComboBoxList = Array(CStr(ComboBox3))

   For Each Ky In ComboBoxList

   'On Error Resume Next
   'If nothing is selected in ComboBox3, do nothing and exit this sub.
    If ComboBox3 = "" Then
    Exit Sub
    'Otherwise, if it has a selection, insert selected text.
    Else
     ActiveWindow.Selection.SlideRange.Shapes("WarningText1").TextFrame2.TextRange = vbCrLf & dict2.Item(Ky)(0)

    End If

 Next

Set dict2 = Nothing

End Sub

它将在形状WarningText1中打印出dict2.Item(Ky)(0)。该变量由用户在GUI中选择,并从字典中提取。选择和输出的一个例子是"没有预期的冰雹"。

我的下一个要点是:

Private Sub WarningInfo2()
Call Dictionary.WindInfo

  'Sets the font for the warning information text.

   With ActiveWindow.Selection.SlideRange.Shapes("WarningText1").TextFrame2.TextRange.Font

    .Size = 24
    .Name = "Calibri"
    .Shadow.Visible = True

   End With

ComboBoxList = Array(CStr(ComboBox4))

   For Each Ky In ComboBoxList

   'On Error Resume Next
   'If nothing is selected in ComboBox4, do nothing and exit this sub.
    If ComboBox4 = "" Then
    Exit Sub
    'Otherwise, if it has a selection, insert selected text.
    Else
     ActiveWindow.Selection.SlideRange.Shapes("WarningText1").TextFrame2.TextRange = vbCrLf & dict3.Item(Ky)(0)

    End If

 Next

Set dict3 = Nothing

End Sub

它将打印出dict3.Item(Ky)(0)。但是,通过设置第二个子的方式,它只会覆盖第一个子数据(因为两个子数据都在同一个UserForm中)。我需要找到一种方法来更改这行代码ActiveWindow.Selection.SlideRange.Shapes("WarningText1").TextFrame2.TextRange = vbCrLf & dict3.Item(Ky)(0),以便将此文本添加到形状中的现有文本" WarningText1"。

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

ActiveWindow.Selection.SlideRange.Shapes(“WarningText1”)。TextFrame2.TextRange = ActiveWindow.Selection.SlideRange.Shapes(“WarningText1”)。TextFrame2.TextRange& vBCrLf& dict3.Item(Ky)(0)