Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #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
 

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 05:33 PM.


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