以下代码不需要懂,直接复制即可

文本动作

拆分多段

  1. '拆分合并-拆成多段
  2. Sub bt_TextSplitParagraph()
  3. On Error GoTo hexit 'hexit:
  4. Dim oSld As Slide
  5. Dim oShp As Shape
  6. Dim oRng As TextRange
  7. Dim oCount As Long
  8. Dim i As Integer
  9. If ActiveWindow.Selection.Type = ppSelectionNone Then Exit Sub
  10. If ActiveWindow.Selection.Type = ppSelectionSlides Then Exit Sub
  11. Set oSld = ActiveWindow.Selection.SlideRange(1)
  12. ' add tag
  13. For Each oShp In ActiveWindow.Selection.ShapeRange
  14. If oShp.HasTextFrame Then
  15. oShp.Tags.add "select", "y"
  16. End If
  17. Next
  18. For Each oShp In oSld.Shapes
  19. If oShp.Tags("select") = "y" Then
  20. For oCount = 1 To oShp.TextFrame.TextRange.Paragraphs.Count
  21. Set oRng = oShp.TextFrame.TextRange.Paragraphs(oCount)
  22. If oRng.Text = "" Or oRng.Text = Chr(13) Or oRng.Text = Chr(10) Then
  23. Else
  24. With oShp.Duplicate
  25. If .Tags("select") = "y" Then .Tags.Delete ("select")
  26. .top = oRng.BoundTop - .TextFrame.MarginTop
  27. .left = oShp.left
  28. .TextFrame.TextRange.Text = oRng.TrimText
  29. .Tags.add "line", "y"
  30. End With
  31. End If
  32. Next
  33. End If
  34. Next
  35. For i = oSld.Shapes.Count To 1 Step -1
  36. With oSld.Shapes(i)
  37. If .Tags("select") = "y" Then
  38. .Tags.Delete ("select")
  39. .Delete
  40. End If
  41. End With
  42. Next
  43. For i = oSld.Shapes.Count To 1 Step -1
  44. With oSld.Shapes(i)
  45. If .Tags("line") = "y" Then
  46. .Select msoFalse
  47. .Tags.Delete ("line")
  48. End If
  49. End With
  50. Next
  51. Exit Sub
  52. hexit:
  53. End Sub

拆成多行

  1. '拆分合并-拆成多行
  2. Sub bt_TextSplitLines()
  3. On Error GoTo hexit 'hexit:
  4. Dim oSld As Slide
  5. Dim oShp As Shape
  6. Dim oRng As TextRange
  7. Dim oCount As Long
  8. Dim i As Integer
  9. If ActiveWindow.Selection.Type = ppSelectionNone Then Exit Sub
  10. If ActiveWindow.Selection.Type = ppSelectionSlides Then Exit Sub
  11. Set oSld = ActiveWindow.Selection.SlideRange(1)
  12. ' add tag
  13. For Each oShp In ActiveWindow.Selection.ShapeRange
  14. If oShp.HasTextFrame Then
  15. oShp.Tags.add "select", "y"
  16. End If
  17. Next
  18. For Each oShp In oSld.Shapes
  19. If oShp.Tags("select") = "y" Then
  20. For oCount = 1 To oShp.TextFrame.TextRange.Lines.Count
  21. Set oRng = oShp.TextFrame.TextRange.Lines(oCount)
  22. If oRng.Text = "" Or oRng.Text = Chr(13) Then
  23. Else
  24. With oShp.Duplicate
  25. If .Tags("select") = "y" Then .Tags.Delete ("select")
  26. .top = oRng.BoundTop - .TextFrame.MarginTop
  27. .left = oShp.left
  28. .TextFrame.TextRange.Text = oRng.TrimText
  29. .Tags.add "line", "y"
  30. End With
  31. End If
  32. Next
  33. End If
  34. Next
  35. For i = oSld.Shapes.Count To 1 Step -1
  36. With oSld.Shapes(i)
  37. If .Tags("select") = "y" Then
  38. .Tags.Delete ("select")
  39. .Delete
  40. End If
  41. End With
  42. Next
  43. For i = oSld.Shapes.Count To 1 Step -1
  44. With oSld.Shapes(i)
  45. If .Tags("line") = "y" Then
  46. .Select msoFalse
  47. .Tags.Delete ("line")
  48. End If
  49. End With
  50. Next
  51. Exit Sub
  52. hexit:
  53. End Sub

合并文字

  1. '拆分合并-合并文字
  2. Sub bt_TextMerge()
  3. On Error GoTo hexit 'hexit:
  4. ' This will merge the text from all selected text boxes into the
  5. ' first selected box then delete the other text boxes
  6. Dim oRng As ShapeRange
  7. Dim oFirstShape As Shape
  8. Dim oSh As Shape
  9. Dim X As Long
  10. Dim owidth As Long
  11. Set oRng = ActiveWindow.Selection.ShapeRange
  12. If oRng.Count <= 1 Then Exit Sub
  13. Set oFirstShape = oRng(1)
  14. owidth = ArrGroupSize()(3)
  15. oFirstShape.Width = owidth
  16. For X = 2 To oRng.Count
  17. oFirstShape.TextFrame.TextRange.Text = _
  18. oFirstShape.TextFrame.TextRange.Text _
  19. & oRng(X).TextFrame.TextRange.Text
  20. If X < oRng.Count Then
  21. oFirstShape.TextFrame.TextRange.Text = _
  22. oFirstShape.TextFrame.TextRange.Text
  23. End If
  24. Next
  25. For X = oRng.Count To 2 Step -1
  26. oRng(X).Delete
  27. Next
  28. Set oRng = Nothing
  29. Set oFirstShape = Nothing
  30. Set oSh = Nothing
  31. hexit:
  32. End Sub
  33. '选定组大小
  34. Function ArrGroupSize()
  35. On Error GoTo hexit
  36. Dim oSld As Slide
  37. Dim oShp As Shape
  38. Dim oCount As Integer
  39. Dim x As Integer
  40. Dim L_left As Double
  41. Dim L_right As Double
  42. Dim L_top As Double
  43. Dim L_bottom As Double
  44. Dim pWidth As Double
  45. Dim pheight As Double
  46. Dim oleft As Double
  47. Dim oTop As Double
  48. Dim owidth As Double
  49. Dim oheight As Double
  50. Dim Offset_x As Double
  51. Dim Offset_y As Double
  52. Dim oSize(5) As Double
  53. If ActiveWindow.Selection.Type <> ppSelectionShapes Then
  54. ArrGroupSize = oSize()
  55. Exit Function
  56. End If
  57. ' 幻灯片大小
  58. With ActivePresentation.PageSetup
  59. pWidth = .SlideWidth
  60. pheight = .SlideHeight
  61. End With
  62. Set oSld = ActiveWindow.Selection.SlideRange(1)
  63. oCount = ActiveWindow.Selection.ShapeRange.Count
  64. ' 单个元素
  65. If oCount = 1 Then
  66. Exit Function
  67. End If
  68. With ActiveWindow.Selection.ShapeRange(1)
  69. L_left = .left
  70. L_right = .left + .Width
  71. L_top = .top
  72. L_bottom = .top + .Height
  73. End With
  74. For x = 2 To oCount
  75. With ActiveWindow.Selection.ShapeRange(x)
  76. If .left < L_left Then L_left = .left
  77. If .left + .Width > L_right Then L_right = .left + .Width
  78. If .top < L_top Then L_top = .top
  79. If .top + .Height > L_bottom Then L_bottom = .top + .Height
  80. End With
  81. Next
  82. ' Debug.Print "左: " & ConvertPointToCm(L_left) & " 厘米"
  83. ' Debug.Print "右: " & ConvertPointToCm(L_right) & " 厘米"
  84. ' Debug.Print "上: " & ConvertPointToCm(L_top) & " 厘米"
  85. ' Debug.Print "下: " & ConvertPointToCm(L_bottom) & " 厘米"
  86. ' 整体大小
  87. oleft = L_left
  88. oTop = L_top
  89. owidth = L_right - L_left
  90. oheight = L_bottom - L_top
  91. oSize(0) = 1
  92. oSize(1) = oleft
  93. oSize(2) = oTop
  94. oSize(3) = owidth
  95. oSize(4) = oheight
  96. ArrGroupSize = oSize()
  97. ' Debug.Print "整体-宽" & ConvertPointToCm(oWidth) & " 厘米"
  98. ' Debug.Print "整体-高" & ConvertPointToCm(oHeight) & " 厘米"
  99. hexit:
  100. End Function

拆分字符

  1. '拆分合并-拆成单字
  2. Sub bt_TextSplitCharacter()
  3. On Error GoTo hexit 'hexit:
  4. Dim oSld As Slide
  5. Dim oShp As Shape
  6. Dim oRng As TextRange
  7. Dim oCount As Long
  8. Dim i As Long
  9. If ActiveWindow.Selection.Type = ppSelectionNone Then Exit Sub
  10. If ActiveWindow.Selection.Type = ppSelectionSlides Then Exit Sub
  11. Set oSld = ActiveWindow.Selection.SlideRange(1)
  12. Set oShp = ActiveWindow.Selection.ShapeRange(1)
  13. With oShp
  14. If .HasTextFrame Then
  15. If Len(.TextFrame.TextRange.Text) = 0 Then Exit Sub
  16. For oCount = 1 To .TextFrame.TextRange.Characters.Count
  17. Set oRng = .TextFrame.TextRange.Characters(oCount)
  18. With oShp.Duplicate
  19. .top = oRng.BoundTop - .TextFrame.MarginTop
  20. .left = oRng.BoundLeft - .TextFrame.MarginLeft
  21. .Width = oRng.BoundWidth
  22. .Width = oRng.BoundHeight
  23. .TextFrame.TextRange.Text = oRng.TrimText
  24. .TextFrame.TextRange.Font.Color.RGB = oRng.Font.Color
  25. .TextFrame.TextRange.Font.Bold = oRng.Font.Bold
  26. .Tags.add "Split", "y"
  27. End With
  28. Next
  29. End If
  30. End With
  31. oShp.Visible = msoFalse
  32. For i = oSld.Shapes.Count To 1 Step -1
  33. With oSld.Shapes(i)
  34. If .Tags("Split") = "y" Then
  35. .Select msoFalse
  36. .Tags.Delete ("Split")
  37. End If
  38. End With
  39. Next
  40. hexit:
  41. End Sub

动画组合添加形状

  1. '动画组合添加形状
  2. Sub bt_GroupAddShape()
  3. On Error GoTo hexit 'hexit:
  4. Dim oSld As Slide
  5. Dim oShp As Shape
  6. Dim oShp2 As Shape
  7. Dim hasFlash As Boolean
  8. Dim oName As String
  9. Dim i As Integer
  10. Set oSld = ActiveWindow.Selection.SlideRange(1)
  11. Set oShp = ActiveWindow.Selection.ShapeRange(1)
  12. Set oShp2 = ActiveWindow.Selection.ShapeRange(2)
  13. If oShp.Type = msoGroup Then
  14. If hasAnimation(oSld, oShp) Then
  15. oShp.PickupAnimation
  16. hasFlash = True
  17. End If
  18. oName = oShp.Name
  19. CommandBars.ExecuteMso ("ObjectsUngroup")
  20. oShp2.Select msoFalse
  21. CommandBars.ExecuteMso ("ObjectsGroup")
  22. With ActiveWindow.Selection.ShapeRange(1)
  23. .Name = oName
  24. If hasFlash = True Then
  25. .ApplyAnimation
  26. End If
  27. End With
  28. End If
  29. hexit:
  30. End Sub

分离形状文本

  1. '形状-分离形状文本
  2. Sub bt_ShapeSeparateText()
  3. On Error GoTo hexit 'hexit:
  4. Dim oShp As Shape
  5. For Each oShp In ActiveWindow.Selection.ShapeRange
  6. If oShp.HasTextFrame Then
  7. If oShp.TextFrame.TextRange.Text <> "" Then
  8. With oShp.Duplicate
  9. .Fill.Visible = msoFalse
  10. .Line.Visible = msoFalse
  11. .left = oShp.left
  12. .top = oShp.top
  13. End With
  14. oShp.TextFrame.TextRange.Text = ""
  15. End If
  16. End If
  17. Next
  18. hexit:
  19. End Sub

更多动作

VBA笔记