We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Updating a table in the Header of Word using VB6

Medium Priority
886 Views
Last Modified: 2012-05-11
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
Comment
Watch Question

Retired
CERTIFIED EXPERT
Top Expert 2012
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
tbl.Cell

Compile error memer or data member not found.

It does not have ".cell" as a member

Author

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.
GrahamSkanRetired
CERTIFIED EXPERT
Top Expert 2012

Commented:
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,

Author

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?

Author

Commented:
Also using Word 2003
GrahamSkanRetired
CERTIFIED EXPERT
Top Expert 2012

Commented:
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




GrahamSkanRetired
CERTIFIED EXPERT
Top Expert 2012

Commented:
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.

Author

Commented:
If I could I would give you a 1000 points.  THANK YOU SO MUCH
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.