【问题标题】:Select word from MS Word through VB.NET通过 VB.NET 从 MS Word 中选择单词
【发布时间】:2015-06-05 07:33:57
【问题描述】:

我想通过 VB.NET (Microsoft.Office.Interop.Word) 从 Ms Word 中选择特定单词。

知道怎么做吗?

已编辑: 问题是我找不到/替换超过 255 个符号的字符串。这就是为什么我试图为这个问题找到另一个解决方案。

【问题讨论】:

  • 你尝试过什么吗?如果是这样,请发布您的工作以及您可能遇到的任何错误..
  • @Nadeem_MK,对您的回答发表了评论。

标签: vb.net ms-word office-interop


【解决方案1】:

您的问题似乎很模糊,但作为开始,您可以如下进行;

        Dim objWord As New Word.Application  
        Dim WordToFind as string = "Test"
        objWord.Visible = True 

        'Open an existing document.  
        Dim objDoc As Word.Document = objWord.Documents.Open("C:\folder\MyDoc.doc")  
        objDoc = objWord.ActiveDocument  

        'Find word
        objDoc.Content.Find.Execute(FindText:=WordToFind)  

        ' perform your process with the searched text

        'Close the document 
        objDoc.Close()  
        objDoc = Nothing 
        objWord.Quit()  
        objWord= Nothing 

希望对你有帮助!

【讨论】:

  • 感谢您的回答。我有相同的代码。问题是我找不到/替换超过 255 个符号的字符串。这就是为什么我试图找到另一种方法来解决这个问题。
  • 哦.. 可能您需要编辑您的问题并提及它;)
【解决方案2】:

如果有人正在寻找此问题的解决方案:

Imports Word = Microsoft.Office.Interop.Word
Dim WordApp As Word.Application = New Word.Application
Dim WordDoc As Word.Document

Public Function FindReplaceText(CellsValueWithLabel As String()()) As Boolean
    'Find and replace texts from arrays
    For Each cellsValue In CellsValueWithLabel
        Try
            If cellsValue(1).Length < 255 Then
                If WordDoc.Content.Find.Execute(FindText:=cellsValue(0), ReplaceWith:=cellsValue(1), Replace:=Word.WdReplace.wdReplaceAll) Then
                    logHistory.insertLogHistory(Chr(34) + cellsValue(0) + Chr(34) + " - replaced by " + Chr(34) + cellsValue(1) + Chr(34))
                End If
            Else
                Dim myRange = WordDoc.Content
                While myRange.Find.Execute(FindText:=cellsValue(0))
                    If myRange.Find.Found Then
                        myRange.Select()
                        My.Computer.Clipboard.SetText(cellsValue(1))
                        WordApp.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault)
                        logHistory.insertLogHistory(Chr(34) + cellsValue(0) + Chr(34) + " - replaced by " + Chr(34) + cellsValue(1) + Chr(34))
                        Clipboard.Clear()
                    End If
                End While
            End If
        Catch ex As Exception
            logHistory.insertLogHistory("********** ERROR ********** " + cellsValue(0) + " " + ex.Message.ToString())
        End Try
    Next

    WordDoc.Save()
    Return True
End Function

【讨论】:

    【解决方案3】:

    如果它可以帮助任何人,替换超过 255 个字符,使用范围会有所帮助。

     If (SomeText.Length < 250) Then
        oSel.Find.Execute("SomeText", , , , , , True, Word.WdFindWrap.wdFindContinue, , "Replacetext", Word.WdReplace.wdReplaceAll)
        Else
            Dim rng As Word.Range = oSel.Range
            rng.Find.Execute("SomeText", , , , , , , , , , )
            rng.Text = DirectCast(StringWithMoreThan255Characters, String)
    
     End If
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-17
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多