Search code examples
vbams-accessms-word

I get this error when I run my subroutine: Runtime error 438. Object doesn't support this property or method


Sub sWordTables(daSN2, daGL, daST, daSY, daBEY, daENY, daSUP)

 Dim daDB As DAO.Database
 Dim appWord As Object 'Word.Application
 Dim WordDoc As Object 'Word.Document
 Dim wdTable As Object 'Word.Table'
 Dim cname As String
 Dim cnum As String
 Dim gss As Integer
 Dim rst As DAO.Recordset
 Dim aVar As String
 Dim wdcount As Integer


  Dim strPath As String
  Dim IntHolder As Integer
  Set appWord = CreateObject("Word.Application")

  strPath = "C:\Users\wpulk\Documents\"

Set daDB = CurrentDb()
Set rst = daDB.OpenRecordset("qry" & daST, dbOpenDynaset)

  Do While Not rst.EOF
     Set WordDoc = appWord.Documents.Open(strPath & "MasterRecord.dotx")
     wdcount = WordDoc.wdTable.Count
 
    With WordDoc
       cname = rst!CourseName
       cnum = rst!CourseNumber
       gss = rst!GradeScore
       incn = rst!IncrementNumber
    
        If wdcount = 1 Then
            .Variables.Add ("StudentName"), daST
            .Variables.Add ("Academic Advisor"), daSUP
            .Variables.Add ("School Year"), daSY
            .Variables.Add ("Grade Level"), daGL
            .Variables.Add ("Beginning Date"), daBEY
            .Variables.Add ("Ending Date"), daENY
            .Fields.Update
         End If
       If wdcount = 2 Then
           If .FormFields("Math").Result = cname Then
            .Variables.Add ("Math" & incn & "a"), cnum
            .Variables.Add ("Math" & incn & "b"), gss
            ElseIf .FormFields("English").Result = cname Then
            .Variables.Add ("Eng" & incn & "a"), cnum
            .Variables.Add ("Eng" & incn & "b"), gss
            ElseIf .FormFields("Word Building").Result = cname Then
            .Variables.Add ("WB" & incn & "a"), cnum
            .Variables.Add ("WB" & incn & "b"), gss
            ElseIf .FormFields("Lierature").Result = cname Then
            .Variables.Add ("Lit" & incn & "a"), cnum
            .Variables.Add ("Lit" & incn & "b"), gss
            ElseIf .FormFields("Science").Result = cname Then
            .Variables.Add ("Science" & incn & "a"), cnum
            .Variables.Add ("Science" & incn & "b"), gss
            ElseIf .FormFields("Social Studies").Result = cname Then
            .Variables.Add ("SocS" & incn & "a"), cnum
            .Variables.Add ("SocS" & incn & "b"), gss
            ElseIf .FormFields("Bible").Result = cname Then
            .Variables.Add ("Bible" & incn & "a"), cnum
            .Variables.Add ("Bible" & incn & "b"), gss
            ElseIf .FormFields("Florida History").Result = cname Then
            .Variables.Add ("oth" & incn & "a"), cnum
            .Variables.Add ("oth" & incn & "b"), gss
            .Fields.Update
            End If
        End If
    End With
Loop
appWord.ActiveDocument.SaveAs strPath & "MasterRecord" & daST & ".docx"
      Set WordDoc = Nothing
      Set appWord = Nothing

End Sub

I was trying to run this subroutine from a function that passes some variables to the subroutine. Then the subroutine will put the information from the function into the tables I created in a word template. I expected it fill in the word template with the data and then save it as another document. When I run it I get the error code 438. This object doesn't support the property or method. I thought I had everything right. I can't figure out what's wrong.


Solution

  • A Word document doesn't have a wdTable property and so the wdcount = WordDoc.wdTable.Count line fails with the error 438 ... to get a count of the Tables in a Word document, instead use wdcount = WordDoc.Tables.Count