Duplicating a range?

B

Bob S

VBA is like Freudian analysis; if you aren't crazy when you start, you
will be when you finish...

rngWork.Select
' rngWork is several characters in the middle of a line/paragraph

Set rngKillChar = rngWork.Duplicate
rngKillChar.Collapse Direction:=wdCollapseStart
rngKillChar.Select
Selection.TypeBackspace
' deletes the desired character just to the left of the original range

rngWork.Select

At this point rngWork starts at the beginning of the line! How did the
start of rngWork get moved?

Bob S
 
J

Jay Freedman

Hi Bob,

Yes, it will drive you 'round the bend eventually. Before then,
though, let me beat on you some more about not using the Selection
except when absolutely necessary, and especially about not mixing the
Selection and Ranges in odd ways.

The code you showed does indeed misbehave badly. However, this version
is quite docile:

Set rngKillChar = rngWork.Duplicate
rngKillChar.Collapse Direction:=wdCollapseStart
rngKillChar.MoveStart unit:=wdCharacter, Count:=-1
rngKillChar.Delete
' deletes the desired character just to the left
' of the original range
rngWork.Select
 
B

Bob S

Hi Bob,

Yes, it will drive you 'round the bend eventually. Before then,
though, let me beat on you some more about not using the Selection
except when absolutely necessary, and especially about not mixing the
Selection and Ranges in odd ways.

The code you showed does indeed misbehave badly. However, this version
is quite docile:

Set rngKillChar = rngWork.Duplicate
rngKillChar.Collapse Direction:=wdCollapseStart
rngKillChar.MoveStart unit:=wdCharacter, Count:=-1
rngKillChar.Delete
' deletes the desired character just to the left
' of the original range
rngWork.Select


Yes, I had been trying to avoid using Delete because that was what had
failed terribly in the previous case, whereas Selection.TypeBackspace
had worked...

Bob S
 

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