Reputation: 31
I would like to create a macro on powerpoint, when I highlight a portion of the text and run the macro. it will create a level1 round bullet. I intend to use this as the base to create level 2 (sub-bullets, nested within level 1), and level 3, but couldn't figure out what's wrong with my code. Is there any expert here able to provide me some direction please ?
What I want to achieve is something like this, where eventually I will have 3 button as addin, and level 1 to level 3 bullet style can be switched freely by clicking on the button.
Sub ApplyLBulletsToSelectedCode()
On Error Resume Next
Err.Clear
Dim oText As TextRange
Set oText = ActiveWindow.Selection.TextRange
If Err.Number <> 0 Then
MsgBox "No text was selected. Please highlight some text " _
& "or select a text frame and run the macro again.", vbExclamation
End
End If
With oText
.ParagraphFormat.Alignment = ppAlignLeft
.IndentLevel = 1
With .Parent.Ruler
.Levels(1).FirstMargin = 20
.Levels(1).LeftMargin = 0
End With
With .ParagraphFormat.Bullet
.Visible = msoCTrue
.RelativeSize = 1
.Character = 159
With .Font
.Color.RGB = RGB(0, 0, 0)
.Name = "Wingdings"
End With
End With
With .Font
.Name = "Calibri"
.Bold = msoFalse
.Color.RGB = RGB(0, 0, 0)
.Size = 14
End With
End With
End Sub
Upvotes: 3
Views: 5566
Reputation: 196
This is one the right track, but I'm seeing two major issues.
One is that you don't really have any handling for setting different indent levels -- it just assumes we're setting the indent level to 1. But if you want to set an indent level for 2, 3, 4, etc, then you don't want to have to write a whole new sub. So, it's best to have the sub accept a variable (my_level), and use that to set your indents and indent level, as seen below. Then you just point your three indent buttons to their own caller subs, which pass along the appropriate indent level.
Note that the first line indent is always -20 -- this value only needs to change if you want differing amounts of space between the bullet and the text in each level. The LeftIndent value is just a multiple of the my_level value.
The other issue I'm seeing is that you're using the Ruler object to set the indents. The problem is that doing it this way this affects the indent of all other text in the shape, including the text that's not selected. TextRange2 contains the .ParagraphFormat.FirstLineIndent and .ParagraphFormat.LeftIndent properties, which will keep your changes localized to the selected text. Some of the properties you're changing aren't available in TextRange2 however, so you need to set two different variables that are based on the selection, one for TextRange and one for TextRange2.
Note that you have to set the .FirstLineIndent and .LeftIndent properties after you set the indent level. If you were to move the oText2 portion before the oText portion in the code below, the indents would not be set properly.
This should point you in the right direction:
Sub CallLevel1()
ApplyLBulletsToSelectedCode (1)
End Sub
Sub CallLevel2()
ApplyLBulletsToSelectedCode (2)
End Sub
Sub CallLevel3()
ApplyLBulletsToSelectedCode (3)
End Sub
Sub ApplyLBulletsToSelectedCode(my_level As Long)
On Error Resume Next
Err.Clear
Dim oText As TextRange
Dim oText2 As TextRange2
Set oText = ActiveWindow.Selection.TextRange
Set oText2 = ActiveWindow.Selection.TextRange2
If Err.Number <> 0 Then
MsgBox "No text was selected. Please highlight some text " _
& "or select a text frame and run the macro again.", vbExclamation
End
End If
With oText
.Paragraphs.IndentLevel = my_level
With .ParagraphFormat.Bullet
.Visible = msoCTrue
.RelativeSize = 1
.Character = 159
With .Font
.Color.RGB = RGB(0, 0, 0)
.Name = "Wingdings"
End With
End With
With .Font
.Name = "Calibri"
.Bold = msoFalse
.Color.RGB = RGB(0, 0, 0)
.Size = 14
End With
End With
With oText2
.ParagraphFormat.Alignment = ppAlignLeft
.ParagraphFormat.FirstLineIndent = -20
.ParagraphFormat.LeftIndent = 20 * my_level
End With
End Sub
Upvotes: 1