?
Solved

Word and Table in easy step

Posted on 2002-04-04
15
Medium Priority
?
929 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
  • 7
  • 6
  • 2
15 Comments
 
LVL 56

Expert Comment

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

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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

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 1000 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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

588 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