Jump to content
Xtreme .Net Talk

Recommended Posts

  • 2 weeks later...
  • 2 months later...
Posted

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 hasn't 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 aren't - they just have no properties.

 

Attached is the not so great Property reader (vb2005) (I'm 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...

 

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

DocInfo.zip

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...