|
| View previous topic :: View next topic |
| Author |
Message |
elle0612 Guest
|
Posted: Thu Nov 22, 2007 7:07 am Post subject: Problem with macro (cut out old table, replace with new tabl |
|
|
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 |
|
| Back to top |
|
 |
Google Sponsor

|
Posted: Thu Nov 22, 2007 7:07 am Post subject: Advertisement |
|
|
|
|
| Back to top |
|
 |
Doug Robbins - Word MVP Guest
|
Posted: Thu Nov 22, 2007 7:10 pm Post subject: Re: Problem with macro (cut out old table, replace with new |
|
|
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
"elle0612" <elle0612@discussions.microsoft.com> wrote in message
news:262F2BE0-E75F-491A-8EED-E3B3AE3E2F95@microsoft.com...
| Quote: | 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
|
|
|
| Back to top |
|
 |
elle0612 Guest
|
Posted: Sat Nov 24, 2007 5:05 pm Post subject: Re: Problem with macro (cut out old table, replace with new |
|
|
Hi
It is Selection.Cut that is hightlighted
"Doug Robbins - Word MVP" wrote:
| Quote: | 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
"elle0612" <elle0612@discussions.microsoft.com> wrote in message
news:262F2BE0-E75F-491A-8EED-E3B3AE3E2F95@microsoft.com...
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
|
|
|
| Back to top |
|
 |
Helmut Weber Guest
|
Posted: Sat Nov 24, 2007 8:11 pm Post subject: Re: Problem with macro (cut out old table, replace with new |
|
|
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 |
|
| Back to top |
|
 |
fumei via OfficeKB.com Guest
|
Posted: Tue Nov 27, 2007 6:33 pm Post subject: Re: Problem with macro (cut out old table, replace with new |
|
|
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"
--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200711/1 |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|