通过VB.NET从MS word中选择word

通过VB.NET从MS word中选择word,vb.net,ms-word,office-interop,Vb.net,Ms Word,Office Interop,我想通过VB.NET(Microsoft.Office.Interop.word)从Ms word中选择特定的word 知道怎么做吗 编辑: 问题是我无法找到/替换超过255个符号的字符串。这就是为什么我试图为这个问题找到另一个解决方案。你的问题似乎很模糊,但作为一个开始,你可以按照以下步骤进行 Dim objWord As New Word.Application Dim WordToFind as string = "Test" objWo

我想通过VB.NET(Microsoft.Office.Interop.word)从Ms word中选择特定的word

知道怎么做吗

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

你的问题似乎很模糊,但作为一个开始,你可以按照以下步骤进行

        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 

希望有帮助

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

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
导入Word=Microsoft.Office.Interop.Word Dim WordApp作为Word.Application=新Word.Application Dim WordDoc作为Word.Document 公共函数FindReplaceText(CellsValueWithLabel为String()())为布尔值 '从数组中查找和替换文本 对于cellsValue WithLabel中的每个cellsValue 尝试 如果单元格值(1).Length<255,则 如果WordDoc.Content.Find.Execute(FindText:=cellsValue(0),replacetwith:=cellsValue(1),Replace:=Word.WdReplace.wdReplaceAll),则 logHistory.insertLogHistory(Chr(34)+单元格值(0)+Chr(34)+”-替换为“+Chr(34)+单元格值(1)+Chr(34)) 如果结束 其他的 Dim myRange=WordDoc.Content 而myRange.Find.Execute(FindText:=cellsValue(0)) 如果找到myRange.Find.Find,则 myRange.Select() My.Computer.Clipboard.SetText(单元格值(1)) WordApp.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault) logHistory.insertLogHistory(Chr(34)+单元格值(0)+Chr(34)+”-替换为“+Chr(34)+单元格值(1)+Chr(34)) 剪贴板。清除() 如果结束 结束时 如果结束 特例 logHistory.insertLogHistory(“*********错误********”+单元格值(0)+“+ex.Message.ToString()) 结束尝试 下一个 WordDoc.Save() 返回真值 端函数
如果替换255个以上的字符对任何人都有帮助,那么使用range会有所帮助

 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
如果(SomeText.Length<250)那么
oSel.Find.Execute(“SomeText”,,True,Word.WdFindWrap.wdFindContinue,“Replacetext”,Word.WdReplace.wdReplaceAll)
其他的
尺寸标注为Word.Range=oSel.Range
rng.Find.Execute(“SomeText”、、、、、、、、)
rng.Text=DirectCast(字符串超过255个字符,字符串)
如果结束

您试过什么吗?如果是这样的话,一定要把你的工作和你可能面临的任何错误都张贴出来。@Nadeem_MK,对你的答案发表了评论。谢谢你的回答。我有相同的代码。问题是我无法找到/替换超过255个符号的字符串。这就是为什么我想找到另一种解决这个问题的方法。。可能您需要编辑您的问题并提及它;)