Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Access 2003 VBA to Duplicate a MS Word Table

Posted on 2014-04-26
5
Medium Priority
?
1,302 Views
Last Modified: 2014-04-27
Dear Experts,

I am using Access 2003 to design a Solution where I required to click a command button from Access Form to transfer a recordset to an opened Word Document.

Assuming in the opened Word Document already contained a single template table(1), but if I need to programmatically create a second table(2) based on the template table(1), the copied table(2) should be located under the template table(1) in the same Word Document Page separated by a single paragraph, how do I do it?

I hv no problem transferring the Access recordset into the table(1), I just need the vba code to duplicate an extra table from template table in same Word page if needed.

Thanks in advance.
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
  • 2
  • 2
5 Comments
 
LVL 39

Accepted Solution

by:
thenelson earned 1200 total points
ID: 40025044
If I understand you correctly.

To copy the table:
    Selection.Tables(1).Select
    Selection.Copy

Then move to where you want to paste the copy. One move example:
    Selection.MoveDown Unit:=wdLine, Count:=1

Then paste what you copied:
    Selection.PasteAndFormat (wdPasteDefault)
0
 

Author Comment

by:kaysoo
ID: 40025370
Tq for your reply Mr thenelson, yes you understand my request for help correctly.

I tried your code, there are no syntax error but the Table wasn't copied and pasted as planned too, then MS Access 2003 pop up a message saying "OBJECT REQUIRE".

What is that means?

I attached a sample document for ref and the coding as well.

Private Sub Command96_Click()
On Error GoTo Err_Command96_Click

   
Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim wd As Object
   
    Set wd = CreateObject("Word.Application")
    Set mydoc = wd.Documents.Open("C:\Program Files\Test.docx")
    Set db = CurrentDb()
    wd.Visible = True
     
    'To copy the table:
    Selection.Tables(1).Select
    Selection.Copy
   
    'Then move to where you want to paste the copy. One move example:
    Selection.MoveDown Unit:=wdLine, Count:=100
       
       
    'Then paste what you copied:
    Selection.PasteAndFormat (wdPasteDefault)
   
   
   
Exit_Command96_Click:
    Exit Sub

Err_Command96_Click:
    MsgBox Err.Description
    Resume Exit_Command96_Click
End Sub
Sample-Word-Doc.jpg
0
 
LVL 52

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 800 total points
ID: 40025607
Hi,

pls try

Const wdLine = 5
Const wdPasteDefault = 0
    Set wd = CreateObject("Word.Application")
    Set mydoc = wd.Documents.Open("C:\Program Files\Test.docx")
    Set db = CurrentDb()
    wd.Visible = True
      
    'To copy the table:
    wd.Selection.Tables(1).Select
    wd.Selection.Copy
    
    'Then move to where you want to paste the copy. One move example:
    wd.Selection.MoveDown Unit:=wdLine, Count:=100
        
        
    'Then paste what you copied:
    wd.Selection.PasteAndFormat (wdPasteDefault)

Open in new window

Regards
0
 

Author Closing Comment

by:kaysoo
ID: 40025968
Thanks guys, both of your advice gave me a big help with some slight modification of my own.

'To copy the table:
    wd.Selection.Tables(1).Select
    wd.Selection.Copy

Changed to:

'To copy the table:
    mydoc.Tables(1).Select
    wd.Selection.Copy

the rest of the codes were combination from both comments and it copy and paste as intended.

Thanks again.
0
 
LVL 39

Expert Comment

by:thenelson
ID: 40025998
Your welcome, glad I could help.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

721 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