Solved

Word and Table in easy step

Posted on 2002-04-04
15
881 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 49

Expert Comment

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

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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now