Help with a dynamic document

R

Robert Crandal

My document contains a centered sentence at the
top which acts as a header. Below the header I have
a 20x5 table (20 rows by 5 columns). This document
currently contains no data inside the table.

If someone types data into all 20 rows, I would like
to add a new page to this document that contains
a new header plus a new empty table (20x5). So,
if the table becomes full on Page 1, I would basically
like to create a new/empty Page 2 just in case more
data needs to be added.

How can I do this with macros?? I understand VBA
very well, but I don't have much experience using
macros in a Word 2007 document. I'd appreciate any
tips regarding this.

Thank you!
 
D

Doug Robbins - Word MVP

Is the data being entered into formfields in the cells of the table? If so,
you can have a macro run on exit from the formfield in the last cell of the
table that asked the user if they needed to insert more data and if they
answered yes, a new page containing containing the table could be added to
the document.

Alternatively, if you do not necessarily need a full 20 x 5 table, you could
just have the header in the first row of your table with the cells in that
row merged (and without the top, left and right border if desired) with that
row set to repeat, then when the user pressed tab in the last row of the
table, a new page would be added to the document that contained a table with
the header row and one row containing the five cells and each time they
pressed tab in the last cell of the table, a new row would be added.

Seems to me that unless you want a minimum of 20 rows whether required for
data or not on all pages, that the second method would be the way to go. No
code required.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
G

Graham Mayor

There is very little difference between Word 2007 vba and that of earlier
versions (except that perhaps the help is better in 2007).

A rough and ready method is to intercept the NextCell command to check which
cell the cursor is in when you tab out of it and if that cell is Row 20
Column 5 to insert a page break then add the heading line and insert a new
20 row five column table. Code that will do that follows. I suspect you will
have to modify the inserted table - oDoc.Tables(2) - to match the formatting
of the first table, but you can add the code to do that between inserting it
and selecting its first cell.

Sub NextCell()
Dim oDoc As Document
Dim oRng As Range
Dim iRow As Variant, iCol As Variant
Dim oTable As Table
Dim oHeading As Range
Set oDoc = ActiveDocument
Set oTable = oDoc.Tables(1)
Set oHeading = oDoc.Paragraphs(1).Range
If Selection.Range.InRange(oTable.Cell(20, 5).Range) Then
Set oRng = oDoc.Range
oRng.Start = oRng.End
oRng.InsertBreak wdPageBreak
Set oRng = oDoc.Range
oRng.Start = oRng.End
oRng.InsertAfter oHeading
oRng.End = oDoc.Range.End
oRng.ParagraphFormat.Alignment = wdAlignParagraphCenter
Set oRng = oDoc.Range
oRng.Start = oRng.End
oRng.Tables.Add oRng, 20, 5
'format the table as required
oDoc.Tables(2).Cell(1, 1).Select
Else
Selection.MoveRight Unit:=wdCell
End If
End Sub

If you need greater sophistication, for those users who will strive to screw
it up, then take a look at the invoicer template that you can download from
my web site
http://www.gmayor.com/Invoicer.htm.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
R

Robert Crandal

Hi Graham....thank you for responding.

I just wanted to ask, where exactly do I paste the subroutine that
you provided below?? I am really really newb when it comes
to using VBA with Word. Should I just paste it into the
code mode for "ThisWorkbook"???

Robert C.
 
R

Robert Crandal

Graham,

Very nice.....that code seems to do exactly what I hoped for.
However, the table grid lines are not visible when a new page
is created. My background is white and my table outline
color should be black. Which property or code should I
set now??? (thanks for tolerating my newbiness!)

thank u!
 
R

Robert Crandal

Hi again Graham,

I was wondering, where can I find a list of all commands or
event procedures that I can "intercept" in Word 2007???

Had you never mentioned the "NextCell" command, I never
would have known how to solve my earlier problem. I'm
just wondering where I can find a reference or listing to
for these macro commands.

Thank you!
 
G

Graham Mayor

Grid lines or cell borders? Try the following

Sub NextCell()
Dim oDoc As Document
Dim oRng As Range
Dim oCell As Cell
Dim iRow As Variant, iCol As Variant
Dim oTable As Table
Dim oHeading As Range
Set oDoc = ActiveDocument
Set oTable = oDoc.Tables(1)
Set oHeading = oDoc.Paragraphs(1).Range
If Selection.Range.InRange(oTable.Cell(20, 5).Range) Then
Set oRng = oDoc.Range
oRng.Start = oRng.End
oRng.InsertBreak wdPageBreak
Set oRng = oDoc.Range
oRng.Start = oRng.End
oRng.InsertAfter oHeading
oRng.End = oDoc.Range.End
oRng.ParagraphFormat.Alignment = wdAlignParagraphCenter
Set oRng = oDoc.Range
oRng.Start = oRng.End
oRng.Tables.Add oRng, 20, 5
'format the table as required
oDoc.Tables(2).Select
With Selection.Borders(wdBorderTop)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderLeft)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderBottom)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderRight)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderHorizontal)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderVertical)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
oDoc.Tables(2).Cell(1, 1).Select
Else
Selection.MoveRight Unit:=wdCell
End If
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

I don't know of a comprehensive reference list. CTRL+ALT++ will.give you a
cursor that when clicked on a command will give you its underlying command
name. Others (including NextCell) you can find from the All Commands group
that you add to the QAT.

With a few exceptions I prefer to run separate macros than intercept
built-in commands

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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