我正在尝试将MS Word 2010中的编号列表中的项目混洗。这个问题的背景是我的妻子是一位使用Word进行测试的英语老师。每当她进行测试时,她也会通过更改编号列表中项目的顺序来制作第二个版本。
我期待:
使用代码后,所有列表应保持相同的格式(即起始编号)。
我尝试了第一个例子,但没有成功确定我的选择的开始和结束行号。
示例:
原件:
=====开始:========
问题1答案是正确的?
问题2答案是正确的?
问题3答案是正确的?
======结束======
宏应该创建:
======开始======
问题1答案是正确的?
问题2答案是正确的?
问题3答案是正确的?
====结束==
答案 0 :(得分:0)
由于您只处理3个列表项,因此非常简单。只需交换这两个项目中的任何一个。以下代码也是如此。
对于3个以上的项目,您可能需要重复交换更多行的逻辑。但是你应该从这段代码中得到关于如何解决它的基本想法。
Sub Shuffle()
Dim li As List, rng As Range, random As Integer
Randomize
For Each li In ThisDocument.Lists
' get either 1 or 2. We will swap this with the 3rd item
random = CInt(Rnd + 1)
' add a new paragraph as temporary place holder. This is so that we can keep the paragraph with its formatting intact.
Set rng = li.Range.Paragraphs.Add.Range
rng.FormattedText = li.Range.Paragraphs(random).Range.FormattedText
' swap the items
li.Range.Paragraphs(random).Range.FormattedText = li.Range.Paragraphs(3).Range.FormattedText
li.Range.Paragraphs(3).Range.FormattedText = rng.FormattedText
' remove the temporary paragraph we added
li.Range.Paragraphs.Last.Range.Delete
Next
End Sub
答案 1 :(得分:0)
我稍微修改了Pradeep Kumar的代码,这就像一个魅力,即使每个编号列表中包含未知数量的项目,也可以将其合并到normal.dot模板中:
Sub Shuffle()
Dim li As List, rng As Range, random As Integer, nbr As Integer
Application.ScreenUpdating = False
Randomize
For Each li In ActiveDocument.Lists
nbr = li.CountNumberedItems
' Run along all items in list and swap with a random one from the same list
For a_counter = 1 To nbr
' Make sure the item is not swapped with itself, that would fail
again:
random = CInt((nbr - 1) * Rnd + 1)
If random = a_counter Then GoTo again
' add a new paragraph as temporary place holder. This is so that we can keep the paragraph with its formatting intact.
Set rng = li.Range.Paragraphs.Add.Range
rng.FormattedText = li.Range.Paragraphs(random).Range.FormattedText
' swap the items
li.Range.Paragraphs(random).Range.FormattedText = li.Range.Paragraphs(a_counter).Range.FormattedText
li.Range.Paragraphs(a_counter).Range.FormattedText = li.Range.Paragraphs(nbr + 1).Range.FormattedText
' remove the temporary paragraph we added
li.Range.Paragraphs(nbr + 1).Range.Delete
Next a_counter
Next
Application.ScreenUpdating = True
End Sub