Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
Word
application.errors
conversions
docmanagement
drawing.graphics
formatting.longdocs
international
internet.assistant
mail
mailmerge.fields
menustoolbars
newusers
numbering
oleinterop
pagelayout
printingfonts
setup.networking
spelling.grammar
tables
vba.addins
vba.beginners
vba.customization
vba.general
vba.userforms
web.authoring
word6-7macros
word97vba
  
 
date: Wed, 21 Nov 2007 23:07:02 -0800,    group: microsoft.public.word.vba.beginners        back       


Problem with macro (cut out old table, replace with new table)   
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
date: Wed, 21 Nov 2007 23:07:02 -0800   author:   elle0612

Re: Problem with macro (cut out old table, replace with new table)   
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"  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
>
date: Fri, 23 Nov 2007 05:10:22 +1000   author:   Doug Robbins - Word MVP

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

It is Selection.Cut that is hightlighted

"Doug Robbins - Word MVP" wrote:

> 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"  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
> > 
> 
> 
>
date: Sat, 24 Nov 2007 09:05:01 -0800   author:   elle0612

Re: Problem with macro (cut out old table, replace with new table)   
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
date: Sat, 24 Nov 2007 21:11:12 +0100   author:   Helmut Weber

Re: Problem with macro (cut out old table, replace with new table)   
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
date: Tue, 27 Nov 2007 18:33:07 GMT   author:   fumei via OfficeKB.com u37563@uwe

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us