我正试图在MS Word 2010的编号列表中对项目进行洗牌。这个问题的背景是我的妻子是一名英语老师,她用单词进行考试。每当她进行测试时,她还会更改编号列表中项目的顺序,从而产生第二个版本。
我希望:
使用代码后,所有列表都应保持相同的格式(即开始编号)。
我尝试了第一个实例,但没有成功地确定我选择的起始行号和结束行号。
示例:
原件:
===== Start:========
问题1什么答案是正确的?
问题2什么答案是正确的?
问题3什么答案是正确的?
======End========
宏应该创建以下内容:
======Start========
问题1什么答案是正确的?
问题2什么答案是正确的?
问题3什么答案是正确的?
====End======
发布于 2014-09-23 12:45:58
因为你只处理了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发布于 2014-09-24 09:07:45
我稍微修改了普拉迪普·库马尔的代码,这就像一种魅力,即使每个编号列表中的条目数量未知,也可以将其合并到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 Subhttps://stackoverflow.com/questions/25989326
复制相似问题