Solved

Word and Table in easy step

Posted on 2002-04-04
15
895 Views
Last Modified: 2007-12-19
Hi!

In Word i have to call

    Word.ActiveDocument.Tables.Add Range:=Selection.Range, numrows:=2, numcolumns:= _
        4, defaulttablebehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
        wdAutoFitContent

but if i do this in VB i get everytime an error (error 91). Can anybody help me?

Falke
0
Comment
Question by:Falke
[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
  • 7
  • 6
  • 2
15 Comments
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 6917574
Make sure you defined the Word As a Word Object..
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 6917579
Example:

Dim myWord As New Word.Application
    myWord.Visible = True
myWord.Documents.Open FilePath
.
.

Word.ActiveDocument.Tables.Add Range:=Selection.Range, numrows:=2, numcolumns:= _
       4, defaulttablebehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
       wdAutoFitContent

?
0
 

Author Comment

by:Falke
ID: 6917590
my code is:

Dim Wordapp as object
Set Wordapp = Createobject("Word.Application")
Wordapp.documents.add

Wordapp.activedocument.mailmerge.application.selection.instertafter "Test"

wordapp.activedocument.tables.add Range:=Selection.Range, NumRows:=2, NumColumns:=4,DefaultTableBehavior:=1;AutoFitBehavior:=1
wordapp.visible=True
wordapp.activate
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:WizOfOz
ID: 6917702
Try substitute
Range:=Selection.Range
with
Range:=wordapp.ActiveDocument.Application.Selection.Range

Hope this will help
0
 

Author Comment

by:Falke
ID: 6917920
it works .. but one problem after that

my first text ("TEST") has been deleted ...

how can i change this?

Falke
0
 

Expert Comment

by:WizOfOz
ID: 6918065
If you pass to tableadd a selection the resultant table will substitute the selection so add this line before the tableadd and the table will appear before the text

Wordapp.ActiveDocument.Application.Selection.Collapse

This will work

0
 

Author Comment

by:Falke
ID: 6918784
the collapse is good

but i want the text before the table :)

Thank you get the points :)

Falke
0
 

Expert Comment

by:WizOfOz
ID: 6919066
The Point isn't a problem...
if you type ( after
Wordapp.ActiveDocument.Application.Selection.Collapse

you will see that U can mention the direction of the collapse.
The default is left but U can specify right so the table will appear after the text
0
 

Author Comment

by:Falke
ID: 6919238
could you give me the tip ... which command .. the word help ist not very helpful :(

Falke
0
 

Expert Comment

by:WizOfOz
ID: 6920228
Collapse accept:
wdCollapseDirection ->never used..
wdCollapseStart ->collapse to the left
wdCollapseEnd -> collapse to the right

Just a tip, for every problem regarding methods of reference U mention in a project U could find everything with the object browser.
0
 

Author Comment

by:Falke
ID: 6920370
doesn't work ..

each time he insert it into the table!

not seperatly before or after!

Falke
0
 

Author Comment

by:Falke
ID: 6920371
should i copy the tabel in another document ? so i can insert it there , where i want?

Falke
0
 

Accepted Solution

by:
WizOfOz earned 250 total points
ID: 6920398
Actually your code could be as follow...
well in this way after the insertion of the text, the collapse put the pointer at the end of the original selection so that the table appear after the text.
Cut and paste the code, it works.


Dim Wordapp As Word.Application

Set Wordapp = New Word.Application

Wordapp.Documents.Add
Wordapp.Visible = True

Wordapp.ActiveDocument.Application.Selection.InsertAfter "Test"
Wordapp.ActiveDocument.Application.Selection.Collapse (wdCollapseEnd)
Wordapp.ActiveDocument.Tables.Add Range:=Wordapp.ActiveDocument.Application.Selection.Range, NumRows:=2, NumColumns:=4
Wordapp.Activate

0
 

Author Comment

by:Falke
ID: 6920411
good :)

i you get the points ... only one thing after all

how can i delete a column of the table and how can i write after the colum (you get some extra points indeed :) )

Falke
0
 

Expert Comment

by:WizOfOz
ID: 6920455
To write after the table select the table and then collapse at the end of it (just like you do for the text)
use the following code

Wordapp.ActiveDocument.Tables(1).Select
Wordapp.ActiveDocument.Application.Selection.Collapse (wdCollapseEnd)
Wordapp.ActiveDocument.Application.Selection.InsertAfter "Test"

To delete a column (or a row) you must point to the table using the right index (if U have only a table the index will be 1) and point to the column using the column number

Use the following code

Wordapp.ActiveDocument.Tables(1).Columns(2).Delete
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 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