?
Solved

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

Posted on 2011-10-15
6
Medium Priority
?
1,104 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:G_M
  • 3
  • 2
6 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 36973074
objSelection.Range.information(wdActiveEndPageNumber)

Ought to do it for you.

Chris
0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 2000 total points
ID: 36973078
Sorry ... in the context that would be:

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

Chris
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 36973080
IN fact looking agian it looks to me as the likely issue is:

Word.WdInformation

which ought to be:

objword.WdInformation

Chris
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:G_M
ID: 36973082
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
 

Author Comment

by:G_M
ID: 36973111
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36973276
For safety, make the Word Application visible, then you will more easily be able to close it if the code terminates early.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Lost Word File? Eagerly, need it back? Read ahead; this File Recovery guide is for you.
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question