joe
joe

Reputation: 17478

How do I open a file in C# and change its properties?

I need to open a Microsoft Word 2003 file and change its file properties. Such as changing the Subject in the Summary Tab.
alt text

Upvotes: 3

Views: 2715

Answers (2)

AR.
AR.

Reputation: 40535

Microsoft provides a very useful little assembly called DSOFile. With a reference to it in your project, you can modify Office document properties. It won't necessarily let you open the actual Office file's properties dialog, but you could certainly simulate it.

According to Microsoft:

The Dsofile.dll files lets you edit Office document properties when you do not have Office installed

More details and a download link can be found at http://support.microsoft.com/kb/224351

Here's a snippet some (very old) VB code I used ages ago. Sorry I haven't converted to C# and be aware that it's part of a class so there are references to instance variables. Still, it should be pretty easy to understand and covert to your own needs:

Private Sub ProcessOfficeDocument(ByVal fileName As String)
    Dim docDSO As New DSOFile.OleDocumentPropertiesClass
    Dim docTitle, docModified, docAuthor, docKeywords As String
    Try
        docDSO.Open(fileName, True)
        Dim docSummary As DSOFile.SummaryProperties = docDSO.SummaryProperties
        docTitle = docSummary.Title
        docAuthor = docSummary.Author
        docKeywords = docSummary.Keywords
        docModified = CStr(docSummary.DateLastSaved)

        If (Not String.IsNullOrEmpty(docTitle)) Then
            _Title = docTitle
        End If

        If (Not String.IsNullOrEmpty(docAuthor)) Then
            _Author = docAuthor
        End If

        If (Not String.IsNullOrEmpty(docModified)) Then
            _DateModified = DateTime.Parse(docModified)
        End If

    Catch ex As Exception
        'Do whatever you need to do here...'
    Finally
        If (Not docDSO Is Nothing) Then
            docDSO.Close()
        End If
    End Try
End Sub

Upvotes: 8

ine
ine

Reputation: 14084

I can think of 2 ways to do this:

I would go with the second option if you can, because that way you don't have to depend on Word being installed on the system.

Upvotes: 5

Related Questions