我的功能需要阅读MS Word文档,替换一些文本,然后保存为PDF。 阅读和保存PDF效果很好。 但我无法弄清楚如何使用Word对象模型替换文本。
这是我的word_to_PDF()函数,可以完成所有操作。
Function word_to_PDF(Word_source_filepath, PDF_destin_filepath)
Dim wordApplication As ApplicationClass = New ApplicationClass()
Dim wordDocument As Document = Nothing
Dim paramExportFilePath As String = PDF_destin_filepath
Dim paramExportFormat As WdExportFormat = _
WdExportFormat.wdExportFormatPDF
Dim paramOpenAfterExport As Boolean = False
Dim paramExportOptimizeFor As WdExportOptimizeFor = _
WdExportOptimizeFor.wdExportOptimizeForPrint
Dim paramExportRange As WdExportRange = _
WdExportRange.wdExportAllDocument
Dim paramStartPage As Int32 = 0
Dim paramEndPage As Int32 = 0
Dim paramExportItem As WdExportItem = _
WdExportItem.wdExportDocumentContent
Dim paramIncludeDocProps As Boolean = True
Dim paramKeepIRM As Boolean = True
Dim paramCreateBookmarks As WdExportCreateBookmarks = _
WdExportCreateBookmarks.wdExportCreateWordBookmarks
Dim paramDocStructureTags As Boolean = True
Dim paramBitmapMissingFonts As Boolean = True
Dim paramUseISO19005_1 As Boolean = False
' Open the source document.
On Error Resume Next
wordDocument = wordApplication.Documents.Open(Word_source_filepath) ' Microsoft.Office.Interop.Word.Document
If Err.Number Then
Debug.WriteLine(Err.Description)
log_fault("word_to_PDF: Err# " & Err.Number & " " & Err.Description)
Stop '!!!
Else
' Export it in the specified format.
If Not wordDocument Is Nothing Then
Dim FindObject As wordDocument.Find = Application.Selection.Find <<<<<<<<<<<<<<< THIS GETS ERROR: "wordDocument.Find is not defined"
With FindObject
.ClearFormatting()
.Text = "find me"
.Replacement.ClearFormatting()
.Replacement.Text = "Found"
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With
wordDocument.ExportAsFixedFormat(paramExportFilePath, _
paramExportFormat, paramOpenAfterExport, _
paramExportOptimizeFor, paramExportRange, paramStartPage, _
paramEndPage, paramExportItem, paramIncludeDocProps, _
paramKeepIRM, paramCreateBookmarks, _
paramDocStructureTags, paramBitmapMissingFonts, _
paramUseISO19005_1)
Else
'!!! ERR
stop_if_debug()
End If
End If
' Close and release the Document object:
If Not wordDocument Is Nothing Then
wordDocument.Close(False)
wordDocument = Nothing
End If
' Quit Word and release the ApplicationClass object.
If Not wordApplication Is Nothing Then
wordApplication.Quit()
wordApplication = Nothing
End If
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
Return True
答案 0 :(得分:0)
将wordDocument.Find更改为Word.Find。
Find是Word命名空间上的接口,但不是实例类上的对象。
这里有一些关于你要做什么的msdn文档: http://msdn.microsoft.com/en-us/library/f1f367bx.aspx
Word命名空间中的内容 http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word%28v=office.14%29.aspx