Page 1 of 3 123 LastLast
Results 1 to 15 of 31
  1. #1
    maxmd29 is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Nov 2013
    Posts
    12

    Run-Time Error 91 with import code

    I am fairly new to MS Access and VBA. My goal with this code is to import data from a MS Word Macro-Enabled Document into my database. I found a few tutorials online how to do this but I have ran into a run-time error "91: Object Variable or With block variable not set". It gets to line 29 and then jumps to my error section. Any help is much needed



    My code:
    Code:
     Function GetWordData()
    
    Dim appWord As Word.Application
    Dim docm As Word.Document
    Dim cnn As New ADODB.Connection
    Dim rst As New ADODB.Recordset
    Dim strDocmName As String
    Dim blnQuitWord As Boolean
    
    
    On Error GoTo ErrorHandling
    
    
    strDocmName = "C:\C:\Users\maxm\Documents\" & _
        InputBox("Enter the name of the Word contract " & _
        "you want to import:", "Import Contract")
    
    
    Set appWord = GetObject(, "Word.Application")
    Set docm = appWord.Documents.Open(strDocmName)
    
    
    cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=C:\Users\maxm\Documents\" & _
        "importtesting.accdb;"
    
    
    rst.Open "tblContracts", cnn, _
        adOpenKeyset, adLockOptimistic
    
    
    With rst
        .AddNew
            !FirstName = docm.FormFields("fldFirstName").result
            !LastName = docm.FormFields("fldLastName").result
            !Company = docm.FormFields("fldCompany").result
            !Address = docm.FormFields("fldAddress").result
            !City = docm.FormFields("fldCity").result
            !State = docm.FormFields("fldState").result
            !ZIP = docm.FormFields("fldZIP1").result & _
                "-" & docm.FormFields("fldZIP2").result
            !Phone = docm.FormFields("fldPhone").result
            !SocialSecurity = docm.FormFields("fldSocialSecurity").result
            !Gender = docm.FormFields("fldGender").result
            !BirthDate = docm.FormFields("fldBirthDate").result
            !AdditionalCoverage = _
                docm.FormFields("fldAdditional").result
        .Update
        .Close
    End With
    docm.Close
    If blnQuitWord Then appWord.Quit
    cnn.Close
    MsgBox "Contract Imported!"
    
    
    Cleanup:
    Set rst = Nothing
    Set cnn = Nothing
    Set docm = Nothing
    Set appWord = Nothing
    Exit Function
    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 Function

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Where is line 29?

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,921
    With ADO, need to Set the connection and recordset.

    Set cnn = New ADODB.Connection
    Set rst = New ADODB.Recordset
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    maxmd29 is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Nov 2013
    Posts
    12
    Is there more to it than just adding in those 2 lines? I have added those 2 lines into my code, but when it gets to
    Code:
    !FirstName = docm.FormFields("fldFirstName").result
    error 91 still comes up

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,921
    Sorry, I've never coded for import from Word doc.

    Think I am looking at the article you got the code from. It was built for Access 2000, which might explain why the Set lines were omitted or that was an oversight in the article.

    This thread revises the code to use CreateObject instead of GetObject and it also uses DAO instead of ADO. http://www.access-programmers.co.uk/...d.php?t=199989
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Where did your original error happen, exactly? Where is table "tblContracts" located? Is it in another database?

  7. #7
    maxmd29 is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Nov 2013
    Posts
    12
    My original error occurred at !FirstName=docm.FormFields("fldFirstName").result.

    Also, table "tblContracts" is located in the same database.

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,921
    Did you try different code from the link?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    maxmd29 is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Nov 2013
    Posts
    12
    I did, to no avail. I thought that I had it when I got to "set rst=CurrentDb.." but it still returned my error 91 object variable not set. It also is still failing at the same spot.

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,921
    If you want provide the files for analysis, follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    maxmd29 is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Nov 2013
    Posts
    12
    I appreciate all the time and effort you have already invested in my issue. I hope that I have attached my files correctly, I have also included the word document that I am trying to get the data from.
    Attached Files Attached Files

  12. #12
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Can you test this code in a sepearate control button and let us know if it opens your word doc?


    Code:
    Dim appWord As Object
    Dim docm As Object
    Dim strDocmName As String
    strDocmName = "C:\Users\maxm\Documents\" & _
        InputBox("Enter the name of the Word contract " & _
        "you want to import:", "Import Contract")
    Set appWord = CreateObject("Word.Application")
    Set docm = appWord.Documents.Open(strDocmName & ".docm")
    appWord.Visible = True
    Last edited by ItsMe; 12-17-2013 at 05:52 PM. Reason: Wrong file extension for the word doc

  13. #13
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    @June

    Maybe a file picker would be more appropriate. VTW, I found a typo in the string for the path in the original code. "C:\C:\Users...

  14. #14
    maxmd29 is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Nov 2013
    Posts
    12
    I just noticed that too after taking a break from staring that the code. Good catch but unfortunately was not the fix

  15. #15
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by maxmd29 View Post
    I just noticed that too after taking a break from staring that the code. Good catch but unfortunately was not the fix
    Are you interested in my suggestion in post #12?

Page 1 of 3 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 12-13-2013, 07:03 AM
  2. VBA Code Returning Error Run Time Error 3061
    By tgwacker in forum Access
    Replies: 2
    Last Post: 11-24-2013, 11:00 AM
  3. Replies: 6
    Last Post: 04-26-2013, 03:51 PM
  4. Run-time error on import (Split DB)
    By jgelpi16 in forum Programming
    Replies: 1
    Last Post: 03-15-2011, 06:43 AM
  5. Duplicate Check code with Run-Time error '3079'
    By viper in forum Programming
    Replies: 5
    Last Post: 10-18-2010, 10:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums