Avatar of Max Hersey
Max HerseyFlag for Canada

asked on 

VBA remove Content Control on restricted editing file

Hey,

I am trying to make a Microsoft word document that allows users not to edit content once the content controls are removed. My plan to do this was by restricting the editing to only filling in forms and then creating a code that removes the content control anywhere that there is text value. I am new to VBA and cant get the code to work properly. I can get it to remove the content controls when they are empty but when I try to remove them with content they do not disappear. I have attached the document that I cant get to work.

If anyone can help me that would be fantastic!

Thanks,

Max
Client-Journal-Template.docm
* JournalingVBAMicrosoft WordMicrosoft OfficeVisual Basic.NET

Avatar of undefined
Last Comment
Max Hersey
Avatar of irudyk
irudyk
Flag of Canada image

Try modifying you code by changing:
Private Sub CommandButton1_Click()
Application.ActiveDocument.Unprotect
Dim cc As ContentControl
    For Each cc In ActiveDocument.SelectContentControlsByTag("1")
        If cc.Range.Text <> Null Then
                ParentContentControl.LockContentControl = True
                ParentContentControl.LockContents = True
            Else
            
        End If
    Next
Application.ActiveDocument.Protect wdAllowOnlyFormFields
End Sub

Open in new window

to:
Private Sub CommandButton1_Click()
If ActiveDocument.ProtectionType <> wdNoProtection Then Application.ActiveDocument.Unprotect
Dim cc As ContentControl
    For Each cc In ActiveDocument.SelectContentControlsByTag("1")
        If cc.Range.Text <> cc.PlaceholderText Then
                cc.LockContentControl = True
                cc.LockContents = True
            Else
        End If
    Next
Application.ActiveDocument.Protect wdAllowOnlyFormFields
End Sub

Open in new window

Avatar of irudyk
irudyk
Flag of Canada image

Hi,
Any luck with the revised code posted above?
Avatar of Max Hersey
Max Hersey
Flag of Canada image

ASKER

Hmm Im not too sure how to use this coding.  I have attached the document for your viewing. The goal is to have it so once text is added and then the document is locked, then the text can not be changed. If you know a better way to do this then please let me know. Im essentially just trying to make a document dynamically locks information that is imputed. Also, it wants to open in view document. rather than edit? is there a way to have it open ready to be edited?
Client-Journal-Template.docm
Avatar of irudyk
irudyk
Flag of Canada image

Okay, the coding already exists in you template document which is what I changed.

But I see that you've since updated the code and removed the button in the document to Lock content which was linked to the code I updated.

So, it looks like you've decided to change things up from the document/code you originally posted. Given that, I can see why you'd not be sure how to use the coding I posted (since it is of no use now given those changes).

So, what I think you still need is a button to lock the document. To dynamically lock the information as it is inputted will aggravate your users since if they make a mistake/typo on the entry they'll have no way to correct it once they exit the control.

As to you last statement, when I open the document it is in form mode which is what I think you'd want your users to see/be in.

See the revised document and let me know if it is what you are looking for.
Client-Journal-Template.docm
Avatar of Max Hersey
Max Hersey
Flag of Canada image

ASKER

Oh ok I understand now. I know it will be aggravating for them not to be able to edit the document once they lock it but thats how I want it as it because this is part of our legal requirements. I like what you did here but is it possible to have the lock button either follow the page if you scroll down or to put it as part of the ribbon? That way they don't have to scroll up to the top to lock it. Also, with how it opens I would like it to always open in edit mode as my staff will be opening this at least 3 times a day to write their shift comments. So instead of this: User generated image Could we get this?User generated image  

Also i noticed that Enter Shift Time is not working with the coding for some reason.
Avatar of irudyk
irudyk
Flag of Canada image

See if the attached revised document works for you.
Client-Journal-Template.docm
Avatar of Max Hersey
Max Hersey
Flag of Canada image

ASKER

Hey yes I believe everything works well except when I went down a couple tables and filled text in, it would not lock it when I hit the bottom lock doc button. It may be less confusing for my staff if we just have it on the ribbon somewhere.
ASKER CERTIFIED SOLUTION
Avatar of irudyk
irudyk
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Max Hersey
Max Hersey
Flag of Canada image

ASKER

Fantastic, That seems to be working very well!
Avatar of Max Hersey
Max Hersey
Flag of Canada image

ASKER

I really like this workbook now but I would like to add a few more rows to each table and have 4 cells per row. I can do this Myself if you can change the coding so that it stops restricting editing every time i click something. Once I have made the adjustments you can change it back to how you have it.
Visual Basic Classic
Visual Basic Classic

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.

165K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo