Range object vs. Selection object

Z

zSplash

I'm just not "getting" this conversion from Selection object thinking to
Range object thinking!

My code was:
Sub oldCode() 'removes Doc3 from packet
DoEvents
ActiveDocument.Bookmarks("Doc3").Select
ActiveDocument.Bookmarks("\Page").Select
Selection.Cut
End Sub

Now, I've added another bookmark when trying to convert the code to a Range
object, but I get an error (4128 - Type Mismatch) with:
Sub newCode() 'removes Doc3 from packet
Dim myRange As Range, myRange2 As Range
Set myRange = ActiveDocument.Bookmarks("begDoc3").Range
Set myRange2 = ActiveDocument.Bookmarks("endDoc3").Range
ActiveDocument.Range(myRange, myRange2).Delete
End Sub

What should I be declaring (instead of a Range), or what am I missing?

TIA
 
Z

zSplash

So, now I've eeked-out this, and it sorta works. Is this good code? Is
this the most efficient way to do what I need to do? Am I more on the right
track?

Sub newCode()
Dim myRange As Range, myRange2 As Range
Dim pos1, pos2
pos1 = ActiveDocument.Bookmarks("begDoc3").Start
pos2 = ActiveDocument.Bookmarks("endDoc3").End
Set myRange = ActiveDocument.Range(pos1, pos2)
myRange.Delete
End Sub

TIA

st.
 
J

Jonathan West

zSplash said:
So, now I've eeked-out this, and it sorta works. Is this good code? Is
this the most efficient way to do what I need to do? Am I more on the
right
track?

Sub newCode()
Dim myRange As Range, myRange2 As Range
Dim pos1, pos2
pos1 = ActiveDocument.Bookmarks("begDoc3").Start
pos2 = ActiveDocument.Bookmarks("endDoc3").End
Set myRange = ActiveDocument.Range(pos1, pos2)
myRange.Delete
End Sub

You can do the same with a one-liner

ActiveDocument.Range(ActiveDocument.Bookmarks("begDoc3").Start, _
ActiveDocument.Bookmarks("endDoc3").End).Delete


Remember that a Range is an object, which you manipulate in much the same
way as the Selection. You can delete it, change its font etc. It has
properties, such as Start and End. Don't confuse the object with its
properties.
 

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