|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
How do I on MS Word VBA add and remove rows in the tables using command button macros?
have spent hours looking for a code that would work, but with limited understanding (0 previous knowledge) I am struggling to apply anything that is currently on the web. Basically I have a table 2X4 (row x column). First row is titles and second row is where information is populated. Column 4 will contain a drop down option after the first row. My form requires that if needed to add a row or remove a row with a click of a command button containing the drop down option in column 4. |
#2
|
||||
|
||||
Based on your screenshot
Code:
Private Sub CommandButton1_Click() Dim oCC As ContentControl Dim oTable As Table Dim oRng As Range Set oTable = ActiveDocument.Tables(1) For Each oCC In oTable.Range.ContentControls oCC.LockContentControl = False Next oCC oTable.Rows.Last.Range.Copy Set oRng = oTable.Range oRng.Collapse 0 oRng.Paste oRng.ContentControls(1).DropdownListEntries.Item(1).Select For Each oCC In oTable.Range.ContentControls oCC.LockContentControl = True Next oCC Set oCC = Nothing Set oTable = Nothing Set oRng = Nothing End Sub Private Sub CommandButton2_Click() Dim oCC As ContentControl Dim oTable As Table Set oTable = ActiveDocument.Tables(1) For Each oCC In oTable.Range.ContentControls oCC.LockContentControl = False Next oCC oTable.Rows.Last.Delete For Each oCC In oTable.Range.ContentControls oCC.LockContentControl = True Next oCC Set oCC = Nothing Set oTable = Nothing End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Quote:
Thanks for the response, this worked like a charm. I am experiencing issues when I try to restrict the form and to only allow fill out information in certain fields. When I lock the form and click add or remove row I receive the following; Run time error '4605', This command is not available. Based on the your previous posting on other links, I saw that you had a code regarding restrictions of form editing. I am guessing there has to be a code for password. I am trying to figure out how to apply this. Would be able to help me again? |
#4
|
||||
|
||||
Ideally a form with content controls should be protected as readonly with editors added to the content controls, but you can protect it for forms. The code for both is included
Code:
Option Explicit Const sPassword As String = "password" Private oCC As ContentControl Private oTable As Table Private oRng As Range Private Sub CommandButton1_Click() If Not ActiveDocument.ProtectionType = wdNoProtection Then ActiveDocument.Unprotect sPassword End If Set oTable = ActiveDocument.Tables(3) For Each oCC In oTable.Range.ContentControls oCC.LockContentControl = False Next oCC oTable.Rows.Last.Range.Copy Set oRng = oTable.Range oRng.Collapse 0 oRng.Paste oRng.ContentControls(1).DropdownListEntries.Item(1).Select For Each oCC In ActiveDocument.Range.ContentControls oCC.LockContentControl = True ' oCC.Range.Editors.Add (wdEditorEveryone) Next oCC 'ActiveDocument.Protect wdAllowOnlyReading, , sPassword ActiveDocument.Protect wdAllowOnlyFormFields, , sPassword Set oCC = Nothing Set oTable = Nothing Set oRng = Nothing End Sub Private Sub CommandButton2_Click() If Not ActiveDocument.ProtectionType = wdNoProtection Then ActiveDocument.Unprotect sPassword End If Set oTable = ActiveDocument.Tables(3) For Each oCC In oTable.Range.ContentControls oCC.LockContentControl = False Next oCC oTable.Rows.Last.Delete For Each oCC In ActiveDocument.Range.ContentControls oCC.LockContentControl = True 'oCC.Range.Editors.Add (wdEditorEveryone) Next oCC ActiveDocument.Protect wdAllowOnlyFormFields, , sPassword 'ActiveDocument.Protect wdAllowOnlyReading, , sPassword Set oCC = Nothing Set oTable = Nothing End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
|||
|
|||
Run Time error
I have used the code the you have provided but still am experiencing a problem. When I run the macro I get Run-time Error '13' Type Mismatch. I tried to find a way to correct it online but have not had much luck to make changes that would work.
|
#6
|
|||
|
|||
Error
I kept playing around and this time a different row is being highlighted. I am guessing the previous image is related to this one.
|
#7
|
|||
|
|||
Sorry for spamming.
Alright so.... 1) if I use filling in forms restriction then I need to add content control in every cell of the table like in the image I attached because if I lock the "filling in forms" then I will be unable to make changes to the cells. I tried editing the code using your code as the bases but everything got even worse. I saw that your code had content control (1) so my guess was that if I add 3 more text boxes then I should have CC 2,3 and 4? 2) In my response above, I was unable to solve the run-time error "13", type mismatch. I tried changing the commands but that also ended up crashing the code completely. 3) I found a way to use your original code and when I do lock the form I just select restrictions for "comments" and I select the areas that I want to edit. It is not ideal mainly because when I add the row it takes the previous filled in text and pastes it into new row and I want column 1,2,3 to be blank and only copy the drop options in column 4. It is not idea but it is better than not having anything at all for now. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Remove visible command button when attaching in outlook | mfiltvedt84 | Outlook | 11 | 10-02-2018 04:33 PM |
Show table list by using chek box command and save document on sharepoint by using command button | Zhibek | Word VBA | 3 | 08-16-2018 06:19 PM |
Print only specified pages of word by 1 click of command button using vba | Zhibek | Word VBA | 9 | 08-12-2018 11:44 PM |
Word 2013 ActiveX Command Button Failure | irallfml@gmail.com | Word VBA | 1 | 05-23-2014 04:19 PM |
Word VBA: How to create a command button and trigger a function? | tinfanide | Word VBA | 2 | 12-02-2011 05:51 AM |