我需要打开一个 Microsoft Word 2003 文件并更改其文件属性。例如更改摘要选项卡中的主题。
joe
问问题
2466 次
2 回答
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
于 2008-10-14T21:14:17.457 回答
5
我可以想到两种方法来做到这一点:
- 使用 Microsoft Office API。您必须在项目中引用它们,并且需要 Primary Interop Assemblies。
- 将文件转换为 Word 2003 XML 格式并更改 XML 文档中的值。这是有关文档属性的 MSDN 文档:http: //msdn.microsoft.com/en-us/library/aa223625 (office.11).aspx
如果可以的话,我会选择第二个选项,因为这样你就不必依赖系统上安装的 Word。
于 2008-10-14T17:07:20.547 回答