![]() |
#1
|
|||
|
|||
![]()
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! ![]() |
#2
|
||||
|
||||
![]()
See the attached document for a demonstration.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
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 |
#4
|
||||
|
||||
![]()
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] |
#5
|
|||
|
|||
![]()
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! |
#6
|
||||
|
||||
![]()
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] |
#7
|
|||
|
|||
![]()
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
![]() |
#8
|
||||
|
||||
![]()
Yes, it makes a fundamental difference. Your reference to:
Quote:
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] |
#9
|
|||
|
|||
![]()
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 |
#10
|
||||
|
||||
![]()
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
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
![]()
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?
|
#12
|
||||
|
||||
![]()
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] |
#13
|
|||
|
|||
![]()
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)?
Last edited by macropod; 11-15-2018 at 02:29 PM. Reason: Claifying context added from https://answers.microsoft.com |
#14
|
||||
|
||||
![]()
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.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#15
|
|||
|
|||
![]()
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? |
![]() |
Thread Tools | |
Display Modes | |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
xan | Word VBA | 1 | 04-20-2012 07:50 PM |
![]() |
gsj224 | Word VBA | 3 | 07-31-2011 09:26 AM |
![]() |
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 |