Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-25-2021, 10:54 AM
ananas1171 ananas1171 is offline How do I on MS Word VBA add and remove rows in the tables using command button macros? Windows 10 How do I on MS Word VBA add and remove rows in the tables using command button macros? Office 2016
Novice
How do I on MS Word VBA add and remove rows in the tables using command button macros?
 
Join Date: Jun 2021
Posts: 5
ananas1171 is on a distinguished road
Default 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.
Attached Images
File Type: png Capture.PNG (34.5 KB, 20 views)
Reply With Quote
  #2  
Old 06-25-2021, 08:36 PM
gmayor's Avatar
gmayor gmayor is offline How do I on MS Word VBA add and remove rows in the tables using command button macros? Windows 10 How do I on MS Word VBA add and remove rows in the tables using command button macros? Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 06-28-2021, 12:01 PM
ananas1171 ananas1171 is offline How do I on MS Word VBA add and remove rows in the tables using command button macros? Windows 10 How do I on MS Word VBA add and remove rows in the tables using command button macros? Office 2016
Novice
How do I on MS Word VBA add and remove rows in the tables using command button macros?
 
Join Date: Jun 2021
Posts: 5
ananas1171 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
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


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?
Attached Images
File Type: png Remove column.PNG (33.3 KB, 14 views)
File Type: png Add Column.PNG (45.8 KB, 14 views)
Reply With Quote
  #4  
Old 06-28-2021, 08:38 PM
gmayor's Avatar
gmayor gmayor is offline How do I on MS Word VBA add and remove rows in the tables using command button macros? Windows 10 How do I on MS Word VBA add and remove rows in the tables using command button macros? Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #5  
Old 06-30-2021, 05:31 AM
ananas1171 ananas1171 is offline How do I on MS Word VBA add and remove rows in the tables using command button macros? Windows 10 How do I on MS Word VBA add and remove rows in the tables using command button macros? Office 2016
Novice
How do I on MS Word VBA add and remove rows in the tables using command button macros?
 
Join Date: Jun 2021
Posts: 5
ananas1171 is on a distinguished road
Default 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.
Attached Images
File Type: png Run Time error.PNG (110.4 KB, 11 views)
Reply With Quote
  #6  
Old 06-30-2021, 06:27 AM
ananas1171 ananas1171 is offline How do I on MS Word VBA add and remove rows in the tables using command button macros? Windows 10 How do I on MS Word VBA add and remove rows in the tables using command button macros? Office 2016
Novice
How do I on MS Word VBA add and remove rows in the tables using command button macros?
 
Join Date: Jun 2021
Posts: 5
ananas1171 is on a distinguished road
Default 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.
Attached Images
File Type: png error.PNG (112.6 KB, 11 views)
Reply With Quote
  #7  
Old 06-30-2021, 07:51 AM
ananas1171 ananas1171 is offline How do I on MS Word VBA add and remove rows in the tables using command button macros? Windows 10 How do I on MS Word VBA add and remove rows in the tables using command button macros? Office 2016
Novice
How do I on MS Word VBA add and remove rows in the tables using command button macros?
 
Join Date: Jun 2021
Posts: 5
ananas1171 is on a distinguished road
Default

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.
Attached Images
File Type: png Macro editing.PNG (34.0 KB, 11 views)
Reply With Quote
Reply



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
How do I on MS Word VBA add and remove rows in the tables using command button macros? 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
How do I on MS Word VBA add and remove rows in the tables using command button macros? Word VBA: How to create a command button and trigger a function? tinfanide Word VBA 2 12-02-2011 05:51 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:48 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