Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Error "The range cannot be deleted"

64 views
Skip to first unread message

Ron

unread,
Jul 30, 2003, 11:57:33 AM7/30/03
to
Hi
I want to create tables and populate them with values I
retrieve from a text file. Basically, I get a lot of data
from one row in the file, create multiple tables, get the
next row and create the same tables over and over again,
until there is no more data available. I can create a
table, populate it with data, but am unable to get out of
the table and create another one directly after it
(underneath it). I get the error:
The range cannot be deleted

It works fine if I create a table, insert values/text into
a cell, move right, enter data, ....(see 2nd code snippet
below) but the code isn't as clear or as easy to maintain.

I've seen others with this error message (googled), but
didn't come across a solution which would fix my problem.

Here's part of my code that doesn't work:
------------- WHAT I WANT ------------------------
Do While Not EOF(1) 'Loop until end of file
Dim objTable As Table
Dim objCloseTable As Table
Set objCloseTable =
objNewDoc.Tables.Add(objNewDoc.Range, 2, 6)
objCloseTable.Cell(1, 1).Range.Text = "Other"
objCloseTable.Cell(1, 3).Range.Text = "Bid Dep."
objCloseTable.Cell(1, 5).Range.Text = "General"
objCloseTable.Cell(2, 1).Range.Text = "Closing:"
objCloseTable.Cell(2, 3).Range.Text = "Closing:"
objCloseTable.Cell(2, 5).Range.Text = "Closing:"

' read data from file into the following variables
Input #1, sListedAd, sAddenda, sCntID, sLocation, '...

objCloseTable.Cell(1, 2).Range.Text = sOtherClosingDate
objCloseTable.Cell(1, 4).Range.Text = sBidClosingDate
objCloseTable.Cell(1, 6).Range.Text = sGeneralClosingTime
objCloseTable.Cell(2, 2).Range.Text = sOtherClosingLocation
objCloseTable.Cell(2, 4).Range.Text = sBidClosingLocation
objCloseTable.Cell(2, 6).Range.Text = GeneralClosingLocation
Dim objLocTable As Table
Set objLocTable = objNewDoc.Tables.Add(objNewDoc.Range,
1, 2) '*ERROR
objLocTable .Cell(1, 1).Range.Text = "Location"
objLocTable .Cell(1, 2).Range.Text = sComments

Loop
------------- /WHAT I WANT ------------------------


Here's part of my code that works:
------------- WHAT I HAVE------------------------
'--- Closing Dates and Locations ---
ActiveDocument.Tables.Add Range:=Selection.Range,
NumRows:=2, NumColumns:= _
6, DefaultTableBehavior:=wdWord9TableBehavior,
AutoFitBehavior:= _
wdAutoFitFixed
Selection.TypeText Text:="Other"
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=sOtherClosingDate & " " &
sOtherClosingTime
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:="Bid"
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=sBidClosingDate & " " &
sBidClosingTime
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:="General"
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=sClosingDate & " " &
sGeneralClosingTime
Selection.TypeText Text:=sOtherClosingLocation
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.TypeText Text:="-"
------------- /WHAT I HAVE------------------------

I much prefer the first type of sample data above. Any
ideas on how I can get out of the table (from first example
above?), which I believe is the problem?

TIA

Ron

Jay Freedman

unread,
Jul 30, 2003, 1:55:05 PM7/30/03
to
Hi, Ron,

The fundamental cause of the error is that you're using objNewDoc.Range as
the range to insert the second table. VBA doesn't complain when you do that
with the first table, because up to that point the document is empty and you
aren't replacing anything. When you try it with the second table, VBA says
"whoa, there's something there already!" There's a distinction between a
Range object that you Dim and Set, vs. the Range property of a Document
object -- the latter has more restricted behavior.

The solution is to Dim your own Range object, set it to the same extent as
the document's range, and use that to create the second table.

Another problem is that you're trying to build the second table in contact
with the first table. Word immediately considers them to be just one table,
so objLocTable.Cell(1, 1) would refer to the same cell as
objCloseTable.Cell(1, 1). If you want to manipulate them separately, you
have to stick a temporary empty paragraph between the tables, do your stuff,
and then remove the paragraph mark.

Here's a slightly abbreviated sample:

Sub foo()
Dim objNewDoc As Document
Dim objCloseTable As Table

Dim oRange As Range

Set objNewDoc = Documents.Add
Set objCloseTable = _
objNewDoc.Tables.Add(objNewDoc.Range, 2, 6)
With objCloseTable
.Cell(1, 1).Range.Text = "Other"
.Cell(1, 3).Range.Text = "Bid Dep."
.Cell(1, 5).Range.Text = "General"
.Cell(2, 1).Range.Text = "Closing:"
.Cell(2, 3).Range.Text = "Closing:"
.Cell(2, 5).Range.Text = "Closing:"
End With

' snipped out reading from data table and adding
' data to table

' you need an independent Range object
' that starts out with the document's extent
Set oRange = objNewDoc.Range

' insert a temporary blank paragraph
' to separate the two tables
oRange.InsertParagraphAfter

' put the range at the end of the document
oRange.Collapse direction:=wdCollapseEnd

Dim objLocTable As Table
Set objLocTable = objNewDoc.Tables.Add(oRange, 1, 2)
objLocTable.Cell(1, 1).Range.Text = "Location"
objLocTable.Cell(1, 2).Range.Text = "Comments"

' eliminate the temporary paragraph between
' the tables, making them into one table
Set oRange = objLocTable.Range
With oRange
.Collapse direction:=wdCollapseStart
.Move unit:=wdParagraph, Count:=-1
.Delete
End With
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://www.mvps.org/word

0 new messages