使用VBA将Powerpoint中多行中相同高度的形状分组

时间:2017-10-05 12:50:24

标签: vba powerpoint powerpoint-vba

我想在PPT中创建一个VBA宏,使用VBA将Powerpoint中多行的相同高度的形状分组。我的第一步将是这样的形象: Group Textboxes row wise

有许多行和列的文本框矩阵,它们在垂直方向上均匀分布。水平。我想完全选择所有形状并运行一个宏来将文本框划分为多行。下面的代码是复制的,而不是最终的,感谢任何帮助,这个片段,非常感谢。

Sub GroupSameHeightObjects()

  ' Dimension the variables.
  Dim shapeObject As shape
  Dim lSlideNumber As Long
  Dim strPrompt, strTitle As String
  Dim ShapeList() As String
  Dim count As Long

  ' Initialize the counter.
  count = 0

  ' Make sure PowerPoint is in slide view.
  If ActiveWindow.ViewType <> ppViewSlide Then

     ' Set up the error message.
     strPrompt = "You must be in slide view to run this macro." _
        & " Change to slide view and run the macro again."
     strTitle = "Not In Slide View"

     ' Display the error message.
     MsgBox strPrompt, vbExclamation, strTitle

     ' Stop the macro.
     End

  End If

  ' Get the current slide number.
  lSlideNumber = ActiveWindow.Selection.SlideRange.SlideNumber

  ' Loop through the shapes on the slide.
  For Each shapeObject In _
     ActivePresentation.Slides(lSlideNumber).Shapes

     ' See whether shape is a placeholder.
     If shapeObject.Type <> msoPlaceholder Then

        ' Increment count if the shape is not a placeholder.
        count = count + 1

        ' Get the name of the shape and store it in the ShapeList
        ' array.
        ReDim Preserve ShapeList(1 To count)
        ShapeList(count) = shapeObject.Name

     End If

  Next shapeObject

  ' If more than 1 object (excluding a placeholder object) is found,
  ' group the objects.
  If count > 1 Then
     With ActivePresentation.Slides(lSlideNumber).Shapes

        ' Group the shapes together.
        .Range(ShapeList()).Group.Select
     End With
  Else

     Select Case count

        ' One shape found.
        Case 1

           ' Set up the message.
           strPrompt = "Only one shape found." _
              & " You need at least two shapes to group."
           strTitle = "One Shape Available"

        ' Zero shapes found.
        Case 0

           ' Set up the message.
           strPrompt = "No shapes found. You need to have at " _
              & "least two shapes, excluding placeholders."
           strTitle = "No Shapes Available"

        ' An error occurred.
        Case Else

           ' Set up the message.
           strPrompt = "The macro found an error it could not correct."
           strTitle = "Error"

     End Select

     ' Display the message.
     MsgBox strPrompt, vbExclamation, strTitle

  End If

End Sub

2 个答案:

答案 0 :(得分:0)

有些事情可能无法完全满足您的需求,但这样可以为您节省一些麻烦:

   Sub GroupSameHeightObjects()

  ' Dimension the variables.
  Dim shapeObject As shape
  Dim lSlideNumber As Long

  ' This will dim strPrompt as a variant
  ' Dim strPrompt, strTitle As String
  Dim strPrompt as string, strTitle as string

  Dim ShapeList() As String
  Dim count As Long

  ' Initialize the counter.
  count = 0

  ' Make sure PowerPoint is in slide view.
  If ActiveWindow.ViewType <> ppViewSlide Then

     ' Set up the error message.
     strPrompt = "You must be in slide view to run this macro." _
        & " Change to slide view and run the macro again."
     strTitle = "Not In Slide View"

     ' Display the error message.
     MsgBox strPrompt, vbExclamation, strTitle

     ' Stop the macro.
     ' See previous comment
     'End
     Exit Sub

  End If

  ' Get the current slide number.
  ' Nope, you want the SlideIndex; SlideNumber gives you the number that'll
  ' appear when you use PPT's slide numbering features; if the user sets the 
  ' starting number to something other than 1, your code will break
  'lSlideNumber = ActiveWindow.Selection.SlideRange.SlideNumber
   lSlideNumber = ActiveWindow.Selection.SlideRange.SlideIndex

  ' Loop through the shapes on the slide.
  For Each shapeObject In _
     ActivePresentation.Slides(lSlideNumber).Shapes

     ' See whether shape is a placeholder.
     If shapeObject.Type <> msoPlaceholder Then

        ' Increment count if the shape is not a placeholder.
        count = count + 1

        ' Get the name of the shape and store it in the ShapeList
        ' array.
        ' I've learned not to trust shape names in PPT
        ' I'd dim ShapeList as an array of shapes and then
        ' Set ShapeList(count) = shapeObject
        ReDim Preserve ShapeList(1 To count)
        ShapeList(count) = shapeObject.Name

     End If

  Next shapeObject

' You could include this next bit in the following Case selector,
' Case > 1 ... etc.    
      ' If more than 1 object (excluding a placeholder object) is found,
      ' group the objects.
      If count > 1 Then
         With ActivePresentation.Slides(lSlideNumber).Shapes

        ' Group the shapes together.
        .Range(ShapeList()).Group.Select
     End With
  Else

     Select Case count

        ' One shape found.
        Case 1

           ' Set up the message.
           strPrompt = "Only one shape found." _
              & " You need at least two shapes to group."
           strTitle = "One Shape Available"

        ' Zero shapes found.
        Case 0

           ' Set up the message.
           strPrompt = "No shapes found. You need to have at " _
              & "least two shapes, excluding placeholders."
           strTitle = "No Shapes Available"

        ' An error occurred.
        Case Else

           ' Set up the message.
           strPrompt = "The macro found an error it could not correct."
           strTitle = "Error"

     End Select

     ' Display the message.
     MsgBox strPrompt, vbExclamation, strTitle

  End If

End Sub

答案 1 :(得分:0)

我现在没有时间编写/测试任何代码,但如果我必须这样做,我会从类似于我从其他项目获得的代码片段开始:

{__esModule: true, someFunction: ƒ}