kaysoo
asked on
How to VBA add new text to a word table located in document header ?
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.Applica tion")
Set mydoc = wd.Documents.Open("C:\Samp le.doc")
Set db = CurrentDb()
' ** Starts Filling In Field Data into Table 1 Located in Document Body **
Set rs = db.OpenRecordset("MyRecord Sets")
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.Co unt Then
mydoc.tables(iTbl).Rows.Ad d
End If
For Each fld In rs.Fields
iCol = iCol + 1
mydoc.tables(iTbl).Cell(iR ow, 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
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.Applica
Set mydoc = wd.Documents.Open("C:\Samp
Set db = CurrentDb()
' ** Starts Filling In Field Data into Table 1 Located in Document Body **
Set rs = db.OpenRecordset("MyRecord
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.Co
mydoc.tables(iTbl).Rows.Ad
End If
For Each fld In rs.Fields
iCol = iCol + 1
mydoc.tables(iTbl).Cell(iR
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
Can you upload your Word document?
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"
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thx guys for your solution, it works great.
Did you find out what member couldn't be found?
ASKER
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.Heade rs(1).Rang e.Tables(1 )
it solved all the compile errors.
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.Heade
it solved all the compile errors.