extract text from word file

pl456

Registered User.
Local time
Today, 14:52
Joined
Jan 31, 2008
Messages
150
Not sure this is possible.

Anybody know of a way to extract a text string from a word file and import into access?
The string would be the same in every document e.g. DOB ##/##/## but may not be in the same position within the document.
:(
 
Use the code below to get your word document content and then you will need to play with the STRING functions to get what you want.

You will need Microsoft Word XX.X Object Library

Code:
Private Sub Command0_Click()
    
    Dim app As Word.Application
    Dim objDoc As Word.Document
    Dim sVal As String
    
    Set app = New Word.Application
    Set objDoc = app.Documents.Open("C:\Documents and Settings\xionc\Desktop\Magic Folder\test.doc")
    
    sVal = objDoc.Content
    
    'do what you want to do with the contents here
    '...........................................
    
    objDoc.Close
    Set objDoc = Nothing
    
    app.Quit
    Set app = Nothing
    
End Sub
 
Not three bad thanks. still having a bit of issue with it. when you first run the code you get prompted that the file is locked for editing by ' '. and have to make a selection to open as read only.

Any ideas?

Does the file actually have to open?
 
Sorry, figured the last one out, me being stupid.

The real problem now is handling the text, I have passed the contents into an Insert sql statement to insert into a memo field. However when you come accross ' or " in written text it obviously thinks this is part of the statement and fails to execute.

Any suggestions for dealing with these?
 
Right now I am trying to format ADO code that I mostly picked up from an MSDN article. I need to import data from fields in A Word document named Corrective Action Report, located on my desktop. I am getting a compile error message for the line I highlighted in blue.

I want to understand how this code works because I will also have other forms I need to import data from, into other tables.

Private Sub Command0_Click()

Dim app As Word.Application
Dim objDoc As Word.Document
Dim sVal As String

Set app = New Word.Application
Set objDoc = app.Documents.Open("C:\Documents and Settings\JKirley\Desktop\Corrective Action Report.doc")

sVal = objDoc.Content

On Error GoTo Err_CmdButton_Click

directory = "C:\Documents and Settings\JKirley\Desktop\Corrective Action Report"

If strFile = "" Then Exit Sub
Exit_CmdButton_Click:
Exit Sub
Err_CmdButton_Click:
MsgBox Err.Description
Resume Exit_CmdButton_Click

End Sub
On Error GoTo ErrorHandling
strDocName = "C:\Audit Results\" & _
InputBox("Enter the Audit report number" & _
"you want to import:", "Import Corrective Action Report*")
Set appWord = GetObject(, "Word.Application")
Set doc = appWord.Documents.Open(strDocName)
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\CA database_Backup\" & _
"Audit results.mdb;"
rst.Open "tblAuditResults", cnn, _
adOpenKeyset, adLockOptimistic
With rst
.AddNew

...blah blah blah, field field field...

.Update
.Close
End With
doc.Close
If blnQuitWord Then appWord.Quit
cnn.Close
MsgBox "Corrective Action Report Imported!"
Cleanup:
Set rst = Nothing
Set cnn = Nothing
Set doc = Nothing
Set appWord = Nothing
Exit Sub
ErrorHandling:
Select Case Err
Case -2147022986, 429
Set appWord = CreateObject("Word.Application")
blnQuitWord = True
Resume Next
Case 5121, 5174
MsgBox "You must select a valid Word document. " _
& "No data imported.", vbOKOnly, _
"Document Not Found"
Case 5941
MsgBox "The document you selected does not " _
& "contain the required form fields. " _
& "No data imported.", vbOKOnly, _
"Fields Not Found"
Case Else
MsgBox Err & ": " & Err.Description
End Select
GoTo Cleanup
End Sub
Thank you for your help!
 
Hello, Jennifer Kirley,

Please post your question in a separate thread, as it is apparently dealing with a separate issue. Then other members will be able to distinguish your post as a new thread.


Hello, pl456, here are two examples:

To insert Michael O'Donnell into a text field:

If the text-delimiter is a single-quote:
INSERT INTO MyTable (MyField)
VALUES ('Michael O''Donnell');
(NOTE: The O and Donnell in this example are separated by two single-quotes, not a double-quote)

If the text delimiter is a double-quote:
INSERT INTO MyTable (MyField)
VALUES ("Michael O'Donnell");


To insert The "right" way into a text field:

If the text-delimiter is a single-quote:
INSERT INTO MyTable (MyField)
VALUES ('The "right" way'):

If the text-delimiter is a double-quote:
INSERT INTO MyTable (MyField)
VALUES ("The ""right"" way"):
 

Users who are viewing this thread

Back
Top Bottom