我需要一个单词宏,可以突出显示前2500个单词,以便稍后将它们复制到外部应用程序或将这2500个单词直接复制到剪贴板。
经过一些研究后,我找到了类似事物的例子,但是当它找到某个单词并以另一个单词结束时开始复制,而不是以单词的数量结束。
关于如何复制前x个单词的任何想法?
答案 0 :(得分:1)
以下是一个可以做到这一点的小例子:
Sub HighlightFirst2500()
counter = 0
For Each w In ThisDocument.Words
If w Like "*[0-z]*" Then
counter = counter + 1
If counter >= 2500 Then
Exit For
End If
End If
If counter = 1 Then
w.Select
Else
Selection.Extend
End If
w.HighlightColorIndex = wdYellow
Next w
Selection.Copy
MsgBox "The first " & counter & " words were highlighted in yellow AND added to the clipboard!"
End Sub
基本上,
答案 1 :(得分:0)
我看到两个选项,1将取前2,500个单词并将其放在一个字符串中供您稍后在代码中使用,第二个选项将只选择并复制前2,500个单词
Sub PutInVariable()
Dim MyArr As Variant, MyString As String
MyArr = Split(ThisDocument.Content.Text, " ")
ReDim Preserve MyArr(2499)
MyString = Join(MyArr, " ")
End Sub
Sub SelectAndCopy()
Selection.HomeKey Unit:=wdLine
Selection.MoveRight Unit:=wdWord, Count:=2500, Extend:=wdExtend
Selection.Copy
End Sub