L
LEU
I have the following macro that works just fine, but I was wondering if there
is a more efficient way to write it?
Sub Pound()
Dim idx As Long, num As Long
Dim Pnd As Shape
Dim textbox As Shape
Dim aVar As Variant
On Error GoTo endthis
For Each aVar In ActiveDocument.Variables
If aVar.Name = "idx" Then
num = aVar.Index
Exit For
End If
Next aVar
If num = 0 Then
ActiveDocument.Variables.Add Name:="idx", Value:=0
End If
idx = ActiveDocument.Variables("idx").Value + 1
Select Case Selection.Style
Case ActiveDocument.Styles(wdStyleHeading1)
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 25, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles(wdStyleHeading2)
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 25, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles(wdStyleHeading3)
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 60, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles(wdStyleHeading4)
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal,
105, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles(wdStyleHeading5)
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal,
133, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles(wdStyleHeading6)
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal,
155, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles(wdStyleHeading7)
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal,
177, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles("B1")
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 25, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles("B2")
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 25, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles("B3")
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 60, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles("B4")
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal,
105, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles("B5")
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal,
133, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles("B6")
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal,
155, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles("B7")
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal,
177, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
endthis:
End Select
End Sub
is a more efficient way to write it?
Sub Pound()
Dim idx As Long, num As Long
Dim Pnd As Shape
Dim textbox As Shape
Dim aVar As Variant
On Error GoTo endthis
For Each aVar In ActiveDocument.Variables
If aVar.Name = "idx" Then
num = aVar.Index
Exit For
End If
Next aVar
If num = 0 Then
ActiveDocument.Variables.Add Name:="idx", Value:=0
End If
idx = ActiveDocument.Variables("idx").Value + 1
Select Case Selection.Style
Case ActiveDocument.Styles(wdStyleHeading1)
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 25, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles(wdStyleHeading2)
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 25, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles(wdStyleHeading3)
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 60, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles(wdStyleHeading4)
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal,
105, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles(wdStyleHeading5)
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal,
133, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles(wdStyleHeading6)
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal,
155, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles(wdStyleHeading7)
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal,
177, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles("B1")
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 25, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles("B2")
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 25, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles("B3")
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 60, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles("B4")
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal,
105, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles("B5")
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal,
133, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles("B6")
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal,
155, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
Case ActiveDocument.Styles("B7")
Set Pnd = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal,
177, _
Selection.Information(wdVerticalPositionRelativeToPage) - 4, 22, 24)
With Pnd.TextFrame.TextRange
.Text = "#"
.Font.Size = 12
End With
Pnd.Fill.Visible = msoFalse
Pnd.Line.Visible = msoFalse
Pnd.Name = "Pnd" & idx
ActiveDocument.Variables("idx").Value = idx
endthis:
End Select
End Sub