How can use VB get word document some key word, then put it to access

Hi all,

How can use VB to read in the ms-word document files the content, then copy the content to access?
I attempt ("word.application") have opened with CreateObject to word document only. Can I use "Find" function to search ms-word content in VB?
Thanks.
on99Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
No reason why not.

This is an early-binding example (set a reference to the Microsoft Word object library)

Private Sub Command1_Click()
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wdRange As Word.Range
Set wdApp = New Word.Application
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Open("C:\mypath\cat.doc")
Set wdRange = wdDoc.Range
With wdRange.Find
    .Text = "Persian"
    .Execute
    If .Found Then
        wdRange.InsertBefore "Long-haired "
    End If
End With

End Sub


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Shane Russell2nd Line Desktop SupportCommented:
ok Go to this site :

http://www.vb-helper.com/index_office.html

Then you would do something like so :

convert it into a text file, use the File System Object ( which you will need a reference to in Project --> References to Microsoft Scripting Runtime ) to open and read each item from the converted text file into an array and from there you could read back the values from the array and insert each array item into a database by using a for next loop or something to that extent :) Just to give you some idea :)

Also if you go to :

www.juicystudio.com

they have tutorials on the FSO and other things.

Some other sites that may be useful to you are :

www.pscode.com

http:\\vbnet.mvps.org

www.codeguru.com

I hope this helps, if you want an example or actual source code please let me know :)
0
GrahamSkanRetiredCommented:
This is the late-binding version

Private Sub Command1_Click()
Dim wdApp As Object 'Word.Application
Dim wdDoc As Object 'Word.Document
Dim wdRange As Object 'Word.Range
'Set wdApp = New Word.Application
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Open("C:\mypath\cat.doc")
Set wdRange = wdDoc.Range
With wdRange.Find
    .Text = "Persian"
    .Execute
    If .Found Then
        wdRange.InsertBefore "Long-haired "
    End If
   
End With

End Sub
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

GrahamSkanRetiredCommented:
Sorry, I didn't notice that you had two questions. I only answered the second one.

What do you want to put into your database, the whole document?
0
Shane Russell2nd Line Desktop SupportCommented:
Just out of curiousty, are you just answering one part of this question. If you take a look at the title it says then put it to access. The other reason I suggested converting it to a text file was because everyone has different versions of office and so there for using his version of the word object would not work on a different version of office so converting it and using the FSO and searching for a word using a for next loop or something to that extent would work on any machine pretty much :)

I have never tried to convert word doc to text file of any sort whether it be txt or rtf or w/e but it should work by using the coded examples from that website :)
0
Shane Russell2nd Line Desktop SupportCommented:
My question with regards to his second part was did he just want to find certain aspects of the word document and only insert them into the access database or does he just want to go through the whole word document and insert every individual item into the database ? If just inserting all of the items from word into access, then why did you do the search ?
0
GrahamSkanRetiredCommented:
on99,
Both gecko_au2003 and I are unsure about your precise requirement. Personally, for performance reasons, I would not recommend putting a Word document, or any other large object in an Access field. I recommend that you just put the filename & path in the database.
Can you please clarify?
0
on99Author Commented:
I want to select the word document some line, then put in to access, but the word document also have picture on it.., so it can't convert to txt format.
0
on99Author Commented:
I have the database on hand, but the database is word file format(more word file...) & isn't standard format. So now, I need convert all file to access database. I want add some code in each record, e.g. '#0001', then use VB to automatic convert data to access database.
0
GrahamSkanRetiredCommented:
Sorry, still don't understand. How can database be in word file format?

You could add each Word documents's paragraph as text to an Access text field. Is that what you want?
0
on99Author Commented:
I want put the each record to database base on the code number. For the example, it has 3 records will put to database, '#0002#05' record have one image file on it.

word file sample
--------------------------------
#0001#02 <---code number
1. What is your name?
____________________

#0002#05
2. This is my picture.

[image file]

#0003#01
3. ...something like that.
-------------------------------
0
GrahamSkanRetiredCommented:
This code will take the contents of the line two after the sought number. I pasted your data into a Word document and I note that the lines are ended with newline characters rather than paragraph marks that usually end the lines in a Word document.

Option Explicit

Private Sub Command1_Click()
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wdRange As Word.Range
Dim Lines
Dim strText As String
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Set wdApp = New Word.Application
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
      "Data Source=C:\Documents and Settings\User Name\My Documents\MyDB.mdb;"
rs.Open "MyTable", cn, adOpenDynamic, adLockPessimistic, adCmdTable
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Open("C:\Documents and Settings\User Name\My Documents\on99\ToAccess.doc")
Set wdRange = wdDoc.Range
With wdRange.Find
    .Text = "#0001#02*^l*^l*^l" 'look for the code and three newline characters
    .MatchWildcards = True
    .Execute
    If .Found Then
        Lines = Split(wdRange.Text, Chr$(11)) 'create an array of three elements
        strText = Lines(2) 'third element
        rs.AddNew
        rs.Fields("Name").Value = strText
        rs.Update
    End If
   
End With
wdDoc.Close
wdApp.Quit
rs.Close
cn.Close
End Sub

0
GrahamSkanRetiredCommented:
Forgot to say: set references to the Microsoft Active Data Objects Library and to the Microsoft Word Object Library.
0
Shane Russell2nd Line Desktop SupportCommented:
I let you attempt to work it out GrahamSkan as I have never worked with trying to attain certain aspects out of a word document and then inserting them into an access database like that. Give me something to look at, not to mention to learn as well :)
0
on99Author Commented:
GrahamSkan,

Sorry, I'm very hardwork at this two days, so can't to try it..@@, I will test it in this week end, thanks very much^^.
0
on99Author Commented:
GrahamSkan,

I find the error - 461 in 'Set wdRange = wdDoc.Range', how can I solve it?

Private Sub Command3_Click()
Dim wdApp As Word.Application
Dim wdDoc As Word.Documents
Dim Lines
Dim strText As String
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Set wdApp = New Word.Application
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
      "Data Source=C:\inetpub\wwwroot\demo\tcare\MyDB.mdb;"
rs.Open "text", cn, adOpenDynamic, adLockPessimistic, adCmdTable
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Open("C:\a.doc")
Set wdRange = wdDoc.Range  <--- here have error
0
on99Author Commented:
GrahamSkan,

Can I add you to my ICQ list? My ICQ number - 48251162.
0
GrahamSkanRetiredCommented:
Sorry on99,

I didn't realise that I'd left you hanging.
I hadn't declared the wdRange, though I would have expected a different error.

Dim wdRange as Word.Range

What's ICQ?
0
Shane Russell2nd Line Desktop SupportCommented:
It's an instant messenger ICQ = I seek you ( In plain lamens terms english lol ) www.icq.com go there :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

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.