Link to home
Start Free TrialLog in
Avatar of G_M
G_MFlag for Australia

asked on

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

Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

objSelection.Range.information(wdActiveEndPageNumber)

Ought to do it for you.

Chris
ASKER CERTIFIED SOLUTION
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
IN fact looking agian it looks to me as the likely issue is:

Word.WdInformation

which ought to be:

objword.WdInformation

Chris
Avatar of G_M

ASKER

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

Avatar of G_M

ASKER

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

For safety, make the Word Application visible, then you will more easily be able to close it if the code terminates early.