none
Find, select and display text in Word RRS feed

  • Question

  • Hi,

    I'm trying to find and selecting a paragraph text in a word document. My code, so far, finds the text from a textbox as it shows a message to confirm. But I also want to display the header text found in the active Word document. Could you please help me with a solution for that?

    Here is my code so far.

     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim word As New Microsoft.Office.Interop.Word.Application
            Dim doc As New Microsoft.Office.Interop.Word.Document
            Dim findText As String = TextBox1.Text
    
            'doc = word.Documents.Open(My.Settings("Logo").ToString() + "Clausules.docx")
            doc = word.Documents.Open("c:\Temp\Clausules.docx")
            word.Visible = True
            doc.Activate()
    
            doc.Content.Find.ClearFormatting()
            Try
                If doc.Content.Find.Execute(findText) = True Then
                    MessageBox.Show("tekst gevonden")
                    With doc.Content.Find
                        Selection.MoveEnd(wdLine, Count:=1)
                    End With
                    Else
                    MessageBox.Show("tekst niet gevonden")
                End If
                'doc.Close()
                'word.Quit()
            Catch ex As Exception
                doc.Close()
                word.Quit()
            End Try
    
    
        End Sub


    • Edited by RobProsfora Wednesday, June 12, 2019 10:50 AM
    Wednesday, June 12, 2019 10:47 AM

Answers

  • Hi Alex,

    Thanks for your solution.

    I tried your code and got the following error: Cannot run RPC

    Cannot run RPC. What should I do? Anything wrong with the references?

    Regards,

    Rob

    Hi,

    I test my code,no error,it works.

    Imports Microsoft.Office.Interop.Word
    Public Class Form1
        Dim doc As New Microsoft.Office.Interop.Word.Document
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim word As New Microsoft.Office.Interop.Word.Application
            Dim doc As New Microsoft.Office.Interop.Word.Document
            doc = word.Documents.Open("D:\test.docx")
            word.Visible = True
            doc.Activate()
    
            doc.Content.Find.ClearFormatting()
            Dim i = 1
            For Each item As Paragraph In doc.Paragraphs
                If item.Style.NameLocal.ToString.Substring(0, item.Style.NameLocal.ToString.Length - 1) = "Heading" Then
    
                    MsgBox("Head" & i & " name is " & item.Range.Text)
                    i += 1
                End If
            Next
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by RobProsfora Friday, June 14, 2019 2:53 PM
    Friday, June 14, 2019 1:47 AM

All replies

  • Hi,

    You can use the following way to traverse all the head names.

    Imports Microsoft.Office.Interop.Word
    
    Public Class Form1
        Dim doc As New Microsoft.Office.Interop.Word.Document
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim word As New Microsoft.Office.Interop.Word.Application
            Dim doc As New Microsoft.Office.Interop.Word.Document
            Dim findText As String = TextBox1.Text
    
            'doc = word.Documents.Open(My.Settings("Logo").ToString() + "Clausules.docx")
            doc = word.Documents.Open("D:\test.docx")
            word.Visible = True
            doc.Activate()
    
            doc.Content.Find.ClearFormatting()
            Dim i = 1
            For Each item As Paragraph In doc.Paragraphs
    
                If item.Style.NameLocal.ToString.Substring(0, item.Style.NameLocal.ToString.Length - 1) = "Heading" Then
    
                    MsgBox("Head" & i & " name is " & item.Range.Text)
                    i += 1
                End If
    
            Next
            'Try
            '    If doc.Content.Find.Execute(findText) = True Then
            '        MessageBox.Show("tekst gevonden")
            '        'With doc.Content.Find
            '        '    Selection.MoveEnd(wdLine, Count:=1)
            '        'End With
            '    Else
            '        MessageBox.Show("tekst niet gevonden")
            '    End If
            '    'doc.Close()
            '    'word.Quit()
            'Catch ex As Exception
            '    doc.Close()
            '    word.Quit()
            'End Try
    
        End Sub
    
    End Class
    

    Best Regards,

    Alex



    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 13, 2019 3:16 AM
  • Hi Alex,

    Thanks for your solution.

    I tried your code and got the following error: Cannot run RPC

    Cannot run RPC. What should I do? Anything wrong with the references?

    Regards,

    Rob

    Thursday, June 13, 2019 7:09 AM
  • Hi Alex,

    Thanks for your solution.

    I tried your code and got the following error: Cannot run RPC

    Cannot run RPC. What should I do? Anything wrong with the references?

    Regards,

    Rob

    Hi,

    I test my code,no error,it works.

    Imports Microsoft.Office.Interop.Word
    Public Class Form1
        Dim doc As New Microsoft.Office.Interop.Word.Document
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim word As New Microsoft.Office.Interop.Word.Application
            Dim doc As New Microsoft.Office.Interop.Word.Document
            doc = word.Documents.Open("D:\test.docx")
            word.Visible = True
            doc.Activate()
    
            doc.Content.Find.ClearFormatting()
            Dim i = 1
            For Each item As Paragraph In doc.Paragraphs
                If item.Style.NameLocal.ToString.Substring(0, item.Style.NameLocal.ToString.Length - 1) = "Heading" Then
    
                    MsgBox("Head" & i & " name is " & item.Range.Text)
                    i += 1
                End If
            Next
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by RobProsfora Friday, June 14, 2019 2:53 PM
    Friday, June 14, 2019 1:47 AM