Updating a table in the Header of Word using VB6

I have a table in a header that is 6 columns by 4 rows.  I need to update column 2, 4, and 6

with data that I have in my program.

Please explain how to do this.

I have attached the word Document.
Document1.doc
MKadricAsked:
Who is Participating?
 
GrahamSkanRetiredCommented:
You have merged cells in the last row, so you can't address by column. Fortunately you don't have to.

This code shows how to update individual cells in the first row. Note that it uses early binding, so you need to set a reference to the Microsoft Word Object Library.
Sub UpdateHeaderTable()
    Dim wdDoc As Word.Document
    Dim wdApp As Word.Application
    Dim bNewInstance As Boolean
    Dim tbl As Table
    
    On Error Resume Next 'supress error for next line
    Set wdApp = GetObject(, "Word.Application")
    On Error GoTo 0
    If wdApp Is Nothing Then
        Set wdApp = CreateObject("Word.Application")
        bNewInstance = True
    End If
    wdApp.Visible = True
    
    Set wdDoc = wdApp.Documents.Open("C:\MyFolder\Document1.doc")
    Set tbl = wdDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables(1)
    tbl.Cell(1, 2).Range.Text = Format(Now, "dd.MM.yyyy")
    tbl.Cell(1, 4).Range.Text = Format(Now, "hh:nn")
    tbl.Cell(1, 6).Range.Text = Format(DateAdd("n", 18, Now), "hh:nn")
    wdDoc.Close wdSaveChanges
    If bNewInstance Then
        wdApp.Quit
    End If
End Sub

Open in new window

0
 
MKadricAuthor Commented:
tbl.Cell

Compile error memer or data member not found.

It does not have ".cell" as a member
0
 
MKadricAuthor Commented:
I do not know what a merged cell is but a new table can be created.  The table content (colmns 1,3, 5) are always static.  I need to input columns 2, 4, 6.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
GrahamSkanRetiredCommented:
Strange. The code works OK for me. Here is the whole of the code in the project
Option Explicit

Private Sub Command1_Click()
    UpdateHeaderTable
End Sub
Sub UpdateHeaderTable()
    Dim wdDoc As Word.Document
    Dim wdApp As Word.Application
    Dim bNewInstance As Boolean
    Dim tbl As Table
    
    On Error Resume Next 'supress error for next line
    Set wdApp = GetObject(, "Word.Application")
    On Error GoTo 0
    If wdApp Is Nothing Then
        Set wdApp = CreateObject("Word.Application")
        bNewInstance = True
    End If
    wdApp.Visible = True
    
    Set wdDoc = wdApp.Documents.Open("C:\MyFolder\Document1.doc")
    Set tbl = wdDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables(1)
    tbl.Cell(1, 2).Range.Text = Format(Now, "dd.MM.yyyy")
    tbl.Cell(1, 4).Range.Text = Format(Now, "hh:nn")
    tbl.Cell(1, 6).Range.Text = Format(DateAdd("n", 18, Now), "hh:nn")
    wdDoc.Close wdSaveChanges
    If bNewInstance Then
        wdApp.Quit
    End If
End Sub

Open in new window


The bottom row contains only two, and not six cells. Cells 2 to 6 have been merged into a single wide cell so that those columns aren't complete. Any attempt to address columns in this table would give the error:

Run-time error '5992':

Cannot access individual columns in this collection because the table has mixed cell widths.

You asked to update the columns, though it isn't clear what you want to be updated about the columns. I have demonstrated how to update the cell contents for the cells in the first row,

0
 
MKadricAuthor Commented:
First of all Thank you for your prompt replies.

1) User is in app.  
2) User doubles clicks a row in a grid word doc opens
3) User is setting on a particular row and  after the doc opens the information in the doc
header is updated to the information that ithe user is setting on. The header is on every page.

I am running VB6 with sp6.  The "table" does not have member cells... it does have

    tbl.Columns
    tbl.DateCreated
    tbl.DateModified
    tbl.Indexes
    tbl.Keys
    tbl.Name
    tbl.ParentCatalog
    tbl.Properties
    tbl.Type

Any other suguestions?
0
 
MKadricAuthor Commented:
Also using Word 2003
0
 
GrahamSkanRetiredCommented:
Ah.

In my tests, I had only one type of table in the referenced libraries, so I didn't notice that this line could be ambiguous:

Dim tbl As Table

You would need to specify what sort of table with:

Dim tbl As Word.Table




0
 
GrahamSkanRetiredCommented:
It looks as if you have the Microsoft ADO Ext for DDL and security referenced, and that it is earlier in your reference list than the Word library, so VB assumed that it to be an ADOX table.

Apologies.
0
 
MKadricAuthor Commented:
If I could I would give you a 1000 points.  THANK YOU SO MUCH
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.