473,505 Members | 14,394 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Create and Customize a Word Table Doc using vb6

28 New Member
Hi guys, I need to learn how to create a MS-Word Table using the language VB6.
This is all I could do so far:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2. Dim Word_App            As Word.Application
  3. Dim Word_Doc            As Word.Document
  4. Dim Word_Table          As Word.Table
  5. Dim Word_Range          As Word.Range
  6. Dim iCount              As Integer
  7.  
  8. Set Word_App = New Word.Application
  9. Set Word_Doc = Word_App.Documents.Add(DocumentType:=wdNewBlankDocument)
  10. Set Word_Table = Word_Doc.Tables.Add(Word_App.Selection.Range, 3, 5)
  11. Set Word_Range = Word_App.ActiveDocument.Content
  12.  
  13. Set Word_Doc = Word_App.ActiveDocument
  14. Set Word_Table = Word_Doc.Tables.Add(Range:=Word_Doc.Range(Start:=0, End:=0), NumRows:=2, NumColumns:=2)
  15. iCount = 1
  16.  
  17. Word_Doc.Tables(1).AutoFitBehavior (wdAutoFitContent)
  18.  
  19.  
  20. For Each Cell In Word_Table.Range.Cells
  21.     Cell.Range.InsertAfter "Hello World " & iCount
  22.     iCount = iCount + 1
  23. Next Cell
  24.  
  25.  
  26. Word_Doc.SaveAs FileName:="C:\p\Test"
  27. Word_Doc.Close False
  28. Word_App.Quit False
  29.  
  30. End Sub
I'd like to know if this is the best way to create the table. Also I'd like to know how to set the column Width, borders and colors and how may I change the space between cells ?

Thanks
Aug 29 '13 #1
9 5664
Rabbit
12,516 Recognized Expert Moderator MVP
The easiest way to find out the code to do that is to open word and record a macro of you doing all that manually. Then you can just copy and modify the code the macro created for you.
Aug 29 '13 #2
Ghaleon
28 New Member
How may I do that? Sorry I never used it in ms-word x-x
Aug 29 '13 #3
Rabbit
12,516 Recognized Expert Moderator MVP
That depends on what version of word you are using. In 2007, you click developer and then click record macro.
Aug 29 '13 #4
Ghaleon
28 New Member
It's Office 2007, I already checked the checkbox to show the Developer Bar.
Now I have two options:
Button & Keyboard. Wich one should I pick ? Also, where am I supposed to get the code after the record?
Thanks
Aug 29 '13 #5
Rabbit
12,516 Recognized Expert Moderator MVP
It doesn't matter which you pick because you don't actually need to save the code or document once you're done. You don't even have to pick one.

After you finish recording, you can see the code for the macro by clicking macro. Select the macro you just recorded. Click edit. It will take you to the code.
Aug 29 '13 #6
Ghaleon
28 New Member
Thanks, worked perfect !! But what IF I wanted to change manually the size of the rows and cells like Height and Width ? How could I do that?
Aug 29 '13 #7
Rabbit
12,516 Recognized Expert Moderator MVP
You don't have to use the code it creates exactly. Just use it as a template for the methods and objecst you need to use. You can plug in variables for any of the hard-coded values such as the height and width.
Aug 29 '13 #8
Ghaleon
28 New Member
Thanks man, you're awesome !! Just one more little thing, I need to understand the syntax. When I'm ONLY creating the Table, it works perfect. But if I try to insert a picture before that I create the table. An error happens:
This is the code that I use to insert a picture.
Expand|Select|Wrap|Line Numbers
  1. Word_App.Selection.InlineShapes.AddPicture FileName:="C:\Documents and Settings\All Users\Documentos\Minhas imagens\Amostras de imagens\Inverno.jpg", SaveWithDocument:=True
  2.  
When I use the Word_App again to insert values into the table, the error is fired. Why?
Expand|Select|Wrap|Line Numbers
  1. Word_App.Selection.TypeText Text:="Débito"
  2. Word_App.Selection.MoveRight Unit:=wdCell
An error is fired, like:
The required member of the collection does not exists.

Why?
Aug 29 '13 #9
Rabbit
12,516 Recognized Expert Moderator MVP
Most likely the selection has changed.
Aug 29 '13 #10

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

Similar topics

0
2329
by: nish | last post by:
Hi, I am struggling with this code. I am trying to create 3 separate tables and print them in word, however I end up with 1 big table and 1 nested table inside and 1 more nested table within...
0
1246
by: garth | last post by:
I created a Crystal Report that looks perfect when exported to Word, but the box and lines I use to simulate a table in the Crystal Report does not get created as a table in Word. Instead, it is...
3
5620
by: Ken Swinton | last post by:
I am developing a program that gathers inspection visit details and save them in string variables. I need to output this information to a Word template. I have attempted to use Bookmarks, so far...
1
3067
by: surveyor | last post by:
Hello everyone, I have used Microsoft Word 10 library for creating word document in VB6. But I see it is very slow. Please tell me how I can do faster? For creating document content I use...
1
1291
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
1066
by: menelty | last post by:
Hi, I have been trying to create a word document using vb.net. I have several fields I wish to populate so I am using a MS Access back end to spit out the requested data for these fields as the...
2
13562
by: parallax | last post by:
Hi guys, i would like to make create temporary table pagamentos SELECT d.name, SUM(advanced) AS total_advance, f.salario FROM data d JOIN empregados f ON f.name = d.name where MONTH(d.date) = 04...
0
2192
LegalIT
by: LegalIT | last post by:
Hello, I am creating a Word table using a VB.net application. I have it working with the following declaration: Dim table As Word.Table = myRange.Tables.Add(myRange, intRowCount, 5) I...
1
1432
by: pnalla | last post by:
hi how to create a word documetn using Excel macros.. i have some code but it crating a word document but it isnot close properly whats the proble. please suggest me. thanks prasad
4
2016
by: impin | last post by:
word document should display the details about the candidate. <?php include("config.php"); //$table = 'table_name'; $outstr = NULL; $id = $_GET ;
0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
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...
1
7018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.