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 / April 2006

Tip: Looking for answers? Try searching our database.

Using Macro to create table/rows

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
User - 11 Apr 2006 09:39 GMT
Is it possible to use macro and when the user press it, a new row will appear
(or a new table) will be created, wiht the same columns above?
Cindy M  -WordMVP- - 12 Apr 2006 14:56 GMT
Hi =?Utf-8?B?VXNlcg==?=,

> Is it possible to use macro and when the user press it, a new row will appear
> (or a new table) will be created, wiht the same columns above?

Your question isn't clear enough to be able to give you a more detailed answer
than: Probably, yes.

"With the same columns above"... Above what?

Insert a new row above the current one is no problem.

How should the macro know the dimensions of a new table?

Also, please tell us which version of Word you're targeting.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :-)
User - 12 Apr 2006 15:08 GMT
Word XP all our clients PC are using.

I need a macro when a user runs it, it will create an additional row at the
bottom of the current table (the i-blinking cursor will be inside the table
when they run the macro).

And another macro will be to create a table. We can specify the width and
heights of each columns and rows through VBA, right?

> Hi =?Utf-8?B?VXNlcg==?=,
>
[quoted text clipped - 19 lines]
> This reply is posted in the Newsgroup; please post any follow question or reply
> in the newsgroup and not by e-mail :-)
Charles Kenyon - 12 Apr 2006 16:40 GMT
Yes, this is possible. You would want your macro to first decide if it is in
a table. Then, if it is, create an additional row below. I use such a macro
in protected forms to unprotect the form, create a new row, add form fields
in the row, and then reprotect the form and select the first field in the
new row for user input.

If not in a table, then you would want it to create a table for you. I would
suggest that rather than coding the creation of the table, create the table
you want manually and then save that table as an AutoText entry. Have your
macro insert the AutoText. This makes it a lot easier to modify your table
settings in the future (or at least it would be easier for me).

The following macro is to add a row and insert appropriate AutoText entries:

Private Sub AddRow97(Optional lTable As Long = 2)
'
' AddRow97 Macro
' Macro recorded 11/24/03 by Charles Kyle Kenyon
'
   Dim oTemplate As Template
   Set oTemplate = Templates(ThisDocument.FullName)
   Dim sAutoTextEntry1 As String
   Dim sAutoTextEntry2 As String
   If lTable = 3 Then
       sAutoTextEntry1 = "zExpenseDescription"
       sAutoTextEntry2 = "zExpenseAmount"
   Else    '   Table 4 - Payments
       sAutoTextEntry1 = "zPaymentDescription"
       sAutoTextEntry2 = "zPaymentAmount"
   End If
'
   Dim lRowCount As Long
   Dim rRow As Range
   UnprotectDocumentMacro
'
   lRowCount = ActiveDocument.Range.Tables(lTable).Rows.Count
   ActiveDocument.Tables(lTable).Rows(lRowCount - 1).Select
   With Selection
       .Copy
       .Paste
       ActiveDocument.Tables(lTable).Rows(lRowCount).Select
       .Delete Unit:=wdCharacter, Count:=1
       .HomeKey Unit:=wdLine
       Application.DisplayAutoCompleteTips = True
       With AutoCorrect
           .CorrectInitialCaps = True
           .CorrectSentenceCaps = True
           .CorrectDays = True
           .CorrectCapsLock = True
           .ReplaceText = True
       End With
       oTemplate.AutoTextEntries("zDateField").Insert Where _
           :=.Range
       .MoveRight Unit:=wdCell
       oTemplate.AutoTextEntries(sAutoTextEntry1).Insert _
            Where:=.Range
       .MoveRight Unit:=wdCell
       oTemplate.AutoTextEntries(sAutoTextEntry2).Insert _
           Where:=.Range
       .MoveLeft Unit:=wdCell
       .MoveLeft Unit:=wdCell
       ActiveDocument.Tables(lTable).Rows(lRowCount).Select
       With .Cells
           .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
           .Borders(wdBorderRight).LineStyle = wdLineStyleNone
           .Borders(wdBorderTop).LineStyle = wdLineStyleNone
           .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
           .Borders(wdBorderVertical).LineStyle = wdLineStyleNone
           .Borders.Shadow = False
       End With    '   .Cells
   End With    '   Selection
   ActiveDocument.Range.Tables(lTable).Rows(lRowCount).Select
   Selection.HomeKey Unit:=wdLine
   ProtectDocumentMacro
End Sub

This is called from other procedures. The parameter tells it which table is
being changed.

It calls protect/unprotect procedures which follow:

Sub UnprotectDocumentMacro()
'   Unprotect document
   If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
     ActiveDocument.Unprotect
   End If
End Sub

Sub ProtectDocumentMacro()
   If ActiveDocument.ProtectionType <> wdAllowOnlyFormFields Then
       ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True
   End If
End Sub

Hope this is of some help. Note, I wrote these a long time ago and they work
in several of my billing and invoicing templates.
Signature

Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide

See also the MVP FAQ: http://word.mvps.org/FAQs/ which is awesome!

My criminal defense site: http://addbalance.com
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.

> Word XP all our clients PC are using.
>
[quoted text clipped - 33 lines]
>> reply
>> in the newsgroup and not by e-mail :-)
Cindy M  -WordMVP- - 13 Apr 2006 09:42 GMT
Hi =?Utf-8?B?VXNlcg==?=,

> Word XP all our clients PC are using.
>  
> I need a macro when a user runs it, it will create an additional row at the
> bottom of the current table (the i-blinking cursor will be inside the table
> when they run the macro).

The code for this would be:

   Dim tbl as Word.Table

   If Selection.Information(wdWithinTable) Then
       Set tbl = Selection.Tables(1)
       tbl.Rows.Add
   End If

> And another macro will be to create a table. We can specify the width and
> heights of each columns and rows through VBA, right?

Yes, certainly. Here's some framework to get you going:

   Dim tbl as Word.Table
   Dim numRows as Long
   Dim numCols as Long
   
   numRows = 3
   numCols = 4
   Set tbl = ActiveDocument.Tables.Add( _
       Selection.Range, numRows, numCols, _
       wdWord8TableBehavior)

   tbl.Rows.Height = InchesToPoints(1)
   tbl.Rows(3).Height = InchesToPoints(3)
   tbl.Columns(1).Width = InchesToPoints(1.3)

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :-)
 
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.