PDA

View Full Version : Adding/Deleting Rows in a Table



Greg
09-13-2006, 06:02 PM
Hi all,

This is a very general question.

I have seen documents (Word, I think) where a small cross (i.e. an X) in the top right hand corner of a table is used to activate a response such as deleting/adding a row or adding some text or whatever.

If such a thing is possible would someone give me a clue as to how it can be achieved. At this stage my main use for it would be to add or delete rows in a table but if it could be used for other things I am sure I could adapt it accordingly.

I hope this is not too vague.

greg.

fumei
09-14-2006, 08:16 PM
Hmmmm. Do you have such a document?

There is nothing like this that is native to Word. You could make a wee image file and place it in a table. If it was an ActiveX Image control you could use the Image_Click event to fire code.

What exactly is the business case for this? It is easy to add and delete rows from a table using the Table menu, so I am not quite getting the essential use for this.

Greg
09-14-2006, 11:39 PM
Hi fumei,

As you may recall I have been using userforms in legal documents to insert the names of the plaintiff and defendant and their addresses, etc.

However, when I need to add a second defendant or third party I have to manually insert more rows (and name them appropriately). I wish to avoid this.

Perhaps the best way to do this is via the userform where I could have a button to add, for example, a second, third or fourth defendant (or third party) as the case may be.

Whilst I can probably (given enough time) work out how to achieve this, I expect that it will be more difficult where I need to add those additional rows on more than one page of the summons. In fact, the summons in question requires the same headings to be added on five (5) different pages. It's a bloody awful document.

I have no difficulty if there's only one plaintiff and one defendant because I simply fill a bookmark for each and ref fields on the other four pages are then updated.

However, I don't know how to add rows (and headings) where the changes have to be made on more than one page.

Ideally, I would choose the number of rows to added via the userform and after inserting the names (and addresses) (and the type e.g. defendant or third party) the changes could be reflected throughout the whole document.

The magnitude of the problem seems somewhat larger than I had initially contemplated, so any assistance will be appreciated.

Greg.

Norie
09-15-2006, 08:26 AM
Greg

Could you not have a listbox on the userform which contains all the required information?

The idea would be to have textboxes/comboboxes for the actual data and a command button to add the data from them to the listbox.

Then when it comes to filling out the document you could loop through each row of the listbox, add a row to the table and insert the data as required.

fumei
09-15-2006, 08:51 AM
Could you detail this out?

As I see it, there are two situations.

1. Being able to add rows while using the userform. This would not be difficult. In other words, settings things up AT THE START.

2. Changing existing tables.

Your original post seemed to asking about changing the table from the document, not from the userform.
In fact, the summons in question requires the same headings to be added on five (5) different pages. I am not sure what you mean by "headings". Are you talking about five different tables?

In any case, regarding the use of the userform Norie has the right idea.

Norie
09-15-2006, 12:10 PM
Greg

Please find attached an example of what I mean.

Note it's very much a work in progress.

Greg
09-16-2006, 11:01 PM
Thanks fumei, the intention is to change the existing tables (by adding new rows). The document in question is full of tables and I hope to modify some of those tables via the userform.

The headings I refer to are headings such as Plaintiff or Defendant. If I add a row (to add a second defendant) the heading on the new row should say "Second Defendant" or "2nd Defendant" and so on.

The main problem, is that the same kind of table appears on 5 separate pages of the document.

So, my wish is to add new rows via the userform (currently used only to add things such as names, addresses, amount of claim, etc.) so that the new rows are also added on the other pages of the document.

If this needs further clarification I'll send the document to you.

Regards,

Greg.

Norie
09-16-2006, 11:15 PM
Greg

Did you read any of my posts?:)

I realise the attachment isn't exactly what you need but I thought it might give you some ideas.

I've done this type of thing before, creating more complicated but formulaic legal documents in Word using data from Access.

fumei
09-17-2006, 04:54 PM
Norie, I downloaded your file, but it is empty. There is nothing there. No tables. No code. Nothing. What was it supposed to be doing?

Greg, when posting always try to be accurate.
so that the new rows are also added on the other pages of the document.is not accurate.

You mean new rows are added to the other tables. The fact that these tables are on other pages is not relevant.

Yes, post a file. I have been playing with this, and it certainly is possible. However, as has been posted before, you must precisely define what you want to happen.

Norie, could you post a file that does something? I would like to see what you had in mind.

Greg
09-17-2006, 05:23 PM
Hi fumei,

The subject file is attached (I hope).

Greg.

Norie
09-17-2006, 09:34 PM
Gerry

Sorry.:oops:

There is something in the attachment but you would need to go to the VBE to see it.

It's been some time since I've worked with Word VBA, and I just couldn't remember exactly how to get a userform to appear as soon as the document opened, or how to put a button on on the document to open one.

Like I said it's a work in progress and doesn't quite address the OP's question directly, I just thought it might give them some ideas.

fumei
09-18-2006, 01:12 PM
Norie. I am sorry, but I am going to have to dispute you. Of course I looked in the VBE. I will repeat myself....there is NO code in the attachment you posted. It is a completely blank document, as a document; and a completely blank file regarding code.

There are no code modules, other the standard ThisDocument, and it is....empty.

So sorry, you are wrong.

BTW: to get a userform to display on document open use the Document_Open event. It is in the ThisDocument code module. Click Document in the left dropdown, then Open from the right dropdown - or of course simply write it.

Sub Document_Open()
UserForm1.Show
End Sub

Could it be that you saved your code to normal.dot? That is the default location if you are recording a macro.

Norie
09-18-2006, 01:27 PM
I didn't save it to normal.dot, I placed the userform there.:oops:

Like I said a bit rusty with VBA for word.

I'll try reattaching.

fumei
09-18-2006, 01:55 PM
OK. that has code.

Comments.

1. It is a VERY good idea to always explicitly name controls. Sub CommandButton2_Click() - gives no clue, at all, what it is supposed to do.

Sub DeleteItem_Click() WOULD.

Naming controls explicitly makes reading code much easier.

2. The OG's question is regarding adding rows to existing tables. Not creating tables. This is a significant difference.

3. There is no error trapping in this, but that is OK, as it is supposed to give Greg some ideas.

Greg, Norie's demo has some flaws, but it certainly may give you ideas.

As your question IS regarding existing tables, you have a fairly complex logic issue.

1. WHERE in the existing tables do you want the new rows? This must be very explicit.

2. Do these new rows need to be included in the headers of the tables?

Norie
09-18-2006, 02:18 PM
Gerry

I realise all that, like I said it's very much a work in progress.:)

I'm familiar with most of the programming conventions eg using relevant names for variables/objects , using Option Explicit etc, and generally use them myself but for this I just knocked something up.

There is a little bit of error handling in there - before any tables are added/created there's a check to see if there's actually any data in the listbox.

If by error handling you mean using things like On Error Resume Next, then I generally try to avoid using that approach, preferring to, if possible, eliminate the cause of the error.

The last big project I did involving Word was to automate it via VBA to create form legal letters from an Access database and part of that did involve adding new data/rows/columns to existing tables.

Like I say I'm a bit rusty with the old Word VBA syntax.:)

Greg
09-18-2006, 05:51 PM
Thanks Norrie and Gerry. The code provided certainly does give me ideas and I am sure I'll find a good use for it on another project.

However, my particular problem relates to adding (and maybe deleting) rows to existing tables.

Insofar as the placement of those rows is concerned, any additional Defendant rows would appear below the existing Defendant row in the document attached to my last response.

However, I would hope to maintain the appearance of the document by including a small blank row between the parties as currently exists between the Claimant and the Defendant.

An example of the document using 4 defendants is attached.

Greg.

mdmackillop
09-19-2006, 12:36 AM
Hi Greg,
I'd go for the simple method. Add the additional table lines and text for the additional defendant and set the font to Hidden. You can unhide the text in the appropriate cells via VBA if required.
Regards
MD

Greg
09-19-2006, 01:46 AM
Thanks MD but my hope was to automat the process so that the likelihood of error is reduced. The additional rows have to appear on 5 different pages and the chance that one of them will be ovelooked is quite high and can be embarrasing when trying to lodge the documents with the Court.

A sample of the subject form is attached.

Regards,

Greg.

mdmackillop
09-19-2006, 03:14 AM
In that case hide the text or delete the relevant rows where "Defendant" is empty.
or
Create autotexts (or files) containing tables to suit 1 - ? defendants. Insert these at a series of bookmarks based upon a number entered in an InputBox.

ctengelen
09-19-2006, 08:29 AM
In that case hide the text or delete the relevant rows where "Defendant" is empty.
or
Create autotexts (or files) containing tables to suit 1 - ? defendants. Insert these at a series of bookmarks based upon a number entered in an InputBox.

Hi,

Here is also a newbie and I have used the "Autotext" insert quite extensive in my on-line document. I created a macro button "more subjects" , the user clicks it and the macro goes to a bookmark, inserts the autotext and the user now can direct enter the new information. This can also be done via a userform I am sure.

Trudy:hi:

Greg
09-19-2006, 05:28 PM
Hi Trudy,

Just as soon as I solve the additional row problem I intend adding a further userform for the purpose of inserting various indorsements (i.e. short legal pleadings) into my document.

I expect that the userform would have radio buttons (or a drop-down list so that I could choose what type of indorsement to add.

The indorsements would be held as autotext and would be inserted into a bookmark in the manner you suggest.

If you have done something similar I would be grateful if you were to provide me with an example of it.

Regards,

Greg.

mdmackillop
09-20-2006, 10:43 AM
Here's a sample

Greg
09-20-2006, 05:51 PM
Thanks MD,

That worked beautifully when I first unzipped and opened it but try as I might I couldn't get it to work once I had saved it elsewhere. When I debugged, the curser stopped at frmTable.Show. I can't see why.

Public Sub addTable()
frmTable.Show (this seems to be the problem area)
End Sub

However, once I sort that out my hope would be to add that functionality to my existing userform.

Indeed, if I was able to choose the number of defendants from the outset (or add/subtract them at will) I would want the existing userform to also offer me additional spaces to type in the names and addresses of those defendants.

Do you have any ideas as to how that might be achieved?

A better version of the document I regularly use is attached (I think).

Regards,

Greg.

Greg
09-20-2006, 07:15 PM
Hi again,

Further to my latest email, I see that your document is a Document template. That would explain why all my other related doucments have been affected by it. I wonder if this is necessary?

Although your method works very nicely (initially at least), is there a way to add those rows that doesn't rely the use of a global template?

Greg.

mdmackillop
09-21-2006, 12:28 AM
I can't think of a method of doing whaty you're after to a Document that would be practical in day to day use. It is more appropriate to adjust your other code, which I haven't touched, to work in a new document based on a template rather than the other way round.

Greg
09-21-2006, 02:55 AM
Hi again Gerry. MD and Norrie have both been very helpful.

MD has provided a method for adding rows to my table which works very well. However, I feel that its functionality needs to be incorporated into my exiting userform.

Indeed, it would be ideal if, after choosing the number of extra defendants to be added via MD's form, but before the actual rows were added, a click event would bring up a further userform into which I could enter the names and addresses just as I am able to with my existing userform.

If the added rows also contained bookmarks, I could then fillabookmark (and related ref fields) via the extra userform so that the document would then be populated with the new defendants' details.

This is of course a long wish list and I know that I have to take this one step at a time. So, the first question is: Can it be done and if so, where do I start. Should I stick with MD's form and try to incorporate it into my existing userform or should I take another approach entirely?

Any useful comments will be appreciated.

Greg.

ctengelen
09-21-2006, 07:41 AM
Hi,

I created my form using tables. The entries is made direct into the
form not using UserForms. At a certain point the user may
have to insert more information than there is room.
Like we don't know how many warrants a person has outstanding.

The rows/columns in the warrant section I copied into "AutoText"
making sure it is attached to the document template (not global).

The macro unprotects the form, goes to the bookmark
"InsertNewWarrant" and inserts the new rows/columns
from the AutoText and protects the form again.

The user can now insert data into the new fields.



Sub InsertNewWarrant()

' Macro Insert a New Warrant
' Macro recorded 06/04/07 by engelent
'
ActiveDocument.Unprotect Password:=""

Selection.GoTo What:=wdGoToBookmark, Name:="InsertNewWarrant"
With ActiveDocument.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With

Selection.MoveLeft Unit:=wdCharacter, Count:=1

(I placed my bookmark one character to the right to ensure
it will not be replaced by the AutoText insert - it will
be pushed to the end so it is available for more inserts.)

'Insert AutoText entry

Application.DisplayAutoCompleteTips = False
ActiveDocument.AttachedTemplate.AutoTextEntries("InsertNewWarrant").Insert _
Where:=Selection.Range, RichText:=True

' Curser jumps to a specific spot in the table

Selection.GoTo What:=wdGoToBookmark, Name:="agency"

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""


End Sub

I wanted to use UserForms but the powers to be here
wanted to have direct entry into the form.

Well, like I said, I am a newbie in VBA - but this form sits on our
shared drive and is been in use since June by hundreds of users.
No complaint so far! Amazing!

I you would like me to post my complete form - I will strip it of sensative stuff and post it.

Trudy

Greg
09-21-2006, 06:22 PM
Thanks very much for that Trudy.

I am curious to see what your document looks like and how that works for you in practice. Are you able to zip one up and make it available?

Regards,

Greg.

ctengelen
09-26-2006, 02:18 PM
I hope it works for you. As you tab throught the different TextFormFields, different Toolbars will open.

Have fun.
Trudy

mdmackillop
09-26-2006, 05:21 PM
Hi Trudy,
Why not add a macro to run on entry to open your dropdowns when you tab to them?

Sub DD()
SendKeys "{F4}", True
End Sub

Greg
09-26-2006, 05:22 PM
Thanks Trudy,

The many different tool bars look very useful but they don't work for me yet. I think the autotext entries may not have travelled with the document.

Anyway, I'll have another look at it shortly.

Greg.

ctengelen
09-27-2006, 01:59 PM
Hi mdmackillop! :bow:

You are a genius! Not only a Guru! This little macro, added to my form makes things easier for the users here!


***Sub DD()
SendKeys "{F4}", True
****End Sub


Thanks again
Trudy

mdmackillop
09-27-2006, 02:41 PM
It's the little things that make the difference!:friends:

ctengelen
09-28-2006, 11:12 AM
Hi Greg, please make sure that the form is "Protected" or it won't work!

Trudy :whistle:

Greg
09-28-2006, 05:18 PM
Hi Trudy,

I have since realised this but thanks anyway.

Greg.