Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to VBA add new text to a word table located in document header ?

Posted on 2013-11-29
8
Medium Priority
?
4,589 Views
Last Modified: 2013-11-30
Dear Expert,

I have a word document contained two tables.

One table (Table 0) is located in the document header consists of two column and 3 rows, the number of rows and columns are FIXED.

Another table (Table 1) is located in the document body where data from Access recordsets will be filled via vba like code below:

Private Sub

    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim wd As Object
    Dim fld As Field
    Dim iRow As Integer
    Dim iCol As Integer
    Dim iTbl As Integer

    Set wd = CreateObject("Word.Application")
    Set mydoc = wd.Documents.Open("C:\Sample.doc")
    Set db = CurrentDb()
     
   
    ' ** Starts Filling In Field Data into Table 1 Located in Document Body **  

    Set rs = db.OpenRecordset("MyRecordSets")
             
    iTbl = 1      'Existing table number in Word document body
    iRow = 1    'Starting row in table
    wd.Visible = True
       
    Do Until rs.EOF
       
        If iRow > mydoc.tables(iTbl).Rows.Count Then
            mydoc.tables(iTbl).Rows.Add
        End If
       
        For Each fld In rs.Fields
            iCol = iCol + 1
            mydoc.tables(iTbl).Cell(iRow, iCol).Range.Text = Nz(fld.Value)
        Next
        rs.MoveNext
        iRow = iRow + 1
        iCol = 0
    Loop

   
    Set rs = Nothing
    Set mydoc = Nothing
    Set wd = Nothing
   

End Sub

My question is how do I programmatically add text to rows of Table 0 located in Document Header before the transfer of recordsets into Table 1?

Attached is the graphical explanation of the sample document, the gray lined table represent Table 0 located in document header.

Thanks in advance.
Example.JPG
0
Comment
Question by:kaysoo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 40

Expert Comment

by:als315
ID: 39685796
Can you upload your Word document?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39685873
Here is some code to put some text in the table.
Dim tbl As Table

Set tbl = mydoc.Sections.First.Headers(wdHeaderFooterPrimary).Range.Tables(1)
tbl.Cell(1, 1).Range.Text = "Some text"
tbl.Cell(1, 2).Range.Text = "Some more text"

Open in new window

0
 

Author Comment

by:kaysoo
ID: 39686508
Thanks for reply.

GrahamSkan, I hv tried your codes but I received some errors, I execute the VB codes from a command button in Access 2003 form.

The error shown as attached file. I also upload the sample word document as reference.

Hope you guys can help.

Thanks
CompileError.JPG
FC0211sample.doc
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 40

Accepted Solution

by:
als315 earned 1400 total points
ID: 39686547
You can use GrahamScan's code with small modifications:
   
Set tbl = mydoc.Sections.First.Headers(1).Range.Tables(1)
    tbl.Cell(1, 2).Range.Text = "String1"
    tbl.Cell(2, 2).Range.Text = "String2"
    tbl.Cell(3, 2).Range.Text = "String3"
    Set tbl = Nothing

Open in new window

0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 600 total points
ID: 39686555
You are getting a Run-time error '5941' : The requested member of the collection does not exist. It is on the 'Set tbl' line.

The code works for me with the document. that you posted

There is such a Section - all documents have a First Section. The Section does have a Primary Header. In the document that you posted there is a table in the header, so it is difficult to know why it doesn't work for you.

This version splits the line up so you can see which item from which collection can't be found.
Dim tbl As Table
Dim sec As Section
Dim hdr As HeaderFooter

Set sec = mydoc.Sections.First
Set hdr = sec.Headers(wdHeaderFooterPrimary)
Set tbl = hdr.Range.Tables(1)

'Set tbl = mydoc.Sections.First.Headers(wdHeaderFooterPrimary).Range.Tables(1)
tbl.Cell(1, 1).Range.Text = "Some text"
tbl.Cell(1, 2).Range.Text = "Some more text"

Open in new window

0
 

Author Closing Comment

by:kaysoo
ID: 39686744
Thx guys for your solution, it works great.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39686749
Did you find out what member couldn't be found?
0
 

Author Comment

by:kaysoo
ID: 39686753
I believed it was an Access version issue, e.g. Dim tbl As Table doesnt work for me cause my version of VB editor does not have this function, I have to change it to Dim tbl As Object instead.

Dim hdr as HeaderFooter also not classified as a function in my older version of VB editor.

AFter I modified your suggested codes by implementing suggestion from als315

Set tbl = mydoc.Sections.First.Headers(1).Range.Tables(1)

it solved all the compile errors.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

636 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