Create table in Word Document From Access using VBA

imo92

New member
Local time
Today, 09:15
Joined
May 12, 2016
Messages
2
I am trying to create tables in a Word document template from my Access database.

This bit of code runs fine from Word itself and creates tables as required. I was wondering if its possible to run this code from Access and point to a specific word document in which to create the tables.

Code:
Dim numberOfTables As Integer
Dim iCount As Integer

numberOfTables = InputBox("How many tables to make?", "Tables")

For iCount = 0 To numberOfTables - 1

    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 = False
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = False
        '.ApplyStyleRowBands = True 'Office 2010
        '.ApplyStyleColumnBands = False 'Office 2007
    End With

    Selection.EndKey Unit:=wdStory
    Selection.TypeParagraph

Next iCount
 
You can do this in access. What you need to do is open the file from Access :
Code:
Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document
    Dim filepath As String

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

    filepath = "C:\...\YourDoc.docx"
    Set wrdDoc = wrdApp.Documents.Open(filepath)

Also you need to be sure that the correct library is active :
VBA Editor >> Menu >> Tools >> References >> and find on the list Microsoft Word XX.X Object library with the highest possible number after in XX.X (possibly 16.0) >> check it >> press OK
 
Thanks was able to link it to the document
 

Users who are viewing this thread

Back
Top Bottom