Object Variable Not Set

algecan

Registered User.
Local time
Today, 16:53
Joined
Nov 11, 2009
Messages
37
Hi all,

I am just in the middle of writting a little bit of code that will go through each record in a query and export it into a word table. It's in the early stages but all was working yesterday. I've come to it this morning and ran it and now I get an error message;
Run Time error 91: Object Variable or With Block Variable not set.

From what I can see, all my objects are set and I'm a bit confussed with how it suddenly stopped working. Can anyone see where I have gone wrong?

Code:
Private Sub Command15_Click()
 
Dim MyDb As DAO.Database
Dim rsLogin As DAO.Recordset
 
Dim ObjHead As String
Set MyDb = CurrentDb()
Set rsLogin = MyDb.OpenRecordset("query here")
 
'Open word document
    Dim obj As Object
    Dim wdDoc As Word.Document
    Set obj = CreateObject("Word.application")
    obj.Visible = True
    Set wdDoc = obj.Documents.Open("location of file here")
With rsLogin
        .MoveFirst
        Do Until rsLogin.EOF
            If .Fields(7) = True Then
            ObjHeadTest = .Fields(5)
            If ObjHead <> ObjHeadTest Then
                t = t + 1
                r = 4
            End If
            'get current records data
            ObjHead = .Fields(5)
            ObjNar = .Fields(6)
            ConSum = .Fields(1)
            ExpCon = .Fields(2)
            ConTest = .Fields(3)
 
            'export data into table
            wdDoc.Tables(t).Cell(1, 2).Select
            [COLOR=red][B]Selection.TypeText ObjHead[/B][/COLOR]
            wdDoc.Tables(t).Cell(2, 2).Select
            Selection.TypeText ObjNar
            wdDoc.Tables(t).Cell(r, 2).Select
            Selection.TypeText ConSum
            wdDoc.Tables(t).Cell(r, 3).Select
            Selection.TypeText ExpCon
            wdDoc.Tables(t).Cell(r, 4).Select
            Selection.TypeText ConTest
            r = r + 1  
            End If
            .MoveNext
        Loop
End With
 
Set MyDb = Nothing
Set rsLogin = Nothing
End Sub
 
I think the error is because Selection is part of Word and then it should be:
wdDoc.Selection.TypeText ObjHead
 
I did try that but it produces 'Object doesn't support this property or method.

Have aldo tried putting it in a with statement so...

Code:
With wdDoc.tables(t)
            Cell(1, 2).Select
            Selection.TypeText objHead
            .Cell(2, 2).Select
            Selection.TypeText ObjNar
            .Cell(r, 2).Select
            Selection.TypeText ConSum
            .Cell(r, 3).Select
            Selection.TypeText ExpCon
            .Cell(r, 4).Select
            Selection.TypeText ConTest
End With

But I still get RTE 91
 

Users who are viewing this thread

Back
Top Bottom