【问题标题】:In microsoft word for replacing words with blanks在 microsoft word 中用空格替换单词
【发布时间】: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.感谢大家的回复和帮助。

标签: vba ms-word


【解决方案1】:

我认为下面的代码可以满足您的大部分需求。请注意,其中一些 cmets 与我丢弃您的一些代码的原因有关,而另一些可能有助于理解当前版本。

Sub InsertBlanks()
    ' 02 May 2017

    Dim Doc As Document
    Dim WordList As Document
    Dim Rng As Range
    Dim Interval As String, Inter As Integer
    Dim Wd As String

    ' you shouldn't care which Window is active,
    ' though it probably is the one you want, anyway.
    ' The important thing is which document you work on.
'    Windows(OriginalStory).Activate
    Set Doc = ActiveDocument

    Application.ScreenUpdating = False
    Set WordList = Application.Documents.Add
    ' If you want to use all these variables you should also declare them.
    ' However, except for the input itself, they are hardly necessary.
'    sPrompt = "How many spaces would you like between each removed word?"
'    sTitle = "Choose Blank Interval"
'    sDefault = "8"
    Do
        Interval = InputBox("How many retained words would you like between removed words?", _
                            "Choose Blank Interval", CStr(8))
        If Interval = "" Then Exit Sub
    Loop While Val(Interval) < 4 Or Val(Interval) > 25
    Inter = CInt(Interval)
    ' you can modify min and max. Exit by entering a blank or 'Cancel'.

    ' You don't need to select anything.
'    Selection.HomeKey Unit:=wdStory
    Set Rng = Doc.Range(1, 1)               ' that's the start of the document
'    Set Rng = Doc.Bookmarks("James").Range  ' I used another start for my testing

    Do Until Rng.Bookmarks.Exists("\EndOfDoc") = True
        Rng.Move wdWord, Inter
        Wd = Rng.Words(1)
        If Asc(Wd) < 65 Then
            Inter = 1
        Else
            Set Rng = Rng.Words(1)
            With Rng
                ' replace Len(Wd) with a fixed number of repeats,
                ' if you don't want to give a hint about the removed word.
                .Text = String(Len(Wd) - 1, "_") & " "
                .Collapse wdCollapseEnd
            End With
            With WordList.Range
                If .Words.Count > 1 Then .InsertAfter Chr(11)
                .InsertAfter Wd
            End With
            Inter = CInt(Interval)
        End If
    Loop
    Application.ScreenUpdating = True
End Sub

为了避免处理非单词,我上面的代码大致测试了第一个字符是否为字母(ASCII > 64)。这将排除数字,并允许大量符号。例如,“100 欧元”将被接受替换,但“100”不被接受。您可能希望改进这个测试,也许像您最初所做的那样创建一个函数。我想到的另一种方法是排除长度小于 3 个字符的“单词”。这将消除 CrLf(如果 Word 认为是一个词),但它也会消除许多您可能喜欢的介词,而对“€ 100”什么也不做。它要么非常简单,就像我做的那样,要么很复杂。

【讨论】:

  • 非常感谢您发布此替代代码。我想我已经超出了我的知识水平 - 我不知道如何实际使用你给我的东西来执行我想做的任务。如果用户想在一段文本中留出 10 个单词的间隙,然后将下一个单词空白,但首先要确保它确实是一个将被空白的单词而不是句号,我怎么能'调用你建议的代码?如果测试失败,我该如何重复“测试”直到找到一个真实的单词? “如果”陈述?直到?为了?也许我的知识太少,无法做到这一点......
  • 开始在哪里? “如果用户想留 10 个字的间隙”——从哪里开始?在哪里结束?句号怎么可能是一个“词”?这些如何与“突出显示每个第 n 个选择”相结合?你说普通英语的代码对我来说就像我用 VBA 编写的代码对你一样难以理解。
  • 很抱歉,我不知道可能导致混淆的公认命名法。 “开始”是文本的开头,因为我有一行(Selection.HomeKey Unit:=wdStory)。想象一下,您按住 control 键并按右光标箭头键 10 次。它会一个字一个字地跳,对吗?这就是我想要的。 10倍。我说“nth”是因为我想结合用户更改“间隔”的能力,有时是 10,也许是 8,无论他们想要什么。当然你是对的,句号不是一个词。这就是我要解决的问题!
  • 当您按住控制键并按下箭头键时,MSWord 将一个单词视为一次“跳跃”。它还将句号视为一次“跳跃”。如果有回车,它也算作一个。我不知道还能怎么形容它。在进行 x 或 n 或任何数量的跳转后,我想突出显示遇到的下一个“事物”(想象 ctrl+shift+forwards 光标)。这就是我所说的“选择”。我想检查 that 选择以确保它确实是一个词。如果是,则将其剪掉并用下划线替换。如果没有,请转到下一个“事情”并检查它。
  • 所以我想要两个循环。 “内部”循环将关注在指定的向前跳转次数后检查选择。一旦它确定选择的是一个单词而不是别的东西,它就会把这个单词剪掉,粘贴到另一个文档中,然后返回到原始文档并再向前跳转 x 次。第二个“外部”循环应该保持这种状态,直到到达文档的末尾。感谢您的耐心等待,如果我的解释方式不优雅、不准确或具有误导性,我们深表歉意。
【解决方案2】:

Variatus - 非常感谢您。它工作得非常完美,对我来说真的很有用。

而且你的 cmets 对我理解你使用的一些我不熟悉的命令很有帮助。

非常感谢您的耐心和帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 2019-08-13
    相关资源
    最近更新 更多