![]() |
#1
|
|||
|
|||
![]()
Hi all,
I have a document populated with Content Controls in a table. I created a macro to add a row at the bottom of the table and populate the fields with additional content controls. Before adding the row the macro unprotects the document... ActiveDocument.UnprotectThis works perfectly. The last line of the macro is supposed to protect the document again... ActiveDocument.Protect wdAllowOnlyFormFields, TrueHowever, it doesn't work. I tried this format as well because I saw it in some other code... ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=TrueBut it still doesn't work. Any suggestions on what I might be doing wrong? ![]() EDIT: Here is the full code (please excuse the woefully inefficient coding, I am still a newbie in VBA)... Code:
Sub AddTableRow() ' ' AddTableRow Macro ' ' Dim tRows As Integer Dim objCC As ContentControl Call UnProtectForm Selection.Tables(1).Rows.Add Selection.EndKey Unit:=wdColumn 'Selection.Cells(1).Next.Select Selection.StartOf Unit:=wdRow tRows = Selection.Information(wdMaximumNumberOfRows) tRows = tRows - 8 Selection.TypeText tRows Selection.Cells(1).Next.Select Set objCC = ActiveDocument.ContentControls.Add(wdContentControlRichText) objCC.SetPlaceholderText Text:=" " objCC.Appearance = wdContentControlHidden Selection.Cells(1).Next.Select Set objCC = ActiveDocument.ContentControls.Add(wdContentControlPlainText) objCC.SetPlaceholderText Text:=" " objCC.Appearance = wdContentControlHidden Selection.Cells(1).Next.Select Set objCC = ActiveDocument.ContentControls.Add(wdContentControlPlainText) objCC.SetPlaceholderText Text:=" " objCC.Appearance = wdContentControlHidden Selection.Cells(1).Next.Select Set objCC = ActiveDocument.ContentControls.Add(wdContentControlPlainText) objCC.SetPlaceholderText Text:=" " objCC.Appearance = wdContentControlHidden Selection.Cells(1).Next.Select Set objCC = ActiveDocument.ContentControls.Add(wdContentControlPlainText) objCC.SetPlaceholderText Text:="PASS" objCC.Appearance = wdContentControlHidden Selection.StartOf Unit:=wdRow ThisDocument.NextCell Call ProtectForm End Sub Sub UnProtectForm() If ActiveDocument.ProtectionType <> wdNoProtection Then ActiveDocument.Unprotect End If End Sub Sub ProtectForm() If ActiveDocument.ProtectionType = wdNoProtection Then ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="" End If End Sub |
#2
|
||||
|
||||
![]()
Are you sure which document the code is running against? You're using a mixture of Selection, ThisDocument and ActiveDocument calls and I can't tell from what you've posted if they're all referencing the same document.
You might want to check out: https://www.msofficeforums.com/word-...html#post87989
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]() Quote:
I utilized your code in the link you provided, minus the recommended modifications since there is only one table in my document. I do have a question about it though. Here it is... Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean) 'The following code conditionally adds a new row, with content controls, to the designated table. Dim i As Long, j As Long, Prot As Variant Dim tRows As Integer Const Pwd As String = "" 'Insert password (if any) here With CCtrl 'Check that the Content Control is within our table. If .Range.InRange(ActiveDocument.Tables(1).Range) = False Then Exit Sub 'Get the number of ContentControls in the table i = .Range.Tables(1).Range.ContentControls.Count 'Get our ContentControl's index # in the table j = ActiveDocument.Range(.Range.Tables(1).Range.Start, .Range.End).ContentControls.Count 'Check that we're using the last content control If i <> j Then Exit Sub End With 'Solicit user input If MsgBox("Add new row?", vbQuestion + vbYesNo) <> vbYes Then Exit Sub With ActiveDocument ' Un-protect the document, if applicable Prot = .ProtectionType If .ProtectionType <> wdNoProtection Then Prot = .ProtectionType .Unprotect Password:=Pwd End If With Selection.Tables(1).Rows 'Insert an empty paragraph after our table, then replace it with a replica of the last row With .Last.Range .Next.InsertBefore vbCr .Next.FormattedText = .FormattedText End With 'Reset all content controls in the new last row For Each CCtrl In .Last.Range.ContentControls With CCtrl If .Type = wdContentControlCheckBox Then .Checked = False If .Type = wdContentControlRichText Or .Type = wdContentControlText Then .Range.Text = "" If .Type = wdContentControlDropdownList Then .DropdownListEntries(1).Select If .Type = wdContentControlComboBox Then .DropdownListEntries(1).Select If .Type = wdContentControlDate Then .Range.Text = "" End With Next End With ' Go to first cell in new row and change the number listed. Selection.EndKey Unit:=wdColumn Selection.StartOf Unit:=wdRow tRows = Selection.Information(wdMaximumNumberOfRows) tRows = tRows - 8 Selection.Delete Selection.TypeText tRows ' Re-protect the document, if applicable .Protect Type:=Prot, Password:=Pwd End With End Sub ![]() "Bubble" column is just a number (no CC) that needs to increment by 1 for each new row. (The way I am currently doing it is to get the number of rows and minus the number of rows in the header section of the table. (I'm sure this is inefficient but it gets the job done until I learn the "proper" way of doing it.) "Required Dimension" is a RichText CC. "Loc.", "Actual Dimension", and "Method of Inspection" are PlainText CC's "Evaluation" is a PlainText CC which could just as easily be text (no CC) since the word "Pass" will always be in that cell. I can only get your code to run if I Shift-Tab out of the Evaluation cell since it is the last CC in the document. I would prefer it to run when the user Tabs out of the "Method of Inspection" cell. Can you offer a hint on how I would do that? I appreciate your assistance with this and my previous submission you helped me with. |
#4
|
||||
|
||||
![]()
The code is designed to run when you exit the last content control in the table (whose structure is impossible to determine from your post), regardless of how you exit it. Shift-Tab will take you to another content control, but you could just as easily use the mouse to click anywhere else in the document. As for your "Evaluation" content control, I can't see the point in having it if:
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
![]() Quote:
![]() I'm not sure what you mean by "structure" of my table. Could I modify your code so it would only activate when the user exited the "Method Of Inspection" CC? I assume I would do some kind of "SelectContentControlByTag" method but I'm not sure where to put it or how to do it without mucking up your code. ![]() |
#6
|
||||
|
||||
![]()
I meant precisely what I said. The text you originally posted to depict your table gave no indication of its structure. I see you have since modified the post to include an image of the table.
Rather than me re-writing the code, have you tried deleting the "Evaluation" content control?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
![]()
[quote=macropod;97351]
Quote:
Yes, I tried removing the "Evaluation" CC but just like before, the Tab key does nothing (meaning it doesn't move out of the CC it is residing in) since I am in the last CC of the document. |
#8
|
||||
|
||||
![]()
By design, you can't tab out of the last content control in a document. Did you try clicking with the mouse anywhere outside the content control, as I suggested in post #4?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
![]() Quote:
I'm sure there must be a way to add a new table row when exiting the "Method of Inspection" CC, I just need to figure it out. Thanks for your help. |
#10
|
|||
|
|||
![]() Quote:
OK, I figured it out. I just replaced this code... Code:
With CCtrl 'Check that the Content Control is within our table. If .Range.InRange(ActiveDocument.Tables(1).Range) = False Then Exit Sub 'Get the number of ContentControls in the table i = .Range.Tables(1).Range.ContentControls.Count 'Get our ContentControl's index # in the table j = ActiveDocument.Range(.Range.Tables(1).Range.Start, .Range.End).ContentControls.Count 'Check that we're using the last content control If i <> j Then Exit Sub 'End With Code:
If CCtrl.Tag <> "MethodInspect" Then Exit Sub ![]() |
#11
|
||||
|
||||
![]()
The problem with that approach is that it will trigger the macro on any field titled 'MethodInspect' - which means that content control in any row. You might get better results by changing:
i = .Range.Tables(1).Range.ContentControls.Count to: i = .Range.Tables(1).Range.ContentControls.Count -1
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
![]() |
Tags |
protect, unprotect, vba |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
vera | Word VBA | 2 | 05-20-2016 07:35 AM |
Protecting document | brucemc777 | Word | 2 | 01-23-2016 02:38 PM |
![]() |
rgburridge | Word VBA | 4 | 01-27-2015 02:37 PM |
Disabling content controls and protecting document sections. | Catty | Word VBA | 2 | 11-29-2013 05:10 AM |
Protecting Word Document by restricting access permissions! | user | Word | 0 | 11-20-2008 01:21 PM |