How to update properties (summary and custom properties) of a word document without using the word API?
With the help of Dsofile.dll provided by Microsoft, you can easily modify properties of a document when Microsoft Office is not installed in the machine. The link is below -
http://support.microsoft.com/kb/224351
For your convenience, I am putting some code below. I worte the code for a sharepoint project to remove document properties. Hope it helps.
Imports System.IO
Public Class RemoveAllProperty
Private m_oDocument As DSOFile.OleDocumentPropertiesClass
Public Function RemoveProperties(ByVal oStream As Stream) As MemoryStream
‘get the temporay file name along with the path
Dim sFileName As String = Path.GetTempFileName()
Dim oFileStream As System.IO.FileStream
oFileStream = New System.IO.FileStream(sFileName, System.IO.FileMode.Create, FileAccess.ReadWrite)
‘copy the stream
CopyStream(oStream, oFileStream)
oStream.Close()
oFileStream.Close()
oFileStream.Dispose()
Dim oSummProps As DSOFile.SummaryProperties
Dim oCustProp As DSOFile.CustomProperty
Dim sFile As String
sFile = sFileName
m_oDocument = New DSOFile.OleDocumentPropertiesClass
m_oDocument.Open(sFile, False, DSOFile.dsoFileOpenOptions.dsoOptionDefault)
oSummProps = m_oDocument.SummaryProperties
Try
‘remove all the summary properties value
oSummProps.Title = “”
oSummProps.Author = “”
oSummProps.Subject = “”
oSummProps.Manager = “”
oSummProps.Company = “”
oSummProps.Comments = “”
oSummProps.Category = “”
oSummProps.Keywords = “”
Catch ex As Exception
Microsoft.Office.Server.Diagnostics.PortalLog.LogString(”Exception Occurred: {0} || {1}”, ex.Message, ex.StackTrace)
End Try
Dim oRmProp As DSOFile.CustomProperties
oRmProp = m_oDocument.CustomProperties
‘remove all the custom properties value
For Each oCustProp In m_oDocument.CustomProperties
Try
oCustProp.Remove()
Catch ex As Exception
Microsoft.Office.Server.Diagnostics.PortalLog.LogString(”Exception Occurred: {0} || {1}”, ex.Message, ex.StackTrace)
End Try
Next oCustProp
m_oDocument.Save()
m_oDocument.Close()
m_oDocument = Nothing
‘After removing the properties, read the file from the temp directory and read the file into a byte array and return
Dim oFile As System.IO.FileInfo
oFile = New System.IO.FileInfo(sFile)
‘oFileStream = New System.IO.FileStream(sFileName, System.IO.FileMode.Open)
Try
oFileStream = New System.IO.FileStream(sFileName, System.IO.FileMode.Open, FileAccess.Read, FileShare.Read)
Catch ex As Exception
Microsoft.Office.Server.Diagnostics.PortalLog.LogString(”Exception Occurred: {0} || {1}”, ex.Message, ex.StackTrace)
End Try
Dim oMS As New System.IO.MemoryStream()
‘copy stream to memory stream
CopyStream(oFileStream, oMS)
oFileStream.Close()
oFileStream.Dispose()
‘after the read the temporary file into a byte array, delete the temporary file from temp directory
oFile.Delete()
Return oMS
‘Return oFileStream
End Function
‘copy stream from one stream to another
Private Sub CopyStream(ByVal readStream As Stream, ByVal writeStream As Stream)
Dim Length As Integer = 256
Dim buffer As Byte() = New Byte(Length - 1) {}
Dim bytesRead As Integer = readStream.Read(buffer, 0, Length)
‘ write the required bytes
While bytesRead > 0
writeStream.Write(buffer, 0, bytesRead)
bytesRead = readStream.Read(buffer, 0, Length)
End While
‘readStream.Close()
‘writeStream.Close()
End Sub
End Class
Comments