Search code examples
asp.netvb.netcomms-word

how to use Word Object Model to programmatically replace text in a Word doc


My function needs to read an MS Word doc, replace some text, and then SAVE AS PDF. Reading and saving PDF works great. But I can't figure out how to replace text by using Word Object Model.

Here is my word_to_PDF() function which does it all.

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

Solution

  • Change your wordDocument.Find to just Word.Find.

    Find is an interface on the Word namespace but not an object on the instanced class.

    Here's some msdn documentation about what you are trying to do: http://msdn.microsoft.com/en-us/library/f1f367bx.aspx

    What's in the Word namespace http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word%28v=office.14%29.aspx