Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

read word document and fill the details in VB form

Posted on 2012-09-04
5
Medium Priority
?
642 Views
Last Modified: 2012-09-05
i have a word document file which is in .doc format and has some data in it like
name: test name
code:  test code
address: test address
.
.
.
etc...

i have a form in VB6 with textboxes for each "test name", "test code", "test address" etc...
is it possible to read the word document by just specifying the path of the word document and update the text boxes with those information alone.

Please help as i need to complete my project.
0
Comment
Question by:anbuvec
  • 2
4 Comments
 

Author Comment

by:anbuvec
ID: 38367013
some how i have managed to read the file and put it in text box.... i wish i can read particular range or word and input to textbox in vb6 form. i am trying to do this as i have 1000's of such file which i would not be able to open and read one by one.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 38369200
It might help if you posted a sample Word document
0
 
LVL 77

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 38369291
The difficulty might lie in locating the text you want to read in. This outline snippet expects the 'label', e.g. "name:" to be uniquely at the start of a paragraph in the document, with the target text taking up the rest of the paragraph.
Sub ReadDoc()
    Dim appWord As Word.Application
    Dim bNewInstance As Boolean
    Dim docWord As Word.Document
    Dim rng As Word.Range
    Dim strWordPath As String
    Dim lEnd As Long
    
    strWordPath = "C:\MyFolder\MyDoc.doc"
    
    On Error Resume Next 'supress error checking
    Set appWord = GetObject(, "Word.Application")
    On Error GoTo 0 'resume error checking
    
    If appWord Is Nothing Then
        Set appWord = CreateObject("Word.Application")
        appWord.Visible = True
        bNewInstance = True
    End If
    
    Set docWord = appWord.Documents.Open(strWordPath)
    Set rng = docWord.Range
    With rng.Find
        .Text = "name:"
        If .Execute Then
            lEnd = rng.End
            rng.Expand wdParagraph
            rng.Start = lEnd 'start of 'test name'
            rng.MoveEnd wdCharacter, -1 'drop paragraph mark
            Form1.txtName.Text = Trim(rng.Text)
        End If
    End With
    '...
    If bNewInstance = True Then 'only quit the application
        appWord.Quit
    End If
End Sub

Open in new window

0
 

Author Closing Comment

by:anbuvec
ID: 38370982
Dear Graham,

With rng.Find
        .Text = "name:"
        If .Execute Then
            lEnd = rng.End
            rng.Expand wdParagraph
            rng.Start = lEnd 'start of 'test name'
            rng.MoveEnd wdCharacter, -1 'drop paragraph mark
            Form1.txtName.Text = Trim(rng.Text)
        End If
    End With

actually i used just these lines to get my project go on. coz i have my own logic to open a file from ziped folder with password.... only the logic of range.find didnt strike my mind... i really appreciate your quick response... hats off.......

regards
anbu
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
Lost Word File? Eagerly, need it back? Read ahead; this File Recovery guide is for you.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

578 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