VBA Word copy full content with Restricted editing

Copper Contributor

My question is, in vba, how can I copy an opened full word document content to the current word document, with copying EVERYTHING? No matter what I tried, I could not copy the Restrict Editing, only the rest of the content. I created the document. However, I would also like to copy these restrictions, but I can't. I have the right to do it, but the restrictions are not created in the copy. I want to save the structure and code of the current file menu with the content from the old, pasted from another file.

 

I also tried this, it copies everything, but the restricted edit disappears.

    oWord.Activate
    oWord.Selection.WholeStory 'Select whole document
    oWord.Selection.Expand wdParagraph 'Expands your selection to current paragraph
    oWord.Selection.Copy 'Copy your selection
    ThisDocument.Activate
    Selection.EndKey wdStory 'Move to end of document
    Selection.Paste  'Pastes in the content
13 Replies

@Kondorosy 

 

You can have the macro turn off the restriction, temporarily, and then copy and paste.

@Stefan_Blom That's exactly what I did, but it doesn't work well, because although it copies everything, it just doesn't copies the mark of the Restricted Edit sections.

@Kondorosy 

 

You may have to share the code you are using, for someone to be able to provide a workaround. 

oWord.Activate 'OWord is the afterwards opened file
oWord.Selection.WholeStory 'Select whole document
oWord.Selection.Expand wdParagraph 'Expands my selection to current paragraph
oWord.Selection.Copy 'Copy my selection
ThisDocument.Activate 'This is where I want to copy, my original file
Selection.EndKey wdStory 'Move to end of document
Selection.Paste 'pasteing, it's succeed, but without Restricted edit's part
Or this is another solution
Dim oDoc As Document
Dim oTarget As Document

Dim oRng As Range
Set oDoc = oWB
Set oTarget = ThisDocument
Set oRng = oTarget.Range
oRng.FormattedText = oDoc.Range.FormattedText

Unfortunately, the last line of this throws an error message:
"4218 This object interface is not supported"

@Kondorosy 

 

Is the document restricted/protected, for example using the "Filling in forms" type of protection? In that case, you will have to include code that temporarily turns off the protection. There is an example in the article at https://wordmvp.com/FAQs/MacrosVBA/SpellcheckProtectDoc.htm (which includes code that turns off document protection, performs spellchecking, and then turns the protection back on). 

 

However, you may be using a more "modern" type of protection, such as content controls. Maybe someone in the forum will be able to suggest an approach that will work in such a case. 

The document is protected, of course, and before I made any changes I turned the protection off and then back on. Otherwise I use content controls, but unfortunately this does not solve the problem.
Try an IncludeText field.

@Kondorosy 

 

Which specific code is being used to unprotect and reprotect the document?

 

Note that document protection is only required if you are NOT using content controls. With content controls, you can lock areas of a document using the Group facility on the Developer tab.

 

I guess it is possible that the mix of form fields and content controls could be the issue here, but I am not sure. Charles will know more about this. 

@Stefan_Blom 

I doubt I know more about anything to do with Word than you do.

  • I know enough to not mix Content Controls and legacy formfields. Results can be chaotic.
  • I know enough to eschew use of Filling In Forms protection for anything other than legacy formfields. It is way too restrictive of other Word features.
  • I know enough to use Content Controls unless I absolutely need to use legacy formfields, and to think about it if I think I need the legacy formfields. Content Controls can be very flexible and powerful, especially if mapped.

The Insert Text From Document command can overcome Filling In Forms protection. This has been known since Word 97, I believe. I do not know how to write it into a vba procedure. I still do this with forms published by our court system so that I can use them with my client data system and I am not having to type in my name and information every time I use one.

 

First, try using the Insert Text method manually to see if it meets your needs. Then if it does, figure out the vba.

I apologize.
It is Insert > Object > Text from file that I use, not the field, although I expect the field would work. It pulls everything from a document protected for filling in forms.

 

It does not pull the page layout nor headers/footers. It does not pull in styles, at least if the same styles exist in the document receiving the text; in that case, it uses the style definitions in the receiving document.

Unlock works with the following code:

If ThisDocument.ProtectionType = wdAllowOnlyReading Then
ThisDocument.Unprotect Password:=Password
End If
Content controls are cool, but if you want to make sure that the user doesn't mess up the table or write where they can't, for example, content controls can't help. Or am I wrong?
Thank you for your detailed reply! I don't use legacy formfields nor Filling In Forms protection. Only the restricted edit function. This gives the user the ability to only write to the selected parts of the document, and not have the document format fall apart if the user writes or deletes in the wrong place. Unfortunately I don't understand why Isnert Text would help me?