Solved

Word and Table in easy step

Posted on 2002-04-04
15
877 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

707 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

17 Experts available now in Live!

Get 1:1 Help Now