473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Create table in Word Document From Access using VBA

2
I am trying to create tables in a Word document template from my Access database.

This bit of code runs fine from Word itself and creates tables as required. I was wondering if its possible to run this code from Access and point to a specific word document in which to create the tables.

Expand|Select|Wrap|Line Numbers
  1. Dim numberOfTables As Integer 
  2. Dim iCount As Integer 
  3.  
  4. numberOfTables = InputBox("How many tables to make?", "Tables") 
  5.  
  6. For iCount = 0 To numberOfTables - 1 
  7.  
  8.     ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _ 
  9.     3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _ 
  10.     wdAutoFitFixed 
  11.     With Selection.Tables(1) 
  12.         If .Style <> "Table Grid" Then 
  13.             .Style = "Table Grid" 
  14.         End If 
  15.         .ApplyStyleHeadingRows = True 
  16.         .ApplyStyleLastRow = False 
  17.         .ApplyStyleFirstColumn = True 
  18.         .ApplyStyleLastColumn = False 
  19.          '.ApplyStyleRowBands = True 'Office 2010
  20.          '.ApplyStyleColumnBands = False 'Office 2007
  21.     End With 
  22.  
  23.     Selection.EndKey Unit:=wdStory 
  24.     Selection.TypeParagraph 
  25.  
  26.      Next iCount 
  27.  
May 12 '16 #1
3 2704
ADezii
8,834 Expert 8TB
Using simplified Automation Code from Access along with Early Binding, the following segment will open a Word Document named Test.doc in the C:\Test Folder (C:\Test\Test.doc) and create a Table with 25 Rows and 3 Columns. Some simplified formatting will also be applied. It should be very easy to expand this Demo Code:
Expand|Select|Wrap|Line Numbers
  1. Dim appWord As Word.Application
  2. Dim wdDoc As Word.Document
  3.  
  4. Set appWord = New Word.Application
  5.  
  6. appWord.Visible = True
  7.  
  8. Set wdDoc = appWord.Documents.Open(Filename:="C:\Test\Test.doc", ReadOnly:=False)
  9.  
  10. wdDoc.Tables.Add Range:=appWord.ActiveDocument.Range(0, 0), NumRows:=25, NumColumns:=3
  11.  
  12. With wdDoc.Tables(1)
  13.   .ApplyStyleHeadingRows = True
  14.   .ApplyStyleLastRow = False
  15.   .ApplyStyleFirstColumn = True
  16.   .ApplyStyleLastColumn = False
  17. End With
May 12 '16 #2
imo92
2
Thanks this works however how would I create this table between two paragraphs in my document example as this creates the table at the start of the document
May 16 '16 #3
ADezii
8,834 Expert 8TB
I would imagine that you need to specify the end of Paragraph 1 as the Range Argument to the Tables.Add Method, something like:
Expand|Select|Wrap|Line Numbers
  1. '************** AIRCODE NOT TESTED! **************
  2. wdDoc.Tables.Add Range:=appWord.ActiveDocument.Paragraphs(1).Range.End, NumRows:=3, NumColumns:=3
May 16 '16 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: di | last post by:
I have a Access Database, and I would like to create a word document that (preferable would filter)links to ACCESS table or query. I would like to print the word document on the filtered record...
3
by: Michael Tkachev | last post by:
Hi Everybody! I have a problem. When I get a HTML of my page that I'm rendering. I need to generate a word-document from a html. And than I have to send this word-document to the users through...
0
by: jayne | last post by:
I have created the following code the first part works to create a word document with but when I have created the document and existed word, I then click the button again to open up the document to...
1
by: prashantmalikiec | last post by:
Hi all, i am new to .net and my problem is that i am not able to create a ms word document using .net. i dont know how to write some text(hello world) into the word document thanks,...
0
by: gopikrishnan | last post by:
Hi to all, i am developing a java project which workes in linux. I need to create a Word document through my java program, rep_path = --FIlePath-- fso = new File(rep_path); if (!...
0
by: 5anelli | last post by:
Hi to everyone, I need to create and print (from within a c# application) a MS Word document starting from an Access db. Half of this document must be written and printed in, let's say, January,...
1
by: Jai | last post by:
Hi, Is it possibe to update a word document template using ASP.NET 2.0 Example: This is a word template. Employee Name: <Name> Employee No: <EmpNo>
2
by: jeevalokesh | last post by:
Hi I'm new to forms.could any one tell me how to call the word document by using forms.
1
by: devsri79 | last post by:
How to find the word in the word document by using the c#
1
by: SowAruna | last post by:
can anyone one help me how to write the below code in C#.NET i am able to create the wordapp and openthe document but further things like compare word document content and not able to.... plz help...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.