【问题标题】:how to use Word Object Model to programmatically replace text in a Word doc如何使用 Word 对象模型以编程方式替换 Word 文档中的文本
【发布时间】:2014-11-29 14:48:30
【问题描述】:

我的功能需要阅读 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

【问题讨论】:

    标签: asp.net vb.net com ms-word


    【解决方案1】:

    将 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

    【讨论】:

      猜你喜欢
      • 2019-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-04
      相关资源
      最近更新 更多