Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-12-2016, 06:34 AM
highrise955 highrise955 is offline Protecting Document via VBA Not Working Windows 10 Protecting Document via VBA Not Working Office 2013
Advanced Beginner
Protecting Document via VBA Not Working
 
Join Date: Mar 2016
Posts: 37
highrise955 is on a distinguished road
Default Protecting Document via VBA Not Working

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.Unprotect
This works perfectly.

The last line of the macro is supposed to protect the document again...
ActiveDocument.Protect wdAllowOnlyFormFields, True
However, it doesn't work. I tried this format as well because I saw it in some other code...
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True
But 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
The macro "AddTableRow" will be activated when the user is in a content control, if this makes a difference. I'm adding that info because the "ProtectForm()" works fine if I am in the VBA editor and I click Run - Run Sub/UserForm while the cursor is located inside the "ProtectForm()" code.
Reply With Quote
  #2  
Old 03-12-2016, 11:19 PM
macropod's Avatar
macropod macropod is offline Protecting Document via VBA Not Working Windows 7 64bit Protecting Document via VBA Not Working Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
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

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]
Reply With Quote
  #3  
Old 03-13-2016, 04:01 AM
highrise955 highrise955 is offline Protecting Document via VBA Not Working Windows 10 Protecting Document via VBA Not Working Office 2013
Advanced Beginner
Protecting Document via VBA Not Working
 
Join Date: Mar 2016
Posts: 37
highrise955 is on a distinguished road
Default

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

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
Here is my table in question...



"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.
Reply With Quote
  #4  
Old 03-13-2016, 04:16 AM
macropod's Avatar
macropod macropod is offline Protecting Document via VBA Not Working Windows 7 64bit Protecting Document via VBA Not Working Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
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

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:
the word "Pass" will always be in that cell
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 03-13-2016, 04:25 AM
highrise955 highrise955 is offline Protecting Document via VBA Not Working Windows 10 Protecting Document via VBA Not Working Office 2013
Advanced Beginner
Protecting Document via VBA Not Working
 
Join Date: Mar 2016
Posts: 37
highrise955 is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
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:
Yeah, I really don't need the "Evaluation" CC.

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.
Reply With Quote
  #6  
Old 03-13-2016, 04:48 AM
macropod's Avatar
macropod macropod is offline Protecting Document via VBA Not Working Windows 7 64bit Protecting Document via VBA Not Working Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
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

Quote:
Originally Posted by highrise955 View Post
I'm not sure what you mean by "structure" of my table.
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.
Quote:
Originally Posted by highrise955 View Post
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.
Rather than me re-writing the code, have you tried deleting the "Evaluation" content control?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 03-13-2016, 04:52 AM
highrise955 highrise955 is offline Protecting Document via VBA Not Working Windows 10 Protecting Document via VBA Not Working Office 2013
Advanced Beginner
Protecting Document via VBA Not Working
 
Join Date: Mar 2016
Posts: 37
highrise955 is on a distinguished road
Default

[quote=macropod;97351]
Quote:
Originally Posted by highrise955 View Post
I'm not sure what you mean by "structure" of my table.
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?
Sorry about that. After I posted the message I noticed my original pic of my table didn't come up right. I then modified the message with a link pic of the table.

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.
Reply With Quote
  #8  
Old 03-13-2016, 05:29 AM
macropod's Avatar
macropod macropod is offline Protecting Document via VBA Not Working Windows 7 64bit Protecting Document via VBA Not Working Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
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

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]
Reply With Quote
  #9  
Old 03-13-2016, 05:49 AM
highrise955 highrise955 is offline Protecting Document via VBA Not Working Windows 10 Protecting Document via VBA Not Working Office 2013
Advanced Beginner
Protecting Document via VBA Not Working
 
Join Date: Mar 2016
Posts: 37
highrise955 is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
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?
Yes, clicking and shift-tab work fine but since the users may be inputting over a couple of hundred rows I want to make it as seamless as possible.

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.
Reply With Quote
  #10  
Old 03-13-2016, 06:10 AM
highrise955 highrise955 is offline Protecting Document via VBA Not Working Windows 10 Protecting Document via VBA Not Working Office 2013
Advanced Beginner
Protecting Document via VBA Not Working
 
Join Date: Mar 2016
Posts: 37
highrise955 is on a distinguished road
Default

Quote:
Originally Posted by highrise955 View Post
Yes, clicking and shift-tab work fine but since the users may be inputting over a couple of hundred rows I want to make it as seamless as possible.

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.

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
...with this code...

Code:
If CCtrl.Tag <> "MethodInspect" Then Exit Sub
...and it works perfectly. Easier than I thought.
Reply With Quote
  #11  
Old 03-13-2016, 06:58 PM
macropod's Avatar
macropod macropod is offline Protecting Document via VBA Not Working Windows 7 64bit Protecting Document via VBA Not Working Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
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

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]
Reply With Quote
Reply

Tags
protect, unprotect, vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Protecting Document via VBA Not Working Spell out a number without protecting a document vera Word VBA 2 05-20-2016 07:35 AM
Protecting document brucemc777 Word 2 01-23-2016 02:38 PM
Protecting Document via VBA Not Working Cant type into content controls in a form after protecting document using macro 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

Other Forums: Access Forums

All times are GMT -7. The time now is 10:44 AM.


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