Insert Table Row (if/then)

C

Claudine

Hi!

I need a little help. I'm creating a document for my
group. I am trying to automate as much as possible to
eliminate errors in formating and just make things a
little simpler for everyone (in lieu of copy/paste).
I've created a Table with titles on the left and content
on the right. I created autotext entries that contain
the information in each row so that the user can just
insert the appropriate group of information. The only
problem is, that if the user is outside the table, the
autotext entry works properly. If they are inside a
cell, then it gets a little screwy. Is there a macro
that I can use to facilitate this? Maybe something that
looks to see if they're in the cell already?

Any help greatly appreciated.

Claudine
 
D

Dave Lett

I Claudine,

If you have something really simple, then you can use

Selection.Information(wdWithInTable) to return true or false

This works great if you only have an insertion point. If, however, you have
a range highlighted and either the start or end of the range is not in the
table, then Selection.Information(wdWithInTable) will always return false.

If you need to account for highlighted ranges, then you can start with
If Selection.Tables.Count > 0 Then

and move on to start the start and end of the range to determine where you
have your table(s).

HTH,
Dave
 
J

Jean-Guy Marcil

You can use something like to split the table (if necessary) and insert the
autotext:

'_______________________________________
Sub InsertMyAutoText()

With Selection
If .Information(wdWithInTable) Then
Tables(1).Split .Rows(1)
End If
End With
AttachedTemplate.AutoTextEntries("MyAutoText").Insert _
Where:=Selection.Range
'If you are using Normal.dot for your AutoText entries:
'NormalTemplate.AutoTextEntries("MyAutoText").Insert _
' Where:=Selection.Range

End Sub
'_______________________________________

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

Claudine

Thanks Jean-Guy,

What if the user is outside the table? That's my only
problem.. I need to cover the instance when they are in
the table or NOT in the table.

Claudine
 
J

Jean-Guy Marcil

Ooops. Just read Dave's post... always forget about the selected range
possibility!

Since you are building something to assist users in inserting autotexts, if
they have selected a wide range of text that spans many tables, they are
asking for trouble! Nevertheless, it can happen. Here is a variation:

'_______________________________________
Sub InsertMyAutoText()

With Selection
.Collapse
If .Information(wdWithInTable) Then
Tables(1).Split .Rows(1)
End If
End With
AttachedTemplate.AutoTextEntries("MyAutoText").Insert _
Where:=Selection.Range
'NormalTemplate.AutoTextEntries("MyAutoText").Insert _
' Where:=Selection.Range

End Sub
'_______________________________________

This assumes that if there is a range selected, the user wants the autotext
at the beginning of the selection.

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

Jean-Guy Marcil

Bonjour,

Dans son message, < Claudine > écrivait :
In this message, < Claudine > wrote:

| Thanks Jean-Guy,
|
| What if the user is outside the table? That's my only
| problem.. I need to cover the instance when they are in
| the table or NOT in the table.
|

What is it that you want the code to do if the insertion point is not in a
table?

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

Claudine

Hi Dave,

Ok, I guess I need the beginner beginner group or
something. I'm not sure where to start actually.

Let me restate my issue and then show you what I did for
a solution (that isn't that great).

I have people entering info into a table. Each row has a
particular format. The user is able to enter the
particular TYPE of row based on autotext entries.
However, it only works if they are OUTSIDE of the last
table row. If they are IN the table (like they just
finished and haven't clicked outside the table) then the
autotext entry doesn't work properly. SO, I assumed that
most users would still be inside the table and created
the following macro:

Sub insertpullout()
'
' insertpullout Macro
' Macro recorded 6/30/2004 by Claudine M. Jalajas
'
Selection.MoveRight Unit:=wdCell
Selection.Rows.Delete
NormalTemplate.AutoTextEntries
("freeformtable").Insert Where:=Selection. _
Range
End Sub


However, if they are NOT in the table, well, they get an
error.

Claudine
 
C

Claudine

I would like it to then insert the autotext. The
autotext is set up to work properly if they are NOT in
the table.

Claudine :)
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Claudine > écrivait :
In this message, < Claudine > wrote:

|| Hi Dave,
||
|| Ok, I guess I need the beginner beginner group or
|| something. I'm not sure where to start actually.
||
|| Let me restate my issue and then show you what I did for
|| a solution (that isn't that great).
||
|| I have people entering info into a table. Each row has a
|| particular format. The user is able to enter the
|| particular TYPE of row based on autotext entries.
|| However, it only works if they are OUTSIDE of the last
|| table row. If they are IN the table (like they just
|| finished and haven't clicked outside the table) then the
|| autotext entry doesn't work properly. SO, I assumed that
|| most users would still be inside the table and created
|| the following macro:
||

Have you tried my code?
It allows users to insert an autotext entry even if they are in a table. Or
do you want to limit users to append autotext entries only at the bottom of
a table? If so, why? Users, like anybody else, will make mistakes and forget
stuff or change their minds and will want to go back and insert something in
the middle of the table.... Unless you have a very specific context that
limits users' actions...

The only problem I see is if they are outside the table, but not in the
paragraph immediately preceding or following the table. Inserting you tabled
autotexts will create a new table. Then, all users have to do is delete the
¶ between their insertion and the table they wanted to append.

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

Jean-Guy Marcil

Bonjour,

Dans son message, < Claudine > écrivait :
In this message, < Claudine > wrote:

| I would like it to then insert the autotext. The
| autotext is set up to work properly if they are NOT in
| the table.
|

The code I posted will work inside AND outside a table. Read my other post
in the other branch of this thread and let me know if you have reasons to
prevent users from inserting autotexts inside a table (I mean, other than
your code did not work. I believe mine will, unless your autotexts are not
made up uniquely of table elements, i.e. rows; they should not contain any ¶
before or after the autotext row you created.) I know it can work , I have
built a template similar to the one you described. The whole document is a
bunch of table rows slapped together from autotexts inserted with code
similar to the one I posted.

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

Claudine

Hello,

I tried your code (thank you) and got an error. It
says "compile error, sub or function not defined." Then
it highlights the text "Table (1)" in the line,
Tables(1).Split .Rows(1)

What am I doing wrong?

Claudine

FYI: The code says:

Sub insertpullout()

With Selection
.Collapse
If .Information(wdWithInTable) Then
Tables(1).Split .Rows(1)
End If
End With
NormalTemplate.AutoTextEntries("bulletspullout").Insert _
Where:=Selection.Range

End Sub
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Claudine > écrivait :
In this message, < Claudine > wrote:

|| Hello,
||
|| I tried your code (thank you) and got an error. It
|| says "compile error, sub or function not defined." Then
|| it highlights the text "Table (1)" in the line,
|| Tables(1).Split .Rows(1)
||
|| What am I doing wrong?

Nothing! My bad!
Forgot to type a dot in front of Tables(1). <blush>
In XP it works without the dot... But it is not good practice to let the
compiler make decisions in your place!

Actually, replace

.Tables(1).Split .Rows(1)

by
.SplitTable

It is more reliable. My first version will not work if the selection is in
the first row.
Also, warn users that if the cursor is in a table, the autotext will be
inserted *before* the row (or first row if more than one is selected)
currently selected.

I see that your autotext are in Normal.dot. If you intend to distribute your
template, you have to put the autotext in the template itself and use the
other autotext insertion lines I posted, i.e:

AttachedTemplate.AutoTextEntries("MyAutoText").Insert _
Where:=Selection.Range

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

Claudine

Thank you so much!! You've really helped me out. Now, I
have one more question. I see it creates a split table
if the user is outside the table, but what if I want them
to be together (each row). Is this something the user
will have to adjust for?
Claudine
 
C

Claudine

One other thing, I noticed that when the autotext is
inserted using the macro, it loses the formating
(bullets, font, etc) but when the autotext is inserted
with just the autotext entry, it works properly. Any
ideas?

Thank you, Claudine :)
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Claudine > écrivait :
In this message, < Claudine > wrote:

| Thank you so much!! You've really helped me out. Now, I
| have one more question. I see it creates a split table
| if the user is outside the table, but what if I want them

?

You cannot split a table unless your inside the table. The code splits the
table if they are inside the table.
So I am not sure what you mean here.

| to be together (each row). Is this something the user
| will have to adjust for?
| Claudine
|

Maybe you mean to write that if the user is inside the table, and then uses
the code, the result is a split table. That is true, my bad! Forgot about
that. Same about the formatting when inserting the autotext. Finally, I hate
the principle that the autotext will be added ABOVE the current selection...
very annoying. So here is a modified, and I hope improved, version of the
code. It takes care of all those issues, i.e. Result not split, Formatting
preserved & Insert below currently selected row.

'_______________________________________
Sub InsertMyAutoText()

Dim InsideTable As Boolean

InsideTable = False

Application.ScreenUpdating = False

With Selection
If .Information(wdWithInTable) Then
.SelectRow
.MoveDown Unit:=wdLine, Count:=1
If .Information(wdWithInTable) Then
.SplitTable
InsideTable = True
End If
End If
End With
AttachedTemplate.AutoTextEntries("MyAutoText").Insert _
Where:=Selection.Range, RichText:=True

If InsideTable Then Selection.Delete

Application.ScreenRefresh
Application.ScreenUpdating = False

End Sub
'_______________________________________


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

Jean-Guy Marcil

Bonjour,

Dans son message, < Claudine > écrivait :
In this message, < Claudine > wrote:

|| One other thing, I noticed that when the autotext is
|| inserted using the macro, it loses the formating
|| (bullets, font, etc) but when the autotext is inserted
|| with just the autotext entry, it works properly. Any
|| ideas?
||
|| Thank you, Claudine :)

See my reply to your other question.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Claudine > écrivait :
In this message, < Claudine > wrote:

| PERFECT!!! Merci beaucoup! ;-)
|

Glad I could help and that I finally got it right!

--
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