Creating Macro for Adding Custom Table

L

Lisa

Hi,

I've created a custom button that runs a macro that adds a table into my
document and formats it using a specific style. Works great. Only issue is if
I select an existing table and run the macro it breaks (I get run time error
6028 the range cannot be deleted). I'm pretty new to this so I'm not sure how
to fix the code so that if someone tries to run it on an existing table they
have selected it won't crash and instead just applies the style BodyTable.
The code as it stands is:

Sub TableInsert()

ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2,
NumColumns:= _
2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
With Selection.Tables(1)
If .Style <> "BodyTable" Then
.Style = "BodyTable"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
Selection.Style = ActiveDocument.Styles("BodyTable")
End Sub

Any suggests would be greatly appreciated

Thanks
Lisa
 
J

Jean-Guy Marcil

Lisa was telling us:
Lisa nous racontait que :
Hi,

I've created a custom button that runs a macro that adds a table into
my document and formats it using a specific style. Works great. Only
issue is if I select an existing table and run the macro it breaks (I
get run time error 6028 the range cannot be deleted). I'm pretty new
to this so I'm not sure how to fix the code so that if someone tries
to run it on an existing table they have selected it won't crash and
instead just applies the style BodyTable. The code as it stands is:

Sub TableInsert()

ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2,
NumColumns:= _
2, DefaultTableBehavior:=wdWord9TableBehavior,
AutoFitBehavior:= _ wdAutoFitFixed
With Selection.Tables(1)
If .Style <> "BodyTable" Then
.Style = "BodyTable"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
Selection.Style = ActiveDocument.Styles("BodyTable")
End Sub

Any suggests would be greatly appreciated

Thanks
Lisa

You could have both macros in one, i.e., if the current cursor location is
not in a table, create and format a table, otherwise just format the table:

'_______________________________________
Sub TableFormatInsert()

Dim tblFormat As Table

If Not Selection.Information(wdWithInTable) Then
Set tblFormat = ActiveDocument.Tables.Add(Range:=Selection.Range, _
NumRows:=2, NumColumns:=2,
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed)
Else
Set tblFormat = Selection.Tables(1)
End If
With tblFormat
If .Range.Style <> "BodyTable" Then
.Range.Style = "BodyTable"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
End Sub
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top