.DOC is password protected?

Ho hum, was thinking of this as I was tinkering around with structured storage.
The properties for word docs are stored in structured storage and there is one called Security. You can read these properties from word - but you have to open the document first ... catch 22.
So, I implemented all the stuff you need to read the properties off without word (IPropertyStorage, IPropertySetStorage), and can now read the security property. Which is great - so long as the user hasnt checked the box to encrypt the document properties !!! arrrgh. Oh well. It is set to 1 when the doc is protected - and you can read it.
When the properties are scrambled, it appears as if there are no properties in the summaryInfo and documentInfo property sets. you could use this I suppose - but there is the possiblity of identifying documents as protected that arent - they just have no properties.

Attached is the not so great Property reader (vb2005) (Im making another one that will read all the properties and let you change them).

Here is the trial and error approach - which is far more sensible...

Code:
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Core
Imports Word = Microsoft.Office.Interop.Word

Public Class Form1

    Private Function IsDocProtected(ByVal filename As String) As Boolean

        Dim wordApp As Word.Application = Nothing
        Dim doc As Word.Document = Nothing
        Dim isProtected As Boolean = False
        Try
            wordApp = New Word.Application
             open: readonly, open/write password = "Wibble", show word = false
            doc = wordApp.Documents.Open(filename, True, , , "Wibble", , , "Wibble", , , , False)
            doc.Close(False)
        Catch ex As System.Runtime.InteropServices.COMException
             wrong password error:
            If ex.ErrorCode = &H800A1520 Then isProtected = True
        Finally
            GC.Collect()
            GC.WaitForPendingFinalizers()
            wordApp.Quit()
            Dim referenceCount As Integer = 0
            Do
                referenceCount = Marshal.ReleaseComObject(wordApp)
            Loop Until referenceCount < 1
            wordApp = Nothing
        End Try
        Return isProtected
    End Function

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ofd As New OpenFileDialog
        ofd.Title = "Select office document to open"
        Dim result As Windows.Forms.DialogResult
        result = ofd.ShowDialog
        If result = Windows.Forms.DialogResult.OK Then
            If ofd.FileName IsNot Nothing Then
                Me.Text = IsDocProtected(ofd.FileName)
            End If
        End If
    End Sub
End Class
 

Attachments

Back
Top