PDA

View Full Version : VBA insert table doesn't run



lodi.j
10-04-2016, 01:55 AM
Hi,
I have a code for inserting a specific table (directly from the ribbon) in word 2007.
But it won't work. I always receive the run-time error 4198.
It is stuck on the sentences in bold. Can anybody help me with this? I'm quite new to this stuff.
Thank you!!


Sub Table(control As IRibbonControl)
Dim fld As FormField

Selection.InsertCaption Label:="Table", TitleAutoText:="InsertCaption1", _
Title:="", Position:=wdCaptionPositionBelow, ExcludeLabel:=0
Selection.TypeText Text:=vbTab
Set fld = Selection.FormFields.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput)
fld.Result = "Add title of the table"
Selection.TypeParagraph
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=5, NumColumns:= _
3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
With Selection.Tables(1)
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = False
End With
Selection.Tables(1).Select
Selection.Style = ActiveDocument.Styles("Table text")
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.SelectRow
Selection.Cells.Merge
With Selection.Cells
.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
.Borders(wdBorderRight).LineStyle = wdLineStyleNone
.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
End With
With Options
.DefaultBorderLineStyle = wdLineStyleSingle
.DefaultBorderLineWidth = wdLineWidth050pt
.DefaultBorderColor = wdColorAutomatic
End With
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.Style = ActiveDocument.Styles("Tablenote text")
Selection.TypeText Text:=" "
Selection.MoveUp Unit:=wdLine, Count:=3
Set fld = Selection.FormFields.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput)
fld.Result = "Add table text"
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.SelectRow
Selection.Style = ActiveDocument.Styles("Table heading")
Set fld = Selection.FormFields.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput)
fld.Result = "Add table heading"
End Sub
Sub InsertTablenote(control As IRibbonControl)
'
' InsertTablenote Macro
'
'
Selection.Style = ActiveDocument.Styles("Tablenote Reference")
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"SEQ TF", PreserveFormatting:=True
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Tablenote text")
Selection.Find.Execute
Selection.EndOf Unit:=wdCell
Selection.TypeParagraph
Selection.Font.Superscript = True
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"SEQ TN", PreserveFormatting:=True
Selection.Font.Superscript = False
Selection.TypeText Text:=" "
Selection.FormFields.Add Range:=Selection.Range, Type:= _
wdFieldFormTextInput
Selection.PreviousField.Select
With Selection.FormFields(1)
.Name = "Text5"
.EntryMacro = ""
.ExitMacro = ""
.Enabled = True
.OwnHelp = False
.HelpText = ""
.OwnStatus = False
.StatusText = ""
With .TextInput
.EditType Type:=wdRegularText, Default:= _
"Add table footnote text", Format:=""
.Width = 0
End With
End With
End Sub
Sub InsertTablenoteRestart(control As IRibbonControl)
'
' InsertTablenote Macro
'
'
Selection.Style = ActiveDocument.Styles("Tablenote Reference")
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"SEQ TF \r 1", PreserveFormatting:=True
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Tablenote text")
Selection.Find.Execute
Selection.EndOf Unit:=wdCell
Selection.TypeParagraph
Selection.Font.Superscript = True
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"SEQ TN \r 1", PreserveFormatting:=True
Selection.Font.Superscript = False
Selection.TypeText Text:=" "
Selection.FormFields.Add Range:=Selection.Range, Type:= _
wdFieldFormTextInput
Selection.PreviousField.Select
With Selection.FormFields(1)
.Name = "Text5"
.EntryMacro = ""
.ExitMacro = ""
.Enabled = True
.OwnHelp = False
.HelpText = ""
.OwnStatus = False
.StatusText = ""
With .TextInput
.EditType Type:=wdRegularText, Default:= _
"Add table footnote text", Format:=""
.Width = 0
End With
End With
End Sub

gmaxey
10-04-2016, 04:00 AM
Your code ran past that point with no error here. What happens if you change to:

Selection.InsertCaption Label:="Table", Title:="", Position:=wdCaptionPositionBelow, ExcludeLabel:=0

lodi.j
10-04-2016, 11:34 PM
Hi Greg,

Thanks but it still shows run-time error 4198 at that point. Which Office version do you work with?
Maybe that's the problem?

Lodi

gmayor
10-05-2016, 12:55 AM
No problems here either with Word 2010 and 2016 (though there are errors related to missing styles).

Does it work if you reboot the PC and start again?

Can you insert the caption manually from the ribbon? If not, repair Office and try it again.