"Cleaning up" Range object

Z

zSplash

So, long ago I was advised to use the Range object over the Selection
objection in many cases. Finally, I am able to spend a little time
re-writing my code with Range objects. (Perhaps this will solve many of my
issues, and make my code less sloppy.)

Suppose I have a cell in a table whose text I want to "juggle" (i.e., as I
easily do with the Selection object, in my old way of thinking -- move left,
insert text, move right, wdExtend, delete, bold, etc.). So, I've "captured"
the value of the cellRange object that I want to "tweak" with a string
variable. How can I easily remove the all characters in that string
variable whose Asc value = 7 (for example) that I don't want my string? (I
just used to make a loop and if the last character in the selection was
Asc=7, I'd delete it and go through the loop again until asc <> 7.) There
must be an easier (and less sloppy) way!

Here's what I've got, but it's not good, yet -- somehow, I keep adding more
Asc=7 characters to the range! Uff-dah:

Sub test()
Dim theText 'As String
Dim rngTable As Table, rngCell As Cell
Set rngTable = ActiveDocument.Range.Tables(1)
Set rngCell = rngTable.Cell(4, 2)
CheckItAgain:
If Asc(Right(rngCell, 1)) = 7 Then
theText = rngCell
theText = Left(theText, Len(theText) - 1)
rngCell.Range = theText
GoTo CheckItAgain
End If
End Sub


I hope this question makes sense.

TIA
 
J

Jezebel

Dim pRange as Word.Range
Set pRange = ActiveDocument.Range.Tables(1).Cell(4, 2).Range
pRange = Replace(pRange, chr(7),"")
ActiveDocument.Range.Tables(1).Cell(4, 2).Range = pRange

Separately, you should read up on VBA's various loop constructions. There is
never a need for GOTO, except in error-handling.




sage news:[email protected]...
 
J

Jay Freedman

Really, this should be sufficient:

Dim pRange As Range
Set pRange = ActiveDocument.Tables(1).Cell(4, 2).Range
pRange.Text = Replace(pRange.Text, Chr$(7), "")

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
Z

zSplash

Thanks, guys. [one step closer...]

st.

Jay Freedman said:
Really, this should be sufficient:

Dim pRange As Range
Set pRange = ActiveDocument.Tables(1).Cell(4, 2).Range
pRange.Text = Replace(pRange.Text, Chr$(7), "")

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
H

Helmut Weber

Hi everybody,
hi zSplash,

how did chr(7) get into there at first?

Of course, except the one chr(7),
which is always at the end of a table cell.

This is equivalent to inserting a paragraph mark
before the cell's end:
Set pRange = ActiveDocument.Range.Tables(1).Cell(4, 2).Range
pRange = Replace(pRange, chr(7),"")

and this results in an endless loop:

Set pRange = ActiveDocument.Tables(1).Cell(1, 1).Range
While InStr(pRange, chr(7)) > 0
pRange.Text = Replace(pRange.Text, chr$(7), "")
Wend

Always define a cell's range, so that
the end-of-cell mark is excluded.

Except you want to split a range in a cell into an array
with chr(13) & chr(7) as delimiter...

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"








beware of chr(7) in table cells.

Thanks, guys. [one step closer...]

st.

Jay Freedman said:
Really, this should be sufficient:

Dim pRange As Range
Set pRange = ActiveDocument.Tables(1).Cell(4, 2).Range
pRange.Text = Replace(pRange.Text, Chr$(7), "")

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
H

Helmut Weber

Except you want to split a range in a _table_ <<<<
into an array with chr(13) & chr(7) as delimiter...
 
Z

zSplash

Thanks, Helmut. You saw my problem precisely, before I could even
understand what was happening.

Exactly. The chr(7) is the end of the table cell. When I use the earlier
suggested code [pRange = Replace(pRange, chr(7),"")] to replace the chr(7),
it seems to be multiplying it -- so instead of having one chr(7), I now have
two chr(7)...

Using your example, I'll keep messing with it and see if anything becomes
clearer.

st.

Helmut Weber said:
Hi everybody,
hi zSplash,

how did chr(7) get into there at first?

Of course, except the one chr(7),
which is always at the end of a table cell.

This is equivalent to inserting a paragraph mark
before the cell's end:
Set pRange = ActiveDocument.Range.Tables(1).Cell(4, 2).Range
pRange = Replace(pRange, chr(7),"")

and this results in an endless loop:

Set pRange = ActiveDocument.Tables(1).Cell(1, 1).Range
While InStr(pRange, chr(7)) > 0
pRange.Text = Replace(pRange.Text, chr$(7), "")
Wend

Always define a cell's range, so that
the end-of-cell mark is excluded.

Except you want to split a range in a cell into an array
with chr(13) & chr(7) as delimiter...

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"








beware of chr(7) in table cells.

Thanks, guys. [one step closer...]

st.

Jay Freedman said:
Really, this should be sufficient:

Dim pRange As Range
Set pRange = ActiveDocument.Tables(1).Cell(4, 2).Range
pRange.Text = Replace(pRange.Text, Chr$(7), "")

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

On Fri, 17 Mar 2006 12:55:39 +1100, "Jezebel"

Dim pRange as Word.Range
Set pRange = ActiveDocument.Range.Tables(1).Cell(4, 2).Range
pRange = Replace(pRange, chr(7),"")
ActiveDocument.Range.Tables(1).Cell(4, 2).Range = pRange

Separately, you should read up on VBA's various loop constructions.
There
is
never a need for GOTO, except in error-handling.




sage So, long ago I was advised to use the Range object over the Selection
objection in many cases. Finally, I am able to spend a little time
re-writing my code with Range objects. (Perhaps this will solve
many
of
my
issues, and make my code less sloppy.)

Suppose I have a cell in a table whose text I want to "juggle"
(i.e.,
as I
easily do with the Selection object, in my old way of thinking -- move
left,
insert text, move right, wdExtend, delete, bold, etc.). So, I've
"captured"
the value of the cellRange object that I want to "tweak" with a string
variable. How can I easily remove the all characters in that string
variable whose Asc value = 7 (for example) that I don't want my string?
(I
just used to make a loop and if the last character in the selection was
Asc=7, I'd delete it and go through the loop again until asc <> 7.) There
must be an easier (and less sloppy) way!

Here's what I've got, but it's not good, yet -- somehow, I keep adding
more
Asc=7 characters to the range! Uff-dah:

Sub test()
Dim theText 'As String
Dim rngTable As Table, rngCell As Cell
Set rngTable = ActiveDocument.Range.Tables(1)
Set rngCell = rngTable.Cell(4, 2)
CheckItAgain:
If Asc(Right(rngCell, 1)) = 7 Then
theText = rngCell
theText = Left(theText, Len(theText) - 1)
rngCell.Range = theText
GoTo CheckItAgain
End If
End Sub


I hope this question makes sense.

TIA
 
Z

zSplash

And how do I do this? "Always define a cell's range, so that the
end-of-cell mark is excluded."

st.
 
J

Jonathan West

zSplash said:
And how do I do this? "Always define a cell's range, so that the
end-of-cell mark is excluded."

Like this

Dim oRange as Range

Set oRange = ActiveDocument.Tables(1), Cell(1, 1)
oRange.MoveEnd Unit:=wdCharacter, Count:=-1

Now oRange is marking the contents of the cell, excluding the end of cell
marker.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
Z

zSplash

Thanks, Jonathan.

st.

Jonathan West said:
Like this

Dim oRange as Range

Set oRange = ActiveDocument.Tables(1), Cell(1, 1)
oRange.MoveEnd Unit:=wdCharacter, Count:=-1

Now oRange is marking the contents of the cell, excluding the end of cell
marker.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
Z

zSplash

Thanks, Jonathan.

st.

Jonathan West said:
Like this

Dim oRange as Range

Set oRange = ActiveDocument.Tables(1), Cell(1, 1)
oRange.MoveEnd Unit:=wdCharacter, Count:=-1

Now oRange is marking the contents of the cell, excluding the end of cell
marker.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
W

Word Heretic

G'day "zSplash" <zNOSPAMSplash@ gci.net>,

You cant replace those chr(7) in the doc content as they are a
dynamically constructed 'artifact' based on the table object to which
they belong. Ranges are held over document content, so rather you
would use:

MyText = Replace(pRange.text, chr(7),"")


Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


zSplash reckoned:
Thanks, Helmut. You saw my problem precisely, before I could even
understand what was happening.

Exactly. The chr(7) is the end of the table cell. When I use the earlier
suggested code [pRange = Replace(pRange, chr(7),"")] to replace the chr(7),
it seems to be multiplying it -- so instead of having one chr(7), I now have
two chr(7)...

Using your example, I'll keep messing with it and see if anything becomes
clearer.

st.

Helmut Weber said:
Hi everybody,
hi zSplash,

how did chr(7) get into there at first?

Of course, except the one chr(7),
which is always at the end of a table cell.

This is equivalent to inserting a paragraph mark
before the cell's end:
Set pRange = ActiveDocument.Range.Tables(1).Cell(4, 2).Range
pRange = Replace(pRange, chr(7),"")

and this results in an endless loop:

Set pRange = ActiveDocument.Tables(1).Cell(1, 1).Range
While InStr(pRange, chr(7)) > 0
pRange.Text = Replace(pRange.Text, chr$(7), "")
Wend

Always define a cell's range, so that
the end-of-cell mark is excluded.

Except you want to split a range in a cell into an array
with chr(13) & chr(7) as delimiter...

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"








beware of chr(7) in table cells.

Thanks, guys. [one step closer...]

st.

Really, this should be sufficient:

Dim pRange As Range
Set pRange = ActiveDocument.Tables(1).Cell(4, 2).Range
pRange.Text = Replace(pRange.Text, Chr$(7), "")

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

On Fri, 17 Mar 2006 12:55:39 +1100, "Jezebel"

Dim pRange as Word.Range
Set pRange = ActiveDocument.Range.Tables(1).Cell(4, 2).Range
pRange = Replace(pRange, chr(7),"")
ActiveDocument.Range.Tables(1).Cell(4, 2).Range = pRange

Separately, you should read up on VBA's various loop constructions. There
is
never a need for GOTO, except in error-handling.




sage So, long ago I was advised to use the Range object over the Selection
objection in many cases. Finally, I am able to spend a little time
re-writing my code with Range objects. (Perhaps this will solve many
of
my
issues, and make my code less sloppy.)

Suppose I have a cell in a table whose text I want to "juggle" (i.e.,
as I
easily do with the Selection object, in my old way of thinking -- move
left,
insert text, move right, wdExtend, delete, bold, etc.). So, I've
"captured"
the value of the cellRange object that I want to "tweak" with a string
variable. How can I easily remove the all characters in that string
variable whose Asc value = 7 (for example) that I don't want my string?
(I
just used to make a loop and if the last character in the selection was
Asc=7, I'd delete it and go through the loop again until asc <> 7.)
There
must be an easier (and less sloppy) way!

Here's what I've got, but it's not good, yet -- somehow, I keep adding
more
Asc=7 characters to the range! Uff-dah:

Sub test()
Dim theText 'As String
Dim rngTable As Table, rngCell As Cell
Set rngTable = ActiveDocument.Range.Tables(1)
Set rngCell = rngTable.Cell(4, 2)
CheckItAgain:
If Asc(Right(rngCell, 1)) = 7 Then
theText = rngCell
theText = Left(theText, Len(theText) - 1)
rngCell.Range = theText
GoTo CheckItAgain
End If
End Sub


I hope this question makes sense.

TIA
 

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