ADO code for importing from a Word document

Jennifer Kirley

Ambitious noob
Local time
Today, 16:56
Joined
Feb 3, 2009
Messages
5
Hello, I have asked a question here and hope not to get lost...nor do I want to clutter this site with multiple posts. I hope youi can help me!

Thank you
 
Hello, I have asked a question here and hope not to get lost...nor do I want to clutter this site with multiple posts. I hope youi can help me!

Thank you

Jennifer,

Your post was what is called a "Hijack". You posted your question to another member's thread. These types of replies are normally ignored. You were even advised to post it in to a new thread if you want it answers.


Here is your original question if anyone wants to take a look:

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.

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\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
 
Please excuse me, from here out I will only post in a new thread. I missed the part about being advised to do so. I am new here. Sorry to be a problem.
 
Try changing:
Code:
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*")
...to
Code:
On Error GoTo ErrorHandling

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

If strFile = "" Then Exit Sub

strDocName = "C:\Audit Results\" & _
InputBox("Enter the Audit report number" & _
"you want to import:", "Import Corrective Action Report*")
 
Thank you for responding, I think that helped but I am getting an error message:

The remote server machine does not exist or is unavailable (Error 462)

Will this only work if I keep the files some place besides my desktop?
 
Which line of code is highlighted when you receive this error?
 
Which line of code is highlighted when you receive this error?
The first message I get is that the File In In Use: Corrective Action Report is locked for editing by me...it's not. It asks if I want to open a read-only version of the file. I never get a pop up box saying the import has been completed, and the table remains empty.

Here's the code line you asked for:

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

Users who are viewing this thread

Back
Top Bottom