确定VBA中文本框中的行数

时间:2016-04-07 19:53:58

标签: vba powerpoint powerpoint-vba

我在GUI中设置了一个文本框,用户可以在其中输入信息。然后在PPT幻灯片的文本框中吐出该字符串。根据PPT幻灯片中文本框中使用的行数,我需要在文本框中的文本下方输入下一组信息以及许多新行。以下是我到目前为止的情况:

这是用户在GUI中的文本框中输入文本并将其放在PPT幻灯片中的文本框中的代码:

Private Sub Location()

   With ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2
     'Make sure there is text in the call to action textbox. If not, display an error message.
     If C2AText = "" Then
       MsgBox "Woah there! You need to enter text in the location/call to action box."
     'Otherwise, if text is inserted, place that text in the WarningData box found on the PPT slide.
     Else
       .TextRange = C2AText
       .TextRange.Paragraphs.Font.Size = 21
       .TextRange.Paragraphs.Font.Name = "Calibri"
       .TextRange.Paragraphs.Font.Shadow.Visible = True
       .TextRange.Paragraphs.Font.Bold = msoTrue
     End If
   End With

End Sub

此文本确定是否在HailInfo下拉列表中选择了任何内容。如果是,我需要在前一个Sub中插入的C2AText下面放置这么多行:

Private Sub HailInfo()
Call Dictionary.HailInfo

ComboBoxList = Array(CStr(HailDropDown))

   For Each Ky In ComboBoxList
   'On Error Resume Next
     With ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2
       'If nothing is selected in HailDropDown, do nothing and exit this sub.
       If HailDropDown = "" Then
         Exit Sub
       'If a hail option is selected, execute the following code.
       ElseIf HailDropDown <> "" And C2AText.LineCount = 2 Then
         .TextRange = .TextRange & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & dict2.Item(Ky)(0)
       ElseIf HailDropDown <> "" And C2AText.LineCount = 3 Then
         .TextRange = .TextRange & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & dict2.Item(Ky)(0)
       End If
     End With
   Next

   Set dict2 = Nothing

End Sub

在HailInfo子中使用C2AText.LineCount似乎没有做任何事情。它不会在任何地方插入冰雹文本,所以我不确定我做错了什么。任何帮助将不胜感激...谢谢!!

1 个答案:

答案 0 :(得分:0)

你应该尝试以下......

Private Sub HailInfo()
Call Dictionary.HailInfo

ComboBoxList = Array(CStr(HailDropDown))

   For Each Ky In ComboBoxList
   'On Error Resume Next
     With ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2
       'If nothing is selected in HailDropDown, do nothing and exit this sub.
       If HailDropDown = "" Then
         Exit Sub
       'If a hail option is selected, execute the following code.
       Else
         .TextRange.Text = .TextRange.Text & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & dict2.Item(Ky)(0)
       End If
     End With
   Next

   Set dict2 = Nothing

End Sub

您只引用了.TextRange,而不是.TextRange.Text

另外,因为您需要在最后添加文本,所以只需要一个Else条件,而不是两个ElseIf,它们都做同样的事情! ; 0)

更多示例代码... https://msdn.microsoft.com/en-us/library/office/ff822136.aspx