Questions

Access - Word Automation problem

+
0 Votes
Locked

Access - Word Automation problem

jocallahan
In writing data from Access 2003 to a Word 2003 table using Automation, the Word table doesn't continue from page to page as it does automatically when manually inserting new rows. It merely grows to the size of one page, including header and footer and then stops taking data. Here's the code I'm using to add rows to the table as I write data to it.

Set WordObj = CreateObject("Word.Application")
Set WordDoc = WordObj.Documents.Open("C:\TestDoc.dot")
.
.
lRow = lRow + 1
.
.
If WordDoc.Tables(1).Rows.Count < lRow Then
WordDoc.Tables(1).Rows.Add
End If

Thanks for your help.
  • +
    0 Votes
    fwang

    In word, if you move right of the last cell in a table of last row, Word will automatically append a new row. Following macro will create a table with 1 row 3 column, and automatically fill data and grow to 10 rows:

    Sub Macro3()
    Dim i As Integer, j As Integer

    ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _
    3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
    wdAutoFitFixed
    With Selection.Tables(1)
    If .Style <> "Table Grid" Then
    .Style = "Table Grid"
    End If
    .ApplyStyleHeadingRows = True
    .ApplyStyleLastRow = True
    .ApplyStyleFirstColumn = True
    .ApplyStyleLastColumn = True
    End With

    For i = 1 To 10
    For j = 1 To 3
    Selection.TypeText Text:=i & j
    Selection.MoveRight Unit:=wdCell
    Next
    Next
    End Sub

    +
    0 Votes
    jocallahan

    Thanks fwang for your quick reply. I changed my code to your prototype, but am still having the same problem. The table goes row-by-row as expected until it fills the first page, then it jumps to page 2 and continues until the entire page is filled with a table. The last rows are now invisible and there is a blank page 3. I've included a more complete code sample. Thanks again for your help. BTW am using Word 2003 SP2.

    Dim MyRs As Recordset
    Dim WordObj As Word.Application
    Dim WordDoc As Word.Document

    Dim i As Integer, iCellCount As Integer

    Set MyRs = CurrentDb.OpenRecordset("qryClients")
    MyRs.MoveFirst

    Set WordObj = CreateObject("Word.Application")
    WordObj.Visible = True

    Set WordDoc = WordObj.Documents.Add("C:\Clients.dot")


    iTables = WordDoc.Tables.Count

    If iTables > 0 Then

    With WordDoc.Tables(1)
    If .Style <> "Table Grid" Then
    .Style = "Table Grid"
    End If
    .ApplyStyleHeadingRows = True
    .ApplyStyleLastRow = True
    .ApplyStyleFirstColumn = True
    .ApplyStyleLastColumn = True
    End With

    iCellCount = WordDoc.Tables(1).Rows(1).Cells.Count

    End If


    With WordObj.Application
    .ActiveDocument.Tables(1).Select

    Do While Not MyRs.EOF

    For i = 1 To iCellCount
    .Selection.TypeText Text:=Nz(MyRs(Field(i)), "")
    .Selection.MoveRight Unit:=wdCell
    Next i

    MyRs.MoveNext
    Loop

    End With

    WordDoc.SaveAs "C:\Clients.Doc"

    MyRs.Close
    WordObj.Quit
    Set WordObj = Nothing

    +
    0 Votes
    jocallahan

    Setting the Table Property - Text Wrapping to None in the document template solved the problem. Thanks for your help.
    Do you know how to set that property in code?
    Thanks, John

  • +
    0 Votes
    fwang

    In word, if you move right of the last cell in a table of last row, Word will automatically append a new row. Following macro will create a table with 1 row 3 column, and automatically fill data and grow to 10 rows:

    Sub Macro3()
    Dim i As Integer, j As Integer

    ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _
    3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
    wdAutoFitFixed
    With Selection.Tables(1)
    If .Style <> "Table Grid" Then
    .Style = "Table Grid"
    End If
    .ApplyStyleHeadingRows = True
    .ApplyStyleLastRow = True
    .ApplyStyleFirstColumn = True
    .ApplyStyleLastColumn = True
    End With

    For i = 1 To 10
    For j = 1 To 3
    Selection.TypeText Text:=i & j
    Selection.MoveRight Unit:=wdCell
    Next
    Next
    End Sub

    +
    0 Votes
    jocallahan

    Thanks fwang for your quick reply. I changed my code to your prototype, but am still having the same problem. The table goes row-by-row as expected until it fills the first page, then it jumps to page 2 and continues until the entire page is filled with a table. The last rows are now invisible and there is a blank page 3. I've included a more complete code sample. Thanks again for your help. BTW am using Word 2003 SP2.

    Dim MyRs As Recordset
    Dim WordObj As Word.Application
    Dim WordDoc As Word.Document

    Dim i As Integer, iCellCount As Integer

    Set MyRs = CurrentDb.OpenRecordset("qryClients")
    MyRs.MoveFirst

    Set WordObj = CreateObject("Word.Application")
    WordObj.Visible = True

    Set WordDoc = WordObj.Documents.Add("C:\Clients.dot")


    iTables = WordDoc.Tables.Count

    If iTables > 0 Then

    With WordDoc.Tables(1)
    If .Style <> "Table Grid" Then
    .Style = "Table Grid"
    End If
    .ApplyStyleHeadingRows = True
    .ApplyStyleLastRow = True
    .ApplyStyleFirstColumn = True
    .ApplyStyleLastColumn = True
    End With

    iCellCount = WordDoc.Tables(1).Rows(1).Cells.Count

    End If


    With WordObj.Application
    .ActiveDocument.Tables(1).Select

    Do While Not MyRs.EOF

    For i = 1 To iCellCount
    .Selection.TypeText Text:=Nz(MyRs(Field(i)), "")
    .Selection.MoveRight Unit:=wdCell
    Next i

    MyRs.MoveNext
    Loop

    End With

    WordDoc.SaveAs "C:\Clients.Doc"

    MyRs.Close
    WordObj.Quit
    Set WordObj = Nothing

    +
    0 Votes
    jocallahan

    Setting the Table Property - Text Wrapping to None in the document template solved the problem. Thanks for your help.
    Do you know how to set that property in code?
    Thanks, John