PDA

View Full Version : Insert Table (Value out of range)



vaguirre
08-03-2007, 03:51 PM
Hello,

I'm using Windows XP/Word 2003.

I created a Table style type (Table IPC 01), recorded a macro to insert a 3 column table (Insert3ColTable) based on Table IPC 01, and made other formatting adjustments. It's working great.

I also need a macro to insert a 4 and 5 column table with the same formatting adjustments. So, I created a macro called Insert4ColTable, copied and pasted the code from Insert3ColTable, adjusted the number of cols, the .Columns.PreferredWidth, and counts. When it runs I get run-time error '4608': Value out of range. When I select debug it goes to bolded text below.

Sub Insert3ColTable()
'
' Insert3ColTable Macro
' Macro recorded 8/3/2007
'
Selection.Style = ActiveDocument.Styles("Table Text 09")
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=4, NumColumns:= _
3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
With Selection.Tables(1)
.Columns.PreferredWidth = InchesToPoints(1.91)
If .Style <> "Table IPC 01" Then
.Style = "Table IPC 01"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
Selection.MoveRight Unit:=wdCharacter, Count:=4, Extend:=wdExtend
Options.DefaultBorderLineWidth = wdLineWidth100pt
With Selection.Borders(wdBorderBottom)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
Selection.Style = ActiveDocument.Styles("Table Head 10")
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=4
Selection.Style = ActiveDocument.Styles("Body Text 0pt After")
End Sub

Sub Insert4ColTable()
'
' Insert4ColTable Macro
' Macro created 8/3/2007
'
Selection.Style = ActiveDocument.Styles("Table Text 09")
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=4, NumColumns:= _
4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
With Selection.Tables(1)
.Columns.PreferredWidth = InchesToPoints(1.43)
If .Style <> "Table IPC 01" Then
.Style = "Table IPC 01"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
Selection.MoveRight Unit:=wdCharacter, Count:=5, Extend:=wdExtend
With Selection.Borders(wdBorderBottom)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
Selection.Style = ActiveDocument.Styles("Table Head 10")
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=4
Selection.Style = ActiveDocument.Styles("Body Text 0pt After")
End Sub

Sub Record2ColTable()
'
' Record2ColTable Macro
' Macro recorded 8/3/2007
'
Selection.Style = ActiveDocument.Styles("Table Text 09")
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=4, NumColumns:= _
2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
With Selection.Tables(1)
.Columns.PreferredWidth = InchesToPoints(2.75)
If .Style <> "Table IPC 01" Then
.Style = "Table IPC 01"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
Selection.MoveRight Unit:=wdCharacter, Count:=3, Extend:=wdExtend
Options.DefaultBorderLineWidth = wdLineWidth100pt
With Selection.Borders(wdBorderBottom)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
Selection.Style = ActiveDocument.Styles("Table Head 10")
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=4
Selection.Style = ActiveDocument.Styles("Body Text 0pt After")
End Sub


So, I recorded another macro to insert 2 columns and it works. Why doesn't Insert4ColTable work? :dunno

Thanks bunches!
Vicki

fionabolt
08-06-2007, 12:38 AM
Works fine for me.

Question: where was your insertion point?
Question: are you sure that the document you had open had the style "Table Text 09"?

fumei
08-06-2007, 03:26 AM
A follow up to question 1 above: is your Selection in fact an insertion point?

Also, it is MUCH better to not use Selection at all.

vaguirre
08-06-2007, 08:38 AM
Initially, I had the cursor in various empty paragraphs thinking that might cause the error. The style "Table Text 09" is part of the document.

I tried running it again this morning with the cursor at the top of the document and I still get the same error.

In a nutshell, these tables are usually inserted as autotext entries via keyboard shortcuts. However, the clients (employees) forget to take the .dot and reattach the template when they work away from the office, etc.

So I thought I'd try creating the tables with macros, create an autonew macro, and copy the macros to the document so they'd always be available, etc. Since I'm fairly new to VBA I wanted to start with something simple.

What do I need to do to not use selection? I'm open to any suggestions.

Thanks,
Vicki