【发布时间】:2017-05-03 10:06:41
【问题描述】:
我想做一个宏来做以下事情:
突出显示每第 n 个选择。 检查该选择以确保它是一个单词(而不是数字或标点符号)。 剪切单词并将其粘贴到另一个文档中。 用空格替换单词。 重复直到文档结束。
困难的部分是检查选择以验证它确实是一个单词而不是其他东西。
我发现其他人编写的一些代码可能有效,但我不明白如何在我的宏中使用其余命令实现它:
Function IsLetter(strValue As String) As Boolean
Dim intPos As Integer
For intPos = 1 To Len(strValue)
Select Case Asc(Mid(strValue, intPos, 1))
Case 65 To 90, 97 To 122
IsLetter = True
Case Else
IsLetter = False
Exit For
End Select
Next
End Function
Sub Blank()
Dim OriginalStory As Document
Set OriginalStory = ActiveDocument
Dim WordListDoc As Document
Set WordListDoc = Application.Documents.Add
Windows(OriginalStory).Activate
sPrompt = "How many spaces would you like between each removed word?"
sTitle = "Choose Blank Interval"
sDefault = "8"
sInterval = InputBox(sPrompt, sTitle, sDefault)
Selection.HomeKey Unit:=wdStory
Do Until Selection.Bookmarks.Exists("\EndOfDoc") = True
Selection.MoveRight Unit:=wdWord, Count:=sInterval, Extend:=wdMove
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
If IsLetter = True Then
Selection.Cut
Selection.TypeText Text:="__________ "
Windows(WordListDoc).Activate
Selection.PasteAndFormat (wdFormatOriginalFormatting)
Selection.TypeParagraph
Windows(OriginalStory).Activate
Else
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdMove
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
Loop
Loop
End Sub
函数应该位于代码其余部分的“之上”,对吧?但是当我运行它时,我得到一个错误'argument not optional'。
非常感谢任何想法或提示。
【问题讨论】:
-
函数位于何处并不重要,重要的是过程并通过引用它来包含参数的值。-
:-)现在If IsLetter(what is ?) = True Then -
现在你还有
loop没有Do替换它End If -
我不明白您打算通过哪种方式识别“每第 n 个选择”。实际上,您必须解释第一个“选择”是什么。您发布的代码没有任何作用。它似乎确实试图用下划线替换选择,但不清楚它是如何首先进行选择的,也不会多次重复该操作。
-
我的意思是用户可以指定每个空白单词之间应该出现多少个单词。这个想法是,用户输入所需的间隔,然后宏向前跳过该间隔并选择下一个单词,将其取出,在其位置放置下划线,然后将剪切的单词粘贴到另一个文档中。 The problem arises when the selection is not in fact a word, and is a carriage return, speech mark, or other punctuation mark.感谢大家的回复和帮助。