Numbered List in separate tables

T

Tendresse

Sorry if my post is a bit lengthy, i'll try to keep it as simple and clear as
possible.

My document is composed of a number of tables. Each table has 1 column and 2
rows: the first row has a heading and the second row has the relevant text
(body), formatted using a numbered list.

The numbered list is continuing through the tables. For example, if the
number of the last paragraph in table1 is 5, then the first paragraph in the
second row of table2 starts as number 6. And of course the numbers gets
adjusted automatically every time I add (or delete) any paragraph in any
table.

There is one blank line after each table to separate it from the following
one.

Users of that document may need to insert new tables between the exisiting
ones. These new tables should have a blank line before and after to separate
them from the surrounding ones. They should also have 2 rows and the
numbering of their paragraphs (in the second row) should continue from the
previous list.

I wrote a code to achieve that. It works very well. However, the success of
this code depends on the position of the cursor prior to running the macro.
The cursor HAS TO BE in the blank line between the tables AND right under the
second cell of the table above it. If the cursor is - for example - inside
one of the tables, or if the cursor is a few lines away from the table above
it, the macro doesn't achieve the required result.

I know that some of the users may not give any attention to the position of
the cursor before running the macro (even if I prompt them to do so using a
msgbox at the beginning of the macro). Therefore, i was wondering if there is
a way that checks the position of the cursor, and Exit Sub if the cursor is
not right under the second cell of the table above it.

Please advise if you need me to give a brief description of the code I worte.

Any suggestions will be much appreciated.

Many thanks
Tendresse
 
C

Carolin

It's rather difficult to test for each possible scenario. However to test if
the cursor is in a table or on a paragraph mark:

Selection.Collapse wdCollapseStart
If Selection.Information(wdWithInTable) Then [selection is in a table]
If Asc(Selection) = 13 Then [selection is at a paragraph mark]
 
J

Jean-Guy Marcil

Tendresse was telling us:
Tendresse nous racontait que :

With such a screen name, I had no chice but offer all the help I could! ;-)
Sorry if my post is a bit lengthy, i'll try to keep it as simple and
clear as possible.

Better a little too much than, as is often the case, not enough... which
leads to misunderstandings, frustration and a general waste of time for a
all involved!
My document is composed of a number of tables. Each table has 1
column and 2 rows: the first row has a heading and the second row has
the relevant text (body), formatted using a numbered list.

The numbered list is continuing through the tables. For example, if
the number of the last paragraph in table1 is 5, then the first
paragraph in the second row of table2 starts as number 6. And of
course the numbers gets adjusted automatically every time I add (or
delete) any paragraph in any table.

There is one blank line after each table to separate it from the
following one.

Users of that document may need to insert new tables between the
exisiting ones. These new tables should have a blank line before and
after to separate them from the surrounding ones. They should also
have 2 rows and the numbering of their paragraphs (in the second row)
should continue from the previous list.

I wrote a code to achieve that. It works very well. However, the
success of this code depends on the position of the cursor prior to
running the macro. The cursor HAS TO BE in the blank line between the
tables AND right under the second cell of the table above it. If the
cursor is - for example - inside one of the tables, or if the cursor
is a few lines away from the table above it, the macro doesn't
achieve the required result.

I know that some of the users may not give any attention to the
position of the cursor before running the macro (even if I prompt
them to do so using a msgbox at the beginning of the macro).
Therefore, i was wondering if there is a way that checks the position
of the cursor, and Exit Sub if the cursor is not right under the
second cell of the table above it.

Please advise if you need me to give a brief description of the code
I worte.

Any suggestions will be much appreciated.

Many thanks


Here is some code that covers all eventualities... at least those I could
think of!

'_______________________________________
Dim rgeInsert As Range
Dim tblNew As Table

'If user has selected a range, let's assume that the table _
must be inserted at the beginning of the selection
Selection.Collapse wdCollapseStart

If Selection.Information(wdWithInTable) Then
Set rgeInsert = Selection.Tables(1).Range
'Now let's assume the new table must be inserted below _
the currently selected one, even if that seems to contradict _
the above comment!
rgeInsert.Collapse wdCollapseEnd
Else
Set rgeInsert = Selection.Range
End If

With rgeInsert
If Not .Paragraphs(1).Range.Text = Chr(13) Then
'In case cursor not immediately before the ¶
.MoveEndUntil Chr(13)
.Collapse wdCollapseEnd
Else
'if we have a lone ¶, add another ¶ _
to separate new table from preceding one
.InsertParagraphBefore
.Collapse wdCollapseEnd
End If
End With

Set tblNew = ActiveDocument.Tables.Add(rgeInsert, 2, 1)

'Format table as desired
With tblNew
.Cell(2, 1).Range.Style = "NumberedStyle"
With .Borders
.OutsideLineStyle = wdLineStyleSingle
.OutsideLineWidth = wdLineWidth225pt
.InsideLineStyle = wdLineStyleSingle
.InsideLineWidth = wdLineWidth050pt
End With
'etc.
End With
'_______________________________________

--

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

Tendresse

Carolin, thank you very much for your reply. Much appreciated.
Jean-Guy Marcil, merci enormement pour ta reponse :).

Jean, I gave your code a go but i got the following error:

run-time error '5834'
Item with specified name does not exist.

and the debug highlighted the following line in the code in yellow:

..Cell(2,1).Range.Style = "NumberedStyle"

I'm not sure what Item the error refers to. I'm relatively new with VBA so
i'm sort of feeling my way still.

Salut,

Tendresse
 
J

Jean-Guy Marcil

Tendresse was telling us:
Tendresse nous racontait que :
Carolin, thank you very much for your reply. Much appreciated.
Jean-Guy Marcil, merci enormement pour ta reponse :).

Jean, I gave your code a go but i got the following error:

run-time error '5834'
Item with specified name does not exist.

and the debug highlighted the following line in the code in yellow:

.Cell(2,1).Range.Style = "NumberedStyle"

I'm not sure what Item the error refers to. I'm relatively new with
VBA so i'm sort of feeling my way still.

You have to replace "NumberedStyle" by the actual style name you are using
for numbering the paragraphs in the second row

--

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