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

x
?
Solved

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

Posted on 2005-03-19
21
Medium Priority
?
568 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
  • 8
  • 6
  • 5
19 Comments
 
LVL 77

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 77

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
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.

 
LVL 77

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 77

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 77

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 77

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 77

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 77

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

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.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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 Month15 days, 7 hours left to enroll

575 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