?
Solved

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

Posted on 2005-03-19
21
Medium Priority
?
558 Views
Last Modified: 2010-08-05
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.
0
Comment
Question by:on99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 5
21 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 100 total points
ID: 13584874
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
 
LVL 23

Assisted Solution

by:gecko_au2003
gecko_au2003 earned 100 total points
ID: 13584876
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 13584881
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 13584888
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
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13584894
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
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13584895
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 13584979
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
 

Author Comment

by:on99
ID: 13585181
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
 

Author Comment

by:on99
ID: 13585201
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 13586344
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
 

Author Comment

by:on99
ID: 13587055
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 13589392
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 13589398
Forgot to say: set references to the Microsoft Active Data Objects Library and to the Microsoft Word Object Library.
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13591076
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
 

Author Comment

by:on99
ID: 13599542
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
 

Author Comment

by:on99
ID: 13632114
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
 

Author Comment

by:on99
ID: 13632134
GrahamSkan,

Can I add you to my ICQ list? My ICQ number - 48251162.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 13883436
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
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13883460
It's an instant messenger ICQ = I seek you ( In plain lamens terms english lol ) www.icq.com go there :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month12 days, 6 hours left to enroll

752 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