Dave T
03-22-2012, 08:00 PM
Hello All,
I have been trying to get a macro by John McGhie to work.
His macro creates a three column table along with the following styles:
Table Body Text
Table Heading
Table Bullet
Table NumberAll of the macro work and creates the styles however the Table Bullet style does not have any bullets nor doe the Table Number style have any numbers.
Can anyone please explain why the following code does not work for me ???
Any comments appreciated.
Regards,
Dave T
Forgot to mention I am using Word 2003.
Option Explicit
Sub Main()
'http://www.mackb.com/Uwe/Forum.aspx/word/6159/Macro-for-table-problems
' Insert or Format Table Macro
' Macro recorded 14/03/00 by John McGhie
'
' On Error GoTo Error
Dim astyle As Style
Dim aDoc As String
Dim aTemplate As String
Dim x As Integer
Dim TabBodyText As Style
Dim TabHeading As Style
Dim TabBullet As Style
Dim tabNumber As Style
aTemplate = ActiveDocument.AttachedTemplate.FullName
aDoc = ActiveDocument.FullName
For Each astyle In ActiveDocument.Styles
If UCase(astyle.NameLocal) = UCase("Table Body Text") Then Set TabBodyText = astyle
If UCase(astyle.NameLocal) = UCase("Table Bullet") Then Set TabBullet = astyle
If UCase(astyle.NameLocal) = UCase("Table Heading") Then Set TabHeading = astyle
If UCase(astyle.NameLocal) = UCase("Table Number") Then Set tabNumber = astyle
Next astyle
If TabBodyText Is Nothing Then
ActiveDocument.Styles.Add Name:="Table Body Text"
Set TabBodyText = ActiveDocument.Styles("Table Body Text")
With TabBodyText
.AutomaticallyUpdate = False
.BaseStyle = "Body Text"
.NextParagraphStyle = "Table Body Text"
End With
With TabBodyText.Font
'.Name = BodyFont
.Name = "Arial"
.Size = 10
.Bold = False
.Italic = False
End With
With TabBodyText.ParagraphFormat
.LeftIndent = 0
.RightIndent = 0
.SpaceBefore = 2
.SpaceBeforeAuto = False
.SpaceAfter = 2
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
.Alignment = wdAlignParagraphLeft
.WidowControl = False
.KeepWithNext = False
.KeepTogether = True
.PageBreakBefore = False
.FirstLineIndent = 0
.OutlineLevel = wdOutlineLevelBodyText
End With
End If
If TabHeading Is Nothing Then
ActiveDocument.Styles.Add Name:="Table Heading"
Set TabHeading = ActiveDocument.Styles("Table Heading")
With TabHeading
.AutomaticallyUpdate = False
.BaseStyle = "Table Body Text"
.NextParagraphStyle = "Table Heading"
End With
With TabHeading.Font
'.Name = HeadingFont
.Name = "Arial"
.Size = 10
.Bold = True
.Italic = False
End With
With TabHeading.ParagraphFormat
.LeftIndent = 0
.RightIndent = 0
.SpaceBefore = 2
.SpaceBeforeAuto = False
.SpaceAfter = 2
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
.Alignment = wdAlignParagraphLeft
.WidowControl = False
.KeepWithNext = True
.KeepTogether = True
.PageBreakBefore = False
.FirstLineIndent = 0
.OutlineLevel = wdOutlineLevelBodyText
End With
End If
If TabBullet Is Nothing Then
ActiveDocument.Styles.Add Name:="Table Bullet"
Set TabBullet = ActiveDocument.Styles("Table Bullet")
With TabBullet
.AutomaticallyUpdate = False
.BaseStyle = "Table Body Text"
.NextParagraphStyle = "Table Bullet"
End With
With TabBullet.Font
'.Name = BodyFont
.Name = "Arial"
.Size = 10
.Bold = False
.Italic = False
End With
With TabBullet.ParagraphFormat
.LeftIndent = 22
.RightIndent = 0
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 5
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
.Alignment = wdAlignParagraphLeft
.WidowControl = True
.KeepWithNext = False
.KeepTogether = False
.PageBreakBefore = False
.FirstLineIndent = -17
.OutlineLevel = wdOutlineLevelBodyText
End With
TabBullet.ParagraphFormat.TabStops.ClearAll
TabBullet.ParagraphFormat.TabStops.Add Position:=22, _
Alignment:=wdAlignTabLeft, _
Leader:=wdTabLeaderSpaces
With ListGalleries(wdBulletGallery).ListTemplates(7).ListLevels(1)
.NumberFormat = ChrW(61623)
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleBullet
.NumberPosition = 6
.Alignment = wdListLevelAlignLeft
.TextPosition = 22
.TabPosition = 22
.ResetOnHigher = 0
.StartAt = 1
.Font.Name = "Symbol"
.LinkedStyle = "Table Bullet"
End With
End If
If tabNumber Is Nothing Then
ActiveDocument.Styles.Add Name:="Table Number"
Set tabNumber = ActiveDocument.Styles("Table Number")
With tabNumber
.AutomaticallyUpdate = False
.BaseStyle = "Table Bullet"
.NextParagraphStyle = "Table Number"
End With
With tabNumber.Font
'.Name = BodyFont
.Name = "Arial"
.Size = 10
.Bold = False
.Italic = False
End With
With tabNumber.ParagraphFormat
.LeftIndent = 22
.RightIndent = 0
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 2
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
.Alignment = wdAlignParagraphLeft
.WidowControl = True
.KeepWithNext = False
.KeepTogether = False
.PageBreakBefore = False
.FirstLineIndent = -17
.OutlineLevel = wdOutlineLevelBodyText
End With
tabNumber.ParagraphFormat.TabStops.ClearAll
tabNumber.ParagraphFormat.TabStops.Add Position:=22, _
Alignment:=wdAlignTabLeft, _
Leader:=wdTabLeaderSpaces
With ListGalleries(wdNumberGallery).ListTemplates(7).ListLevels(1)
.NumberFormat = "%1."
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleArabic
.NumberPosition = 6
.Alignment = wdListLevelAlignLeft
.TextPosition = 22
.TabPosition = 22
.ResetOnHigher = 0
.StartAt = 1
.LinkedStyle = "Table Number"
End With
End If
If Not Selection.Information(wdWithInTable) Then
ActiveDocument.Tables.Add Range:=Selection.Range, _
NumRows:=5, _
NumColumns:=3, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed
End If
Dim aTable As Table
Set aTable = Selection.Tables(1)
aTable.Select
aTable.AutoFormat Format:=wdTableFormatGrid5, _
ApplyBorders:=True, _
ApplyShading:=True, _
ApplyFont:=False, _
ApplyColor:=False, _
ApplyHeadingRows:=True, _
ApplyLastRow:=False, _
ApplyFirstColumn:=False, _
ApplyLastColumn:=False, AutoFit:=True
aTable.Select
With Selection
.Rows.AllowBreakAcrossPages = False
.Paragraphs.Reset
.Font.Reset
.Style = ActiveDocument.Styles("Table Body Text")
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth075pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth075pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth075pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth075pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderHorizontal)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth025pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderVertical)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth025pt
.Color = wdColorAutomatic
End With
.Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
.Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
.Borders.Shadow = False
End With
With Options
.DefaultBorderLineStyle = wdLineStyleSingle
.DefaultBorderLineWidth = wdLineWidth025pt
.DefaultBorderColor = wdColorAutomatic
End With
aTable.Rows(1).Select
With Selection
.Style = ActiveDocument.Styles("Table Heading")
.Rows(1).HeadingFormat = True
With .Shading
.Texture = wdTexture10Percent
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorAutomatic
End With
End With
aTable.AutoFitBehavior (wdAutoFitWindow)
End
Error:
MsgBox "The Template has been damaged. See Tech Support.", vbCritical
End Sub
I have been trying to get a macro by John McGhie to work.
His macro creates a three column table along with the following styles:
Table Body Text
Table Heading
Table Bullet
Table NumberAll of the macro work and creates the styles however the Table Bullet style does not have any bullets nor doe the Table Number style have any numbers.
Can anyone please explain why the following code does not work for me ???
Any comments appreciated.
Regards,
Dave T
Forgot to mention I am using Word 2003.
Option Explicit
Sub Main()
'http://www.mackb.com/Uwe/Forum.aspx/word/6159/Macro-for-table-problems
' Insert or Format Table Macro
' Macro recorded 14/03/00 by John McGhie
'
' On Error GoTo Error
Dim astyle As Style
Dim aDoc As String
Dim aTemplate As String
Dim x As Integer
Dim TabBodyText As Style
Dim TabHeading As Style
Dim TabBullet As Style
Dim tabNumber As Style
aTemplate = ActiveDocument.AttachedTemplate.FullName
aDoc = ActiveDocument.FullName
For Each astyle In ActiveDocument.Styles
If UCase(astyle.NameLocal) = UCase("Table Body Text") Then Set TabBodyText = astyle
If UCase(astyle.NameLocal) = UCase("Table Bullet") Then Set TabBullet = astyle
If UCase(astyle.NameLocal) = UCase("Table Heading") Then Set TabHeading = astyle
If UCase(astyle.NameLocal) = UCase("Table Number") Then Set tabNumber = astyle
Next astyle
If TabBodyText Is Nothing Then
ActiveDocument.Styles.Add Name:="Table Body Text"
Set TabBodyText = ActiveDocument.Styles("Table Body Text")
With TabBodyText
.AutomaticallyUpdate = False
.BaseStyle = "Body Text"
.NextParagraphStyle = "Table Body Text"
End With
With TabBodyText.Font
'.Name = BodyFont
.Name = "Arial"
.Size = 10
.Bold = False
.Italic = False
End With
With TabBodyText.ParagraphFormat
.LeftIndent = 0
.RightIndent = 0
.SpaceBefore = 2
.SpaceBeforeAuto = False
.SpaceAfter = 2
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
.Alignment = wdAlignParagraphLeft
.WidowControl = False
.KeepWithNext = False
.KeepTogether = True
.PageBreakBefore = False
.FirstLineIndent = 0
.OutlineLevel = wdOutlineLevelBodyText
End With
End If
If TabHeading Is Nothing Then
ActiveDocument.Styles.Add Name:="Table Heading"
Set TabHeading = ActiveDocument.Styles("Table Heading")
With TabHeading
.AutomaticallyUpdate = False
.BaseStyle = "Table Body Text"
.NextParagraphStyle = "Table Heading"
End With
With TabHeading.Font
'.Name = HeadingFont
.Name = "Arial"
.Size = 10
.Bold = True
.Italic = False
End With
With TabHeading.ParagraphFormat
.LeftIndent = 0
.RightIndent = 0
.SpaceBefore = 2
.SpaceBeforeAuto = False
.SpaceAfter = 2
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
.Alignment = wdAlignParagraphLeft
.WidowControl = False
.KeepWithNext = True
.KeepTogether = True
.PageBreakBefore = False
.FirstLineIndent = 0
.OutlineLevel = wdOutlineLevelBodyText
End With
End If
If TabBullet Is Nothing Then
ActiveDocument.Styles.Add Name:="Table Bullet"
Set TabBullet = ActiveDocument.Styles("Table Bullet")
With TabBullet
.AutomaticallyUpdate = False
.BaseStyle = "Table Body Text"
.NextParagraphStyle = "Table Bullet"
End With
With TabBullet.Font
'.Name = BodyFont
.Name = "Arial"
.Size = 10
.Bold = False
.Italic = False
End With
With TabBullet.ParagraphFormat
.LeftIndent = 22
.RightIndent = 0
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 5
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
.Alignment = wdAlignParagraphLeft
.WidowControl = True
.KeepWithNext = False
.KeepTogether = False
.PageBreakBefore = False
.FirstLineIndent = -17
.OutlineLevel = wdOutlineLevelBodyText
End With
TabBullet.ParagraphFormat.TabStops.ClearAll
TabBullet.ParagraphFormat.TabStops.Add Position:=22, _
Alignment:=wdAlignTabLeft, _
Leader:=wdTabLeaderSpaces
With ListGalleries(wdBulletGallery).ListTemplates(7).ListLevels(1)
.NumberFormat = ChrW(61623)
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleBullet
.NumberPosition = 6
.Alignment = wdListLevelAlignLeft
.TextPosition = 22
.TabPosition = 22
.ResetOnHigher = 0
.StartAt = 1
.Font.Name = "Symbol"
.LinkedStyle = "Table Bullet"
End With
End If
If tabNumber Is Nothing Then
ActiveDocument.Styles.Add Name:="Table Number"
Set tabNumber = ActiveDocument.Styles("Table Number")
With tabNumber
.AutomaticallyUpdate = False
.BaseStyle = "Table Bullet"
.NextParagraphStyle = "Table Number"
End With
With tabNumber.Font
'.Name = BodyFont
.Name = "Arial"
.Size = 10
.Bold = False
.Italic = False
End With
With tabNumber.ParagraphFormat
.LeftIndent = 22
.RightIndent = 0
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 2
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
.Alignment = wdAlignParagraphLeft
.WidowControl = True
.KeepWithNext = False
.KeepTogether = False
.PageBreakBefore = False
.FirstLineIndent = -17
.OutlineLevel = wdOutlineLevelBodyText
End With
tabNumber.ParagraphFormat.TabStops.ClearAll
tabNumber.ParagraphFormat.TabStops.Add Position:=22, _
Alignment:=wdAlignTabLeft, _
Leader:=wdTabLeaderSpaces
With ListGalleries(wdNumberGallery).ListTemplates(7).ListLevels(1)
.NumberFormat = "%1."
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleArabic
.NumberPosition = 6
.Alignment = wdListLevelAlignLeft
.TextPosition = 22
.TabPosition = 22
.ResetOnHigher = 0
.StartAt = 1
.LinkedStyle = "Table Number"
End With
End If
If Not Selection.Information(wdWithInTable) Then
ActiveDocument.Tables.Add Range:=Selection.Range, _
NumRows:=5, _
NumColumns:=3, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed
End If
Dim aTable As Table
Set aTable = Selection.Tables(1)
aTable.Select
aTable.AutoFormat Format:=wdTableFormatGrid5, _
ApplyBorders:=True, _
ApplyShading:=True, _
ApplyFont:=False, _
ApplyColor:=False, _
ApplyHeadingRows:=True, _
ApplyLastRow:=False, _
ApplyFirstColumn:=False, _
ApplyLastColumn:=False, AutoFit:=True
aTable.Select
With Selection
.Rows.AllowBreakAcrossPages = False
.Paragraphs.Reset
.Font.Reset
.Style = ActiveDocument.Styles("Table Body Text")
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth075pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth075pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth075pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth075pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderHorizontal)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth025pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderVertical)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth025pt
.Color = wdColorAutomatic
End With
.Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
.Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
.Borders.Shadow = False
End With
With Options
.DefaultBorderLineStyle = wdLineStyleSingle
.DefaultBorderLineWidth = wdLineWidth025pt
.DefaultBorderColor = wdColorAutomatic
End With
aTable.Rows(1).Select
With Selection
.Style = ActiveDocument.Styles("Table Heading")
.Rows(1).HeadingFormat = True
With .Shading
.Texture = wdTexture10Percent
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorAutomatic
End With
End With
aTable.AutoFitBehavior (wdAutoFitWindow)
End
Error:
MsgBox "The Template has been damaged. See Tech Support.", vbCritical
End Sub