Extract Page Number of selected text from MS Word using VB.NET

Hey guys,

I am creating a small application that searches an MS Word Document for any information contained in parentheses/brackets and eventually prints it to an excel spreadsheet (I'll work on that later.

So far I am able to extract the data from the document and display it in a MsgBox; however, when i try to extract the page number I keep getting read-only errors with the file (assuming it is trying to open a second instance of the file).

Can someone point me in the right direction for extracting the page number with the text I have extracted. The code attached is the same that is giving me the error... without the reference to objPageNumber, the code works to give me only the text in the document.

Cheers
G_M

 
Imports Microsoft.Office.Interop

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        Dim objword As Word.Application
        Dim objDoc As Word.Document
        Dim strWord As String
        Dim objSelection As Word.Selection
        Dim strInputFile As String
        Dim objPageNumber As String

        strInputFile = txtInputFile.Text
        objword = CreateObject("Word.Application")
        objDoc = objword.Documents.Open(strInputFile)
        objSelection = objword.Selection


        objSelection.Find.Forward = True
        objSelection.Find.MatchWildcards = True
        objSelection.Find.Text = "\(*\)"


        Do While True

            objSelection.Find.Execute()
            If objSelection.Find.Found Then
                strWord = objSelection.Text
                objPageNumber = objSelection.Information(Word.WdInformation.wdActiveEndAdjustedPageNumber)
                MessageBox.Show(strWord & " , Page" & objPageNumber)
            Else
                Exit Do
            End If
        Loop

    End Sub
End Class

Open in new window

G_MAsked:
Who is Participating?
 
Chris BottomleyConnect With a Mentor Software Quality Lead EngineerCommented:
Sorry ... in the context that would be:

objSelection.Range.information(Word.WdInformation.wdActiveEndPageNumber)

Chris
0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
objSelection.Range.information(wdActiveEndPageNumber)

Ought to do it for you.

Chris
0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
IN fact looking agian it looks to me as the likely issue is:

Word.WdInformation

which ought to be:

objword.WdInformation

Chris
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
G_MAuthor Commented:
Perfect... it's quite embarrassing how easy some people make it look... Thank you very much. The solution is attached ;o)

 
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        Dim objword As Word.Application
        Dim objDoc As Word.Document
        Dim strWord As String
        Dim objSelection As Word.Selection
        Dim strInputFile As String
        Dim objPageNumber As String

        strInputFile = txtInputFile.Text
        objword = CreateObject("Word.Application")
        objDoc = objword.Documents.Open(strInputFile)
        objSelection = objword.Selection


        objSelection.Find.Forward = True
        objSelection.Find.MatchWildcards = True
        objSelection.Find.Text = "\(*\)"



        Do While True

            objSelection.Find.Execute()
            If objSelection.Find.Found Then
                strWord = objSelection.Text
                objPageNumber = objSelection.Range.Information(Word.WdInformation.wdActiveEndPageNumber)
                MessageBox.Show(strWord & " , Page" & objPageNumber)
            Else
                Exit Do
            End If
        Loop

    End Sub

Open in new window

0
 
G_MAuthor Commented:
Also realised the reason I was getting the "Read Only" Errors was because I wasn't terminating the instance I created of the test.docx I was using.

Additional line athe the bottom of code:

objword.Quit()

 
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        Dim objword As Word.Application
        Dim objDoc As Word.Document
        Dim strWord As String
        Dim objSelection As Word.Selection
        Dim strInputFile As String
        Dim objPageNumber As String

        strInputFile = txtInputFile.Text
        objword = CreateObject("Word.Application")
        objDoc = objword.Documents.Open(strInputFile)
        objSelection = objword.Selection


        objSelection.Find.Forward = True
        objSelection.Find.MatchWildcards = True
        objSelection.Find.Text = "\(*\)"




        Do While True

            objSelection.Find.Execute()
            If objSelection.Find.Found Then
                strWord = objSelection.Text
                objPageNumber = objSelection.Range.Information(Word.WdInformation.wdActiveEndPageNumber)
                MessageBox.Show(strWord & " , Page" & objPageNumber)
            Else
                Exit Do
            End If
        Loop

        objword.Quit()

Open in new window

0
 
GrahamSkanRetiredCommented:
For safety, make the Word Application visible, then you will more easily be able to close it if the code terminates early.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.