查找和替换单词doc [英] Find and Replace in a word doc

查看:98
本文介绍了查找和替换单词doc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将VB设置为设置字符串并将其替换为单词doc。

I am trying to set VB to take a set string and replace it into a word doc.

当我运行代码时,文件打开但不替换文本:

When I am running the code, the file opens but does not replace the text:

Public Class ThisAddIn

    Private Sub ThisAddIn_Startup() Handles Me.Startup
        Me.Application.Documents.Open(FileName:="C:\Users\msmith2\Documents\testing.docx")

    End Sub
    Private Sub SearchReplace()
        Dim FindObject As Word.Find = Application.Selection.Find
        With FindObject
            .ClearFormatting()
            .Text = "template"
            .Replacement.ClearFormatting()
            .Replacement.Text = "Found"
            .Execute(Replace:=Word.WdReplace.wdReplaceAll)
        End With
    End Sub
    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown

    End Sub

End Class

推荐答案

您可能正在替换文字,但是, 我看不到你在哪里保存文件。 如果您在关闭它之前没有保存它, 它不会保留更改。

You may be replacing the text but,  i don't see where you are saving the document.  If you don't save it before closing it,  it will not keep the changes.

 此外, 你需要确保释放Word com对象,同时关闭文档和单词应用程序,如果你不想让它保持打开状态。 然而, 我没有看到你在哪里使应用程序可见,所以, 
你将有几个Word应用程序在不知情的情况下运行。 查看您的任务管理器,看看您是否有一堆运行尚未在代码中关闭的Word应用程序。

 Also,  you need to make sure you are releasing the Word com objects along with closing the document and the word application if you don't want it left open.  However,  i don't see where you are making the word application visible so,  you will have several Word applications running without even knowing it.  Look in your Task Manager and see if you have a bunch of Word Applications running that you have not closed in your code.

您可以使用  Find.Execute方法 查找和替换MS Word文档中的文本,如下所示。 此示例
显示了一个Sub,可以调用该Sub来替换文档中所有出现的指定文本。 它还允许您使用大小写匹配和整个单词选项。

You can use the Find.Execute method to find and replace text in an MS Word document as shown below.  This example shows a Sub that can be called to replace all occurrences of the specified text in the document.  It also allows you to use case matching and whole word options too.

Imports Microsoft.Office.Interop
Imports System.Runtime.InteropServices

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim MsDocFilename As String = "C:\TestFolder\MyDucument.docx"
        ReplaceWords(MsDocFilename, "SomeWord", "ReplaceWithThis", False, True)
        Me.Text = "Done"
    End Sub

    Private Sub ReplaceWords(Filename As String, TextToReplace As String, ReplaceWith As String, MatchCase As Boolean, WholeWord As Boolean)
        Dim oWord As New Word.Application
        Dim oDoc As Word.Document = oWord.Documents.Open(CObj(Filename))
        oDoc.Content.Find.Execute(FindText:=CObj(TextToReplace), ReplaceWith:=CObj(ReplaceWith), Replace:=Word.WdReplace.wdReplaceAll, MatchCase:=CObj(MatchCase), MatchWholeWord:=CObj(WholeWord))
        oDoc.Save()
        oDoc.Close()
        oWord.Quit()
        Marshal.ReleaseComObject(oDoc)
        Marshal.ReleaseComObject(oWord)
        oDoc = Nothing
        oWord = Nothing
    End Sub
End Class


这篇关于查找和替换单词doc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆