Max Hersey
asked on
Remove Content Control with VBA Coding with restricted Editing.
Hey, I need help with the VBA coding of this word document. What I want it to do is remove the content control wherever there is text. And because the document with be restricted to only content control editing, it will essentially lock the content in. I cant seem to get my code to actually delete the content controls where there is text thought. If anyone can help I would greatly appreciate it!!
Thanks,
Max
Client-Journal-Template.docm
Thanks,
Max
Client-Journal-Template.docm
You didn't attach the document.
ASKER
Oh ok, I have attached it now. Thanks!
I'm not really a Word person but I can probably figure this out. Do you have code in the document where you tried to delete the content control? Also, why do you want to delete the control rather than just clear it?
ASKER
Hey yes I did attempt to do the coding but Im not very good at it. The reason why I wanted to delete the CC is because I plan on having the document on Restricted editing so that the only thing that can be edited is content control areas. Then I want to be able to make the text that my staff put in permanent so if the CC is deleted and the text remains then it can not be edited. I am trying to make it so that once a staff member inputs information and "locks" it, no one else can change it.
If you do delete the content control, how would you put it back?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Oh awesome! I tried locking the CC as well like you did on line 8 an 9 but it was definitely line 7 that I had done wrong. Thanks so much!
I'm happy I was able to help.
ASKER
I just notice that it is locking the CC's that do not have text in them. Is there a way to just lock the CC's that have text added?
Oh, yes, you're right! And it's because all of them have text. I'll come back to this after I finish our other question.
Replace these two subs.
Private Sub CommandButton1_Click()
Dim objCC As ContentControl
Application.ActiveDocument.Unprotect
For Each objCC In ActiveDocument.ContentControls
Select Case Trim(objCC.Range.Text)
Case "AA", "Please enter your comments here.", "00:00am/pm", _
"Please enter your comments here.", "Choose a description."
' Ignore these
Case Else
objCC.LockContentControl = True
objCC.LockContents = True
End Select
Next
Application.ActiveDocument.Protect wdAllowOnlyFormFields
End Sub
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Application.ActiveDocument.Unprotect
With ContentControl.Range
If ContentControl.Title = "Status" Then
If ContentControl.LockContents = False Then
Select Case .Text
Case "No issues during shift"
.Cells(1).Shading.BackgroundPatternColor = wdColorBlue
Case "Questions for RS about client"
.Cells(1).Shading.BackgroundPatternColor = wdColorGreen
Case "Issues with client during shift"
.Cells(1).Shading.BackgroundPatternColor = wdColorOrange
Case "Incident report filled out during shift"
.Cells(1).Shading.BackgroundPatternColor = wdColorRed
Case Else
.Cells(1).Shading.BackgroundPatternColor = wdColorAutomatic
End Select
End If
End If
End With
Application.ActiveDocument.Protect wdAllowOnlyFormFields
End Sub
ASKER
Oh that's awesome! I also agree with the fact that we only need one locking button. Is there a way to put it on the Ribbon?
I'm sure there is but someone else will probably need to answer that in a new question.
ASKER
ok sounds good thanks!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.