PDA

View Full Version : Cant write into FormFields after apply ActiveDocument.Prtotect wdAllowOnlyFormFields



gallay74
05-26-2019, 11:58 PM
Hi

I created a word file from nomal dot, adding a RichText ContentControl (with Default Properties) and a Command Button with following Code:




Private Sub CommandButton1_Click()

If ActiveDocument.ProtectionType <> wdNoProtection Then ActiveDocument.Unprotect
Application.ActiveDocument.Protect wdAllowOnlyFormFields, Password:=""
End Sub



When click on the command button, it is not possible, to write into the RichText ContentControl-Form Field.


When closing and opening the word file, the FormField gets editable again. Any idea, what goes wrong?


PS: I reduced the problem to a minimal example. The real workflow is: Start with a protected word file, which contains a table. The word file contains a button which is linked to a macro to add a new row to the table (i.e. unprotect docuement, add row, protect document again) to insert then new information into the formfields.



PS2: I use Microsoft Office Professional Plus 2010

macropod
05-27-2019, 01:44 AM
A RichText Content Control is not a Formfield. Since your document contains no code for updating the content control, it's impossible to know what the cause of your problem is.

gallay74
05-27-2019, 02:33 AM
Thank you for your quick answer, I see, that I mixed up ControlContents and FormFields.



Thus, I have to ask my question in a different way: I want to add in a protected document a ControlContent with a macro, so that i can put some text in the ControlContent afterwards. The code of the macro would be:



If ActiveDocument.ProtectionType <> wdNoProtection Then ActiveDocument.Unprotect
Set objCC = ActiveDocument.ContentControls.Add(wdContentControlRichText)
objCC.Title = "Comments"
objCC.SetPlaceholderText , , "Comments"
Application.ActiveDocument.Protect wdAllowOnlyFormFields, Password:=""


Obviously this does not work. Thus, the question is now, can I protect the word file with vba in such a way, that the user can put text into, e.g. RichText-ControlContent some text?

macropod
05-27-2019, 02:49 AM
Why would you be adding the content control with code, rather than having one already in position?

gallay74
05-27-2019, 03:38 AM
The macro adds a new row to an existing table. The content of the new row ist a Dropdown CC, datepicker CC, and finalliy I Need an object for free text Input. Since the number of rows cannot be predected, it must be possibe to add them at "runtime".

macropod
05-27-2019, 04:06 AM
See:
http://www.msofficeforums.com/word-vba/27809-code-add-new-row-table.html#post87989
http://www.msofficeforums.com/word-vba/13955-macro-add-row-table-word-form.html#post38461