?
Solved

Updating a table in the Header of Word using VB6

Posted on 2011-04-19
9
Medium Priority
?
872 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
0
Comment
Question by:MKadric
  • 5
  • 4
9 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 35429229
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
 

Author Comment

by:MKadric
ID: 35429605
tbl.Cell

Compile error memer or data member not found.

It does not have ".cell" as a member
0
 

Author Comment

by:MKadric
ID: 35429629
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
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 35430174
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
 

Author Comment

by:MKadric
ID: 35431454
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
 

Author Comment

by:MKadric
ID: 35431473
Also using Word 2003
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 35431529
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 35431550
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
 

Author Comment

by:MKadric
ID: 35431889
If I could I would give you a 1000 points.  THANK YOU SO MUCH
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question