M
M
Mr. Abdu Jan2014-10-16 16:57:55
Visual Basic
Mr. Abdu Jan, 2014-10-16 16:57:55

How to highlight a word in MS Word via VB.NET?

Hello everyone,
There is one problem.
It is necessary to select a certain word in the Word, through VB.NET ( Microsoft.Office.Interop.Word ).
Any ideas how to do it?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Mr. Abdu Jan, 2014-10-21
@Abdukhafiz

I decided myself.
If anyone needs an answer:

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

T
tester_toster, 2016-11-18
@tester_toster

The asterisk is definitely not needed, the entire line is deleted.

M
Max, 2016-11-18
@MaxDukov

remove the asterisk.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question