Solved

Word and Table in easy step

Posted on 2002-04-04
15
900 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 52

Expert Comment

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

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Suggested Solutions

Title # Comments Views Activity
Put text in a picture ASP.NET C# 2 73
Excel - Save a copy of work book 13 96
MS Access 03, TransferText, decimal places 8 92
Win 10 Automation Error with .doCmd in Access 2 94
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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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 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…

710 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