Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-08-2012, 01:29 AM
shabbaranks shabbaranks is offline Macro to add row to table in a Word form Windows 7 64bit Macro to add row to table in a Word form Office 2007
Advanced Beginner
Macro to add row to table in a Word form
 
Join Date: Mar 2011
Posts: 89
shabbaranks is on a distinguished road
Default Macro to add row to table in a Word form

Hi guys,



Ive never really understood how to automate a macro within a word document as Ive always used vba in access with button triggers.

Can anyone tell me how to automatcally add a new row to a table as the current row is completed (if that makes sense?). This is within a form so obviously the document is protected to a point.

Not too sure if my question is clear enough so please feel free to ask for more info - much apprecicated.

Thanks!
Reply With Quote
  #2  
Old 08-08-2012, 02:05 AM
macropod's Avatar
macropod macropod is offline Macro to add row to table in a Word form Windows 7 64bit Macro to add row to table in a Word form Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

See the attached document for a demonstration.
Attached Files
File Type: doc Formfields - Table Row Add.doc (55.0 KB, 629 views)
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 08-08-2012, 02:09 AM
shabbaranks shabbaranks is offline Macro to add row to table in a Word form Windows 7 64bit Macro to add row to table in a Word form Office 2007
Advanced Beginner
Macro to add row to table in a Word form
 
Join Date: Mar 2011
Posts: 89
shabbaranks is on a distinguished road
Default

Thanks will have a look. So how would this VBA be called? Is it the triggered by the fact the cursor is within a perticular cell in the table?

Thanks
Reply With Quote
  #4  
Old 08-08-2012, 03:31 AM
macropod's Avatar
macropod macropod is offline Macro to add row to table in a Word form Windows 7 64bit Macro to add row to table in a Word form Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi shabbaranks,

The AddRow macro is triggered via the 'On Exit' property of the last formfield in the table.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 08-09-2012, 04:35 AM
shabbaranks shabbaranks is offline Macro to add row to table in a Word form Windows 7 64bit Macro to add row to table in a Word form Office 2007
Advanced Beginner
Macro to add row to table in a Word form
 
Join Date: Mar 2011
Posts: 89
shabbaranks is on a distinguished road
Default

Im still not completely sure how this works? Ive added the VBA to the document but nothing happens when I get to the last cell on a table within a protected form. When you say you would call the macro "on exit" - is this on exit of the last cell? Also to fill in the form the user would tab through the different parts - is this the preferred method of inputting into a form?

Thanks!
Reply With Quote
  #6  
Old 08-09-2012, 04:14 PM
macropod's Avatar
macropod macropod is offline Macro to add row to table in a Word form Windows 7 64bit Macro to add row to table in a Word form Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

It appears you haven't updated the last formfield's 'on exit' property to refer to the macro. To do this, right-click on the formfield, choose 'properties' then use the 'on exit' property to select the macro.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 08-10-2012, 01:11 AM
shabbaranks shabbaranks is offline Macro to add row to table in a Word form Windows 7 64bit Macro to add row to table in a Word form Office 2007
Advanced Beginner
Macro to add row to table in a Word form
 
Join Date: Mar 2011
Posts: 89
shabbaranks is on a distinguished road
Default

Within the table I have text controls - would this make a difference to the vba adding the columns in the table as I still cant get it to work
Reply With Quote
  #8  
Old 08-10-2012, 01:24 AM
macropod's Avatar
macropod macropod is offline Macro to add row to table in a Word form Windows 7 64bit Macro to add row to table in a Word form Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Yes, it makes a fundamental difference. Your reference to:
Quote:
This is within a form so obviously the document is protected to a point.
led me to believe you're using formfields, as the same kind of document protection is not necessary with content controls.

For a document using content controls, one needs to call the 'ContentControlOnExit' event, or perhaps the 'ContentControlBeforeStoreUpdate' event. The process is quite different to what you'd use with formfields. I'll put together some code and get back to you.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 08-10-2012, 01:34 AM
shabbaranks shabbaranks is offline Macro to add row to table in a Word form Windows 7 64bit Macro to add row to table in a Word form Office 2007
Advanced Beginner
Macro to add row to table in a Word form
 
Join Date: Mar 2011
Posts: 89
shabbaranks is on a distinguished road
Default

Apologies but I dont think I get it.. I am using a form (the document is protected when the user enters text) but instead of using the legacy form controls I am using the text controls. Am I right in thinking the text controls are designed to be used on forms?

Again sorry for the mis-understanding

Heres a link to an image of the document - as you can see its protected and form based
http://www.image-share.com/ijpg-1659-212.html
Reply With Quote
  #10  
Old 08-10-2012, 03:17 AM
macropod's Avatar
macropod macropod is offline Macro to add row to table in a Word form Windows 7 64bit Macro to add row to table in a Word form Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Yes, you can use Content Controls for a form. The attached works with or without the restrictions that apply to forms protection or the docx/m-format editing restrictions. It is currently configured for the latter. Simply exiting the last content control in the table will trigger the prompt that asks whether to add a new row. The dropdowns are presently not populated but, if you populate them, you'll see that their selection options are replicated when you add a new row.

To insert the new row immediately above the last row in the table, you could modify the ContentControlOnExit macro in the document by inserting:
'Break the table before the last row
.Last.Range.InsertBreak wdColumnBreak
after:
With Selection.Tables(1).Rows
and deleting or commenting-out:
.Next.InsertBefore vbCr

With that change, the penultimate table row will be replicated.

Note: As coded, you'll get two prompts if you answer 'no' to the addrow question if you click on another content control when exiting the last one in the table. If that annoys you, you could delete the lines:
Code:
'Exit if we're not in a table - we don't really need this is using a bookmarked table,
' but it's a safeguard against the bookmark having been expanded/moved.
If CCtrl.Range.Information(wdWithInTable) = False Then Exit Sub
Attached Files
File Type: docm Content Controls - Table Row Add.docm (48.4 KB, 526 views)
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #11  
Old 10-31-2016, 10:04 AM
cbWilson cbWilson is offline Macro to add row to table in a Word form Windows 8 Macro to add row to table in a Word form Office 2001 for Mac
Novice
 
Join Date: Oct 2016
Posts: 1
cbWilson is on a distinguished road
Default

I've copied and passed the entire table into my document and just added one column changing and using the type of controls I need and the name of the bookmark. Which works great! My problem is the focus in new row is in the second column instead of the first column. Small problem. I can tab back to the first column. Any suggestions?
Reply With Quote
  #12  
Old 10-31-2016, 04:35 PM
macropod's Avatar
macropod macropod is offline Macro to add row to table in a Word form Windows 7 64bit Macro to add row to table in a Word form Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

There is nothing in the code that would cause that; perhaps it has something to do with your table structure?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #13  
Old 11-14-2018, 07:32 PM
Marilyn999 Marilyn999 is offline Macro to add row to table in a Word form Windows 10 Macro to add row to table in a Word form Office 2016
Novice
 
Join Date: Nov 2018
Posts: 1
Marilyn999 is on a distinguished road
Default Macro to add rows to a table with calculations

In the file attached, the last formfield, that has the AddRow macro set as its 'Run macro on: Exit' property, clicking on it does nothing. The formfield concerned is in column 2. Could this be the problem (although, I did try putting a field into the last column (column 7) and running the macro from there and still got nothing)?
Attached Files
File Type: docm Table.docm (32.0 KB, 76 views)

Last edited by macropod; 11-15-2018 at 02:29 PM. Reason: Claifying context added from https://answers.microsoft.com
Reply With Quote
  #14  
Old 11-15-2018, 05:01 AM
macropod's Avatar
macropod macropod is offline Macro to add row to table in a Word form Windows 7 64bit Macro to add row to table in a Word form Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try the attached. Because your table has merged/split rows and the formfield that triggers the macro isn't in the last column that contains one, a different approach is required. I've also added the code required to handle your row-based formula fields.

In the code, you'll see a commented-out line with:
RngSel.FormFields(1).ExitMacro = ""
When you're satisfied the form is ready for release into production, un-comment that line. This will prevent the macro from being triggered by any except the last 'Reason' formfield in the table.

Note that I've streamlined your other macros, too.
Attached Files
File Type: docm Table.docm (34.7 KB, 138 views)
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #15  
Old 11-19-2019, 07:14 AM
noemis noemis is offline Macro to add row to table in a Word form Windows 8 Macro to add row to table in a Word form Office 2016
Novice
 
Join Date: Nov 2019
Posts: 5
noemis is on a distinguished road
Default

Hi Paul,

I'm trying to do exactly the same as in the first example you did, I want them to be able to add rows only when they have completed the last field. my first column is just a sequential number.

I can't copy the macro, any chance you can repeat it in plain form so I can past that into VBA?
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to add row to table in a Word form Macro help sought for Word table xan Word VBA 1 04-20-2012 07:50 PM
Macro to add row to table in a Word form Create Table Form from Word Document gsj224 Word VBA 3 07-31-2011 09:26 AM
Macro to add row to table in a Word form Word Macro That Checks a Check Box Form Field When File Print is Executed DKerne Word VBA 4 06-09-2011 11:54 AM
Do I need a Form or a Macro?? Keith24249 Excel 0 08-23-2010 06:23 AM
Use Form Checkbox to Add Table laxonator Word Tables 0 10-23-2008 06:21 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:58 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft