Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2007

Tip: Looking for answers? Try searching our database.

Creating Macro for Adding Custom Table

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lisa - 05 Oct 2007 14:25 GMT
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
Jean-Guy Marcil - 05 Oct 2007 15:20 GMT
Lisa was telling us:
Lisa nous racontait que :

> Hi,
>
[quoted text clipped - 28 lines]
> 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
'_______________________________________

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org


Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.