How to get the value after read specific word from Microsoft Word?

brownie

Member
Joined
Jun 27, 2013
Messages
5
Programming Experience
Beginner
I want to get the particular value from Microsoft Word after read the specific word
For example if a word document which has text as follows:
Customer Details
Date: 23/6/2013
Name: Jason
Cust ID: A2441
So i want the program read if have word "Date" then it will get the value 23/6/2013 and place into textbox.
Then it will continue read word "Name" then will get Jason into textbox2.
How to get the value of date after i read date?

VB.NET:
[COLOR=#00008B][FONT=Consolas]Dim[/FONT][/COLOR][FONT=Consolas] fd [/FONT][COLOR=#00008B][FONT=Consolas]As[/FONT][/COLOR][FONT=Consolas] OpenFileDialog [/FONT][FONT=Consolas]=[/FONT][FONT=Consolas] [/FONT][COLOR=#00008B][FONT=Consolas]New[/FONT][/COLOR][FONT=Consolas] OpenFileDialog[/FONT][FONT=Consolas]()[/FONT][/FONT][/COLOR]

    fd.Title = [COLOR=#800000]"Open"[/COLOR]
    fd.InitialDirectory = [COLOR=#800000]"C:\Documents"[/COLOR]
    fd.Filter = [COLOR=#800000]"Word 97-2003 Documents (*.doc)|*.doc|Word Documents(*.docx)|*.docx"[/COLOR]
    fd.RestoreDirectory = [COLOR=#800000]True[/COLOR]
    [COLOR=#00008B]If[/COLOR] fd.ShowDialog() = DialogResult.OK [COLOR=#00008B]Then[/COLOR]
        txtQuotaFileName.Text = fd.FileName
    [COLOR=#00008B]End[/COLOR] [COLOR=#00008B]If[/COLOR]

    [COLOR=#00008B]Dim[/COLOR] sReader [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]New[/COLOR] StreamReader(fd.FileName)
    [COLOR=#00008B]Dim[/COLOR] text [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]String[/COLOR] = sReader.ReadToEnd()
    sReader.Close()
    [COLOR=#00008B]If[/COLOR] text.Contains([COLOR=#800000]"Date"[/COLOR]) [COLOR=#00008B]Then[/COLOR]
        [COLOR=gray]'Get the value after Date : [/COLOR]
[COLOR=#000000][FONT=Arial][FONT=Consolas]    [/FONT][COLOR=#00008B][FONT=Consolas]End[/FONT][/COLOR][FONT=Consolas] [/FONT][COLOR=#00008B][FONT=Consolas]If[/FONT][/COLOR]
 
Not pretty but works

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sText As String = ""
        sText += "Customer Details"
        sText += "Date: 23/6/2013"
        sText += "Name: Jason"
        sText += "Cust ID: A2441"


        Dim sStart As String = Strings.InStr(sText, "Date:") + 5
        Dim sDate As String = Strings.Mid(sText, sStart, InStr(sText, "Name") - sStart)
        TextBox1.Text = sDate
    End Sub
 
Not pretty but works

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sText As String = ""
        sText += "Customer Details"
        sText += "Date: 23/6/2013"
        sText += "Name: Jason"
        sText += "Cust ID: A2441"


        Dim sStart As String = Strings.InStr(sText, "Date:") + 5
        Dim sDate As String = Strings.Mid(sText, sStart, InStr(sText, "Name") - sStart)
        TextBox1.Text = sDate
    End Sub

Hi,Thnx for your reply.
But your coding is hardcode the date and date value.
The program need to scan the word document and if find the word "date" then it will direct get the value behind.
 
The above code will scan for the word "Date:" then it will look for the next field...

Is the Next field/word not always "Name" ?
It would help to see a real example of the data

But i get the value of 23/6/2013 as the coding hardcode inside.
Yes. The next field would not always be the Name.
So may be need to scan the word document line by line?
 
Yes. It may be will have a new line after the date data.
And some times the word could be like
Email:xyz@hotmail.com<tab><tab>Fax:06-xxxxxxxx<tab><tab>Tel:07-xxxxxxxx
Means there will be three value need to capture for that line.
 
Yes. There will be a lot different word document and all data and sequence will be difference.
Like some of it would not have Address but others will have it.
I get this code to read word file line by line

Dim oReader As New StreamReader(txtQuotaFileName.Text)
Dim sLine As String = Nothing

While Not oReader.EndOfStream
sLine = oReader.ReadLine()
' Do something with the line.


If (Not String.IsNullOrEmpty(sLine)) Then
MessageBox.Show("have word" + sLine.ToString)
Else
MessageBox.Show("is empty")
End If

End While

oReader.Close()
 
Last edited:
Back
Top