none
Lose the information about unprotected areas ("exceptions") when working in "hidden" mode RRS feed

  • Question

  • I have a protected Word document (ProtectionType = wdAllowOnlyReading) with several unprotected areas ("exceptions").
    If I`m working in "standard" mode - all is Ok (I can Unprotect document, make changes, save it, protect it again and get a document with the same unprotected areas as before). But if I use "hidden" mode (Application.Documents.Add(FileName, visible:=False)), information about unprotected areas ("exceptions") is lost after saving.

    ADD: In my view, in "hidden" mode, we lose the "Editors" collection. But how can I avoid this?
    Wednesday, October 29, 2014 1:37 PM

Answers

  • Hi AlexSh2,

    When you use Documents.Add method to add an invisible document, you can't use find or replece method, for example you can't use code like this:

    With Selection.Find
                .ClearFormatting
                .Text = "protected"
                .Replacement.ClearFormatting
                .Replacement.Text = "unprotected"
                .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
                If .Found = True Then MsgBox "OK"
            End With

    You'll find that .Found property is always false. This might be due to the mechanism of Find method. But you can edit the text in a specific range.

    doc.Range(0, 5).Text = "new text here"

    I don't know why the "Editors" of the new file are invisible, it could be a bug, but if you open it the second time, it'll be visible again.

    So the solution for you is to use Documents.Open instead of Documents.Add, use Range to edit the content of the document. Sample code here:

    Public Sub Edit_Invisible_Protected_Document()
        FileName = "D:\files\OfficeDev\test files\protected.docx"
        Dim doc As Word.Document
        Set doc = Application.Documents.Open(FileName, Visible:=False)
        'unprotect the document
        If doc.ProtectionType <> wdNoProtection Then
            doc.Unprotect "123"
        End If
        
        If doc.ProtectionType = wdNoProtection Then
            'replace text protected with unprotected
            'this won't work when visible=false
            With Selection.Find
                .ClearFormatting
                .Text = "protected"
                .Replacement.ClearFormatting
                .Replacement.Text = "unprotected"
                .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
                If .Found = True Then MsgBox "OK"
            End With
    
            'this will work when visible=false
            doc.Range(0, 5).Text = "new text here"
            
            'protect the document again
            doc.Protect Type:=wdAllowOnlyReading, Password:="123"
            
            doc.Close SaveChanges:=wdSaveChanges
            
            'save the document
            'doc.SaveAs FileName:="D:\files\OfficeDev\test files\saved.docx"
        End If
    End Sub


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by AlexSh2 Thursday, October 30, 2014 3:06 PM
    Thursday, October 30, 2014 9:39 AM
    Moderator