Build Dynamically-Sized and Populated Word Table

david_johnson_CR

New member
Local time
Today, 04:46
Joined
Dec 5, 2017
Messages
7
Greetings,

I'm stumped on this one. I need to populate a Word document with, among other things, a table with simple data. I know how to use bookmarks and I know how to populate them from Access. What I don't know is what to do when the number of rows changes with each record.

How do I dynamically size the Word table and how do I populate that table with bookmarks? I'm open to other solutions as well. If I could build the table elsewhere and drop it into the right spot then, as long as it looks similar to this, that's fine.

Thanks for any help/guidance in advance!
David
 
Let's get clarification.

When you want to build a table, specifically what is the dynamic part?

If you have multiple rows in a table, NORMALLY each record would correspond to one row. So why is this different? Are you transposing rows and columns somehow? Your question is not clear because I don't "see in my mind" what you are using as a data source and further don't see how it relates to your required output.

Not to say we don't have answers here, but SOMETIMES we need a little bit more of the immediate details before we wade into the swamp.
 
Of course, my apologies for not being more clear. The way that I know to feed Access data into Word is through bookmarks. There may be another way but I am not familiar with it/them. So, because the size of the table (number of rows) is not fixed, I do not know how to use bookmarks to meet my firm's needs.

The number of rows is what I meant by dynamic. I am not familiar with a way to simply have Access add in another row to hold another record in a Word table, which is natural in Access. So, the crux of my problem/question is: how do I build a table in Word that can add rows as the number of records increases in my Access recordset and how do I tell Access/Word how/where to populate the data?

Thanks! Sorry for not being more clear initially.
 
Generally speaking if you export from a button into Word, with something like this.
Code:
DoCmd.OutputTo acOutputQuery, "[COLOR="Red"]qryHardwareExportWord[/COLOR]", acFormatRTF, "[COLOR="red"]BotHardware.rtf[/COLOR]", True
You will get a table in Word. Of course you need to use your own query and file names. First Define your query,
when you get the results your looking for export. The output will be rows of data in a table like grid.
 
Last edited:
I use a bookmark. The new form fields won't work because they limit the number of characters that can be pasted.

The Access app creates a string. It concatenates the headers separated by tabs (vbTab) with a vbCRLf at the end of each "line" Then the code loops through the detail and using the same technique concatenates the columns separated by tabs and a vbCrLf at the end of each record. The final step is to insert the string and this is the code. I'm pretty sure it will work as is as long as you are OK with the table format I chose. You just pass in the two variables - the bookmark name and the string to put there. The Word objects are all defined publicly in a different procedure so you would have to take care of that also.

Code:
Public Sub FinishTable(bkmk As String, strTable As String)
    
    On Error GoTo Proc_Err

   '''' WordApp.Visible = True  'uncomment for testing
    
    Call InsertTextAtBookMark(bkmk, strTable)
    Set objTable = WordApp.Selection.ConvertToTable(Separator:=vbTab)
    objTable.AutoFormat Format:=wdTableFormatProfessional, applyshading:=False, applyHeadingrows:=True, AutoFit:=True
    objTable.AutoFitBehavior (wdAutoFitWindow)
    WordApp.Selection.MoveRight unit:=wdCell
    WordApp.Selection.MoveRight unit:=wdCell
    WordApp.Selection.SelectRow
    WordApp.Selection.Font.Bold = wdToggle
    WordApp.Selection.Shading.Texture = wdTextureNone
    WordApp.Selection.Shading.ForegroundPatternColor = wdColorAutomatic
'    WordApp.Selection.Shading.BackgroundPatternColor = -603923969 'color # too low for W2003 - caused -2145263334 error and 462 error
    Set objTable = Nothing
    
Proc_Exit:
    Exit Sub
    
Proc_Err:
    Select Case Err.Number
        Case 4605 'this method or property is not available because the object is empty
            Resume Proc_Exit
        Case 5941 ' member does not exist
            Resume Proc_Exit
        Case 5825   'Object has been deleted
            Resume Proc_Exit
        Case 91     'object variable not set
            Resume Proc_Exit
        Case 4218   'type mismatch
            Resume Proc_Exit
        Case Else
            MsgBox Err.Number & " - " & Err.Description
            Resume Proc_Exit
    End Select
    Resume Proc_Exit
End Sub
Public Sub InsertTextAtBookMark(strBkmk As String, varText As Variant)
    Dim BMRange As Word.Range
    
    On Error GoTo Proc_Err
    
    Set BMRange = WordDoc.Bookmarks(strBkmk).Range
    '---- changes here ----
    'WordDoc.Bookmarks(strBkmk).Select    ' subsequent processing refers to selection
    BMRange.Text = varText & ""
    WordDoc.Bookmarks.Add strBkmk, BMRange
    BMRange.Select
    
Proc_Exit:
    Exit Sub
Proc_Err:
    Select Case Err.Number
        Case 4605 'this method or property is not available because the object is empty
            Resume Proc_Exit
        Case 5941, 6028 ' member does not exist/the range cannot be deleted
'            MsgBox "Bookmark {" & strBkmk & "} There is a mapping error with this document.  Please contact your administrator.", vbOKOnly+vbInformation
            Resume Proc_Exit
        Case 91     'object variable not set
            Resume Next
        Case 4218   'type mismatch
            Resume Proc_Exit
        Case Else
            MsgBox Err.Number & " - " & Err.Description
            Resume Proc_Exit
    End Select
    Resume Proc_Exit
End Sub
 
IF you have bookmark in the Word document, then here is one trick.

Open a Word Application Object in a mode that will allow editing.

Open a copy of your template file (if that is what you are using).

Use the GoTo Bookmark operation within the Word App Object.

Insert a Word table at the bookmark. Presumably this is the first table.

Create an object for a Word Table and assign it (SET var = WordAppObj.Tables(0) since collections number from 0).

Add as many columns as you need up front. I.e. immediately after creating the table. Do it as soon as possible.

Now when you need to add a row, you do as static suggested. table-var.Rows.Add

Now, if you need to add data to an extant row to the table, it is something like

table-var.Rows(n).Cells(m) = "text value"

https://msdn.microsoft.com/en-us/library/kw65a0we.aspx - overview of Word document internal structure

https://msdn.microsoft.com/en-us/library/bb157878.aspx - page from which you can follow links regarding programmatic data management in a Word Table
 

Users who are viewing this thread

Back
Top Bottom