Error with Selections 
Author Message
 Error with Selections

Thanks to everyone who has helped me through so far. Unfortunately,
I'm running into another problem. I'm trying to create a Word document
from a form in an Access database. If I set up a range, the following
code works:

        Dim oTable As Word.Table, aCell As Word.Cell
        Dim MyRange As Word.Range
        Dim intCol As Integer

        Set oTable = objWordDoc.Tables(1)

            For Each aCell In oTable.Rows(2).Cells
                intCol = aCell.ColumnIndex
                Set MyRange = aCell.Range
                Select Case intCol    ' What column are we in?
                Case 1 ' Item Number
                    MyRange.InsertAfter ("1")
                End Select
            Next aCell

However, if I try to set a selection, this code does not work:

        Dim oTable As Word.Table, aCell As Word.Cell
        Dim MySelection As Word.Selection
        Dim intCol As Integer

        Set oTable = objWordDoc.Tables(1)

            For Each aCell In oTable.Rows(2).Cells
                intCol = aCell.ColumnIndex
                Set MySelection = aCell.Select 'This is where the
problem is
            Next aCell

Instead, I get this error message:  Expected Function or Variable It
does not like the aCell.Select statement. Does anyone know what the
solution is?

Brandon



Sat, 31 Jul 2004 00:10:11 GMT  
 Error with Selections
Hi Brandon

Delete this line

Dim MySelection As Word.Selection

and change this line

    Set MySelection = aCell.Select

to this

    aCell.Select

The Selection is where the cursor is, and (except for some new Find features
in Word 2002 not supported by VBA) you can't have more than one
cursor/selection point active at a time, so you can't create Selection
objects. That's what Range objects are for.

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
Word FAQs at http://www.multilinker.com/wordfaq
Please post any follow-up in the newsgroup. I do not reply to Word questions
by email


Quote:
> Thanks to everyone who has helped me through so far. Unfortunately,
> I'm running into another problem. I'm trying to create a Word document
> from a form in an Access database. If I set up a range, the following
> code works:

>         Dim oTable As Word.Table, aCell As Word.Cell
>         Dim MyRange As Word.Range
>         Dim intCol As Integer

>         Set oTable = objWordDoc.Tables(1)

>             For Each aCell In oTable.Rows(2).Cells
>                 intCol = aCell.ColumnIndex
>                 Set MyRange = aCell.Range
>                 Select Case intCol    ' What column are we in?
>                 Case 1 ' Item Number
>                     MyRange.InsertAfter ("1")
>                 End Select
>             Next aCell

> However, if I try to set a selection, this code does not work:

>         Dim oTable As Word.Table, aCell As Word.Cell
>         Dim MySelection As Word.Selection
>         Dim intCol As Integer

>         Set oTable = objWordDoc.Tables(1)

>             For Each aCell In oTable.Rows(2).Cells
>                 intCol = aCell.ColumnIndex
>                 Set MySelection = aCell.Select 'This is where the
> problem is
>             Next aCell

> Instead, I get this error message:  Expected Function or Variable It
> does not like the aCell.Select statement. Does anyone know what the
> solution is?

> Brandon



Sat, 31 Jul 2004 01:21:05 GMT  
 Error with Selections
I was afraid something like that was happening. I guess that brings me
back to the "real" question that I was trying to answer for myself. I
am trying to do several things such as insert rows in a table and
insert formulas in a cell. The help in Word gives examples like this:

If Selection.Information(wdWithInTable) = True Then
    Selection.InsertRows NumRows:=2
    Selection.Borders.Enable =False
End If

I originally tried replacing "Selection" with "Range", but that did
not work. Is there another way to work with these objects without
using a Selection?

Brandon



Quote:
>Hi Brandon

>Delete this line

>Dim MySelection As Word.Selection

>and change this line

>    Set MySelection = aCell.Select

>to this

>    aCell.Select

>The Selection is where the cursor is, and (except for some new Find features
>in Word 2002 not supported by VBA) you can't have more than one
>cursor/selection point active at a time, so you can't create Selection
>objects. That's what Range objects are for.

>--
>Regards
>Jonathan West - Word MVP
>MultiLinker - Automated generation of hyperlinks in Word
>Conversion to PDF & HTML
>http://www.multilinker.com
>Word FAQs at http://www.multilinker.com/wordfaq
>Please post any follow-up in the newsgroup. I do not reply to Word questions
>by email



Sat, 31 Jul 2004 02:01:26 GMT  
 Error with Selections
Hi Brandon,

For most things, you can define a Range object variable called MyRange, and
then replace Selection with MyRange throughout the code.

There are a few methods of the Selection object that can't be applied to a
Range object, such as HomeKey, but there are almost always other ways of
doing the same things.

With the code sample you just gave, I'm pretty sure it will work using a
Range variable. Try this

Dim myRange as Range
Set myRange = Selection.Range
If myRange.Information(wdWithInTable) = True Then
    myRange.InsertRows NumRows:=2
    myRange.Borders.Enable =False
End If

By the way, the original code is inefficient. MS ought to have known better!
This line

    If Selection.Information(wdWithInTable) = True Then

could be simplified to this

    If Selection.Information(wdWithInTable) Then

For more on that topic, take a look at this article

The art of defensive programming
Or how to write code that will be easy to maintain
http://www.mvps.org/word/FAQs/MacrosVBA/MaintainableCode.htm

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
Word FAQs at http://www.multilinker.com/wordfaq
Please post any follow-up in the newsgroup. I do not reply to Word questions
by email


Quote:
> I was afraid something like that was happening. I guess that brings me
> back to the "real" question that I was trying to answer for myself. I
> am trying to do several things such as insert rows in a table and
> insert formulas in a cell. The help in Word gives examples like this:

> If Selection.Information(wdWithInTable) = True Then
>     Selection.InsertRows NumRows:=2
>     Selection.Borders.Enable =False
> End If

> I originally tried replacing "Selection" with "Range", but that did
> not work. Is there another way to work with these objects without
> using a Selection?

> Brandon



> >Hi Brandon

> >Delete this line

> >Dim MySelection As Word.Selection

> >and change this line

> >    Set MySelection = aCell.Select

> >to this

> >    aCell.Select

> >The Selection is where the cursor is, and (except for some new Find
features
> >in Word 2002 not supported by VBA) you can't have more than one
> >cursor/selection point active at a time, so you can't create Selection
> >objects. That's what Range objects are for.

> >--
> >Regards
> >Jonathan West - Word MVP
> >MultiLinker - Automated generation of hyperlinks in Word
> >Conversion to PDF & HTML
> >http://www.multilinker.com
> >Word FAQs at http://www.multilinker.com/wordfaq
> >Please post any follow-up in the newsgroup. I do not reply to Word
questions
> >by email



Sat, 31 Jul 2004 02:49:42 GMT  
 Error with Selections
Jonathan, thanks for your responses. Unfortunately, what you suggested
does not work. This is what I put in:

                aCell.Select
                Set MyRange = Selection.Range

You know how the VBA editor pops up available options as you type?
Well, back when I had MySelection.InsertRows, the editor said that was
a valid syntax. (The code still did not work, but the editor did
acknowledge it was an option.) However, MyRange.InsertRows does not
appear to be a valid option, and of course neither does the code work
when I run it.

There are 2 things that I know I want to be able to do: InsertRows and
InsertFormula. In the latter case, the Word help says the syntax is:

-----
expression.Formula(Formula, NumberFormat)
expression   Required. An expression that returns a Selection object.

InsertFormula Method Example

This example creates a table with three rows and three columns at the
beginning of the active document and then calculates the average of
all the numbers in the first column.

Set MyRange = ActiveDocument.Range(0, 0)
Set myTable = ActiveDocument.Tables.Add(MyRange, 3, 3)
With myTable
    .Cell(1, 1).Range.InsertAfter "100"
    .Cell(2, 1).Range.InsertAfter "50"
    .Cell(3, 1).Select
End With
Selection.InsertFormula Formula:="=Average(Above)"
-----

Unfortunately, I do not seem to be able to return a valid Selection
object, and replacing it with Range does not appear to work either.
You mentioned that there are almost always other ways of doing the
same things. This has also been my experience, but in this case I do
not know what options are open to me. I am open to any suggestions.
Thanks again for your previous insight.

Brandon
-----



Quote:
>Hi Brandon,

>For most things, you can define a Range object variable called MyRange, and
>then replace Selection with MyRange throughout the code.

>There are a few methods of the Selection object that can't be applied to a
>Range object, such as HomeKey, but there are almost always other ways of
>doing the same things.

>With the code sample you just gave, I'm pretty sure it will work using a
>Range variable. Try this

>Dim myRange as Range
>Set myRange = Selection.Range
>If myRange.Information(wdWithInTable) = True Then
>    myRange.InsertRows NumRows:=2
>    myRange.Borders.Enable =False
>End If



Sat, 31 Jul 2004 04:45:12 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Error with Selection.Find method

2. ..User.exe Error on selection of menu ed...

3. Error in Selection formula

4. TreevView selection - hierarchical selection

5. Capturing the selection from HTML selection lists

6. Compile ERROR because of Libraries selection????

7. Application.Selection Error

8. Selection.InsertFile triggers error on second run !

9. Run-time error when making selection in Word from Access

10. error 1004 with range selection in XL2000

11. Selection.InsertFile triggers error on second run !

12. Error 20515, on selection formula?

 

 
Powered by phpBB® Forum Software