Problem with macro (cut out old table, replace with new table)

E

elle0612

Hi

The following macro works ok if the user selects the table for cutting out
first, which he is supposed to, or, clicks either before or after the table
(because then there is a message box which asks the user to select the table
first).

However, if the user clicks inside the table to be cut, and then presses the
assigned macro button on the toolbar, a debug error message is generated (see
below). Can anyone help me with this workaround, how can I get the message
box to appear in this situation, asking the user to select the table first?

Here's the code,

Sub pastetable()

If Selection.Information(wdWithInTable) Then

Selection.Cut

ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2,
NumColumns:= _
5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
With Selection.Tables(1)
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
Selection.TypeText Text:="this"
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:="is"
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:="the"
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:="example"
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:="This"
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=4
Selection.TypeText Text:="is"
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:="example"
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:="of"
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:="macro"
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:="working"

Else
MsgBox "Please select a table."
End If

This is the error message I am getting,

"The method or property is not available because the object is empty".

Thanks
 
D

Doug Robbins - Word MVP

What line of the code is highlighted if you click on Debug?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
E

elle0612

Hi

It is Selection.Cut that is hightlighted

Doug Robbins - Word MVP said:
What line of the code is highlighted if you click on Debug?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
H

Helmut Weber

Hi Elle,

if the selection is the insertion point,
then, with selection.cut,
Word tells that the selection object is empty.

Which is correct, I'd say,
however, same situation,
len(selection.text) returns 1
and
selection.text
returns the first character after the insertion point,
which might not be a bug, but faulty design.






--

Gruß

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
F

fumei via OfficeKB.com

Selection.Cut will fail if the Selection is a point. The table itself is not
selected. So...select the table. The best way would be to make a table
object, and then delete it.

If Selection.Information(wdWithInTable) Then
Set aTable = Selection.Tables(1)
aTable.Delete

This means that the table the Selection is in - whether the Selection
includes the whole, or not - will be deleted. This is the power of objects.

Using Selection, especially to put text into cells is very inefficient. Here
is your code redone. If the Selection is in a table - again, it does not
matter if the whole table is selected, or not - that table is deleted.

A new table is made.

Your example text is in an array, The code goes through each cell in the new
table putting in the text from the array.

Sub pastetable()
Dim aTable As Table
Dim SomeText()
Dim var
SomeText = Array("this", "is", "the", "example", _
"of", "the", "macro", "working", _
"with", "text")

If Selection.Information(wdWithInTable) Then
Set aTable = Selection.Tables(1)
aTable.Delete

ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, _
NumColumns:=5, DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed

Set aTable = Selection.Tables(1)
For var = 1 To aTable.Range.Cells.Count
aTable.Range.Cells(var).Range.Text = _
SomeText(var - 1)
Next
Else
MsgBox "Please put cursor in the table to be actioned."
End If
End Sub

When working with tables you can also use Row objects, and Cell objects to
put things into...Rows and Cells. Using Selection.Move etc. etc. is not a
good way to work with tables in Word.

The text array is not a particularly good example, as you must make sure the
number of items in the array match the number of cells in the table.

In this case, I made sure they do, but I would think this would not be a
normal way of putting content into the table cells.

Here is an example of:

deleting the current table the Selection is in
making a new table
putting text into Cell (3, 2) - third row, column 2

Dim aTable As Table
Dim aCell As Cell

If Selection.Information(wdWithInTable) Then
Set aTable = Selection.Tables(1)
aTable.Delete

ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, _
NumColumns:=5, DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed

Set aTable = Selection.Tables(1)
Set aCell = aTable.Cell(3, 2)
acell.Range.Text = "yadda"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top