Cutting and copying a list in VBA

A

Alan Stancliff

In Word 2003, how can I cut a list that is under my cursor and copy it
using VBA? The list is created with Word's auto-numbering and
auto-outlining feature.

Regards,

Alan
 
D

David Sisson

Dim Rng As Range

'Anything selected will now be in Rng
Set Rng = Selection.Range

'put somewhere else
ActiveDocument.FormFields(1).Result = Rng

If you really want to cut it...

Rng.Delete

or you could use the selection directly
Selection.Copy
or
Selection.Cut

It's usually better to deal with ranges if you can.
 
A

Alan Stancliff

Hi David,

Thanks for the feedback. However, I seem to have generated an error when
I attempted to run this bit of code:

Sub eraseme()
Dim Rng As Range

'Anything selected will now be in Rng
Set Rng = Selection.Range

'put somewhere else
ActiveDocument.FormFields(1).Result = Rng

End Sub

This was the error message that VBA generated:
Run-time error '5941':
The requested member of the collection does not exist

I had text in the document, along with list consisting of an
automatically-created numbered list, followed by more text. I placed the
cursor in the list, pressed <alt><F8> and ran the macro when I generated
the error message.

Can you suggest what I should do now?

Regards,

Alan
 
H

Helmut Weber

Hi Alan,

there must be other ways,
but this one works for me:

Sub Test400t89()
With Selection
.Collapse
If .Range.ListParagraphs.Count > 0 Then
.Paragraphs.First.Range.Select
End If
While .Paragraphs.First.Previous.Range.ListParagraphs.Count > 0
.MoveStart unit:=wdParagraph, Count:=-1
Wend
While .Paragraphs.Last.Next.Range.ListParagraphs.Count > 0
.MoveEnd unit:=wdParagraph, Count:=1
Wend
End With
End Sub


Cutting and pasting, I think, would not be a problem.
--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
H

Helmut Weber

....
which will rise an error,
if either one or both of these conditions are true:
there is no previous paragraph
there is no next paragraph.

Post back, if you need a solution for that, too.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
A

Alan Stancliff

Hello Helmut,

Thank you so much for the code snippet. That worked exactly the way I
wanted it to. Moreover, it gave me some things to research and learn from.

Regards,

Alan
 

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