如何在C#中打开文件并更改其属性?

时间:2008-10-14 17:04:10

标签: c# ms-word

我需要打开Microsoft Word 2003文件并更改其文件属性。例如更改摘要选项卡中的主题。
alt text

2 个答案:

答案 0 :(得分:8)

Microsoft提供了一个名为DSOFile的非常有用的小程序集。通过在项目中引用它,您可以修改Office文档属性。它不一定会让你打开实际的Office文件的属性对话框,但你当然可以模拟它。

根据微软的说法:

  

Dsofile.dll文件允许您编辑   Office文档属性   没有安装Office

更多详情和下载链接可在http://support.microsoft.com/kb/224351

找到

这是我在很久以前使用的一些(非常旧的)VB代码片段。对不起,我还没有转换为C#,请注意它是类的一部分,因此有对实例变量的引用。尽管如此,它应该很容易理解并转化为您自己的需求:

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

答案 1 :(得分:5)

我可以想到两种方法:

如果可以,我会使用第二个选项,因为这样您就不必依赖于系统上安装的Word。