如何使宏忽略标点符号

huangapple go评论70阅读模式
英文:

How to get macro to ignore punctuation

问题

我正在处理一大段文本,并尝试将其全部转换为仅保留前两个字母(用于记忆工作)。 Reddit上的某人给了我一个很好的宏脚本,但它会从文本中删除标点符号。

这是原始的宏:

Sub ReplaceWithFirstTwoLetters()
   Dim rng As Range, words As Variant, word As Variant, replacedText As String
   If Selection.Type = wdSelectionNormal Then Set rng = Selection.Range Else Set rng = ActiveDocument.Content
   words = Split(rng.Text, " ")
   For Each word In words
      If Len(word) > 2 Then replacedText = replacedText & Left(word, 2) & " " Else replacedText = replacedText & word & " "
   Next word
   rng.Text = Trim(replacedText)
End Sub

示例:
原始 -
当在人类事件的过程中,有必要让一个民族解除与另一个民族相连的政治纽带,并在地球的力量中承担独立而平等的地位,自然法和自然之神的法律赋予他们这个权利,对人类的意见表示应该宣布迫使他们分离的原因。
我想要的 -
Wh in th Co of hu ev it be ne fo on pe to di th po ba wh ha co th wi an, an to as am th Po of th ea, th se an eq st to wh th La of Na an of Na Go en th, a de re to th op of ma re th th sh de th ca wh im th to th se。

我得到的 -
Wh in th Co of hu ev it be ne fo on pe to di th po ba wh ha co th wi an an to as am th Po of th ea th se an eq st to wh th La of Na an of Na Go en th a de re to th op of ma re th th sh de th ca wh im th to th se

英文:

I'm working with a large chunk of text, and trying to convert it all to just the first two letter (memorization work). Someone on reddit got me a good macro script, but it removes punctuation from the text.

Here's the original macro:

Sub ReplaceWithFirstTwoLetters()
   Dim rng As Range, words As Variant, word As Variant, replacedText As String
   If Selection.Type = wdSelectionNormal Then Set rng = Selection.Range Else Set rng = ActiveDocument.Content
   words = Split(rng.Text, " ")
   For Each word In words
      If Len(word) > 2 Then replacedText = replacedText & Left(word, 2) & " " Else replacedText = replacedText & word & " "
   Next word
   rng.Text = Trim(replacedText)
End Sub

Example:
Original -
When in the Course of human events it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the Powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.
What I want -
Wh in th Co of hu ev it be ne fo on pe to di th po ba wh ha co th wi an, an to as am th Po of th ea, th se an eq st to wh th La of Na an of Na Go en th, a de re to th op of ma re th th sh de th ca wh im th to th se.

What I got -
Wh in th Co of hu ev it be ne fo on pe to di th po ba wh ha co th wi an an to as am th Po of th ea th se an eq st to wh th La of Na an of Na Go en th a de re to th op of ma re th th sh de th ca wh im th to th se

答案1

得分: 1

此解决方案仅适用于紧跟在单词后面的单字符标点符号。将您想要保留的其他标点符号添加到sPUNCTUATION_TO_KEEP中。代码注释用于解释。

Sub ReplaceWithFirstTwoLetters()
    Const sPUNCTUATION_TO_KEEP As String = ",.;:!?'" ' 添加其他您想要保留的字符
    Dim rng As Range, words As Variant, word As Variant, replacedText As String
    If Selection.Type = wdSelectionNormal Then
        Set rng = Selection.Range
    Else
        Set rng = ActiveDocument.Content
    End If
    ' 处理多个段落
    words = Split(Replace(Replace(rng.Text, vbCr, " "), "  ", " "), " ")
    For Each word In words
        If Len(word) > 2 Then
            replacedText = replacedText & Left(word, 2)
            ' 如果单词末尾有标点符号,则保留标点符号
            If InStr(1, sPUNCTUATION_TO_KEEP, Right$(word, 1), vbBinaryCompare) > 0 Then
                replacedText = replacedText & Right$(word, 1)
            End If
            ' 现在在字母后面添加空格
            replacedText = replacedText & " "
        Else
            replacedText = replacedText & word & " "
        End If
    Next word
    rng.Text = Trim(replacedText)
End Sub

注意:这是Visual Basic for Applications (VBA)代码,用于在Microsoft Word中将单词替换为其前两个字母,并保留特定标点符号。

英文:

This solution will only work for single-character punctuation immediately following a word. Add other punctuation characters that you want to keep to sPUNCTUATION_TO_KEEP. Code comments included for an explanation.

Sub ReplaceWithFirstTwoLetters()
    Const sPUNCTUATION_TO_KEEP As String = ",.;:!?" ' add other characters you want to keep
    Dim rng As Range, words As Variant, word As Variant, replacedText As String
    If Selection.Type = wdSelectionNormal Then
        Set rng = Selection.Range
    Else
        Set rng = ActiveDocument.Content
    End If
    ' handle multiple paragraphs
    words = Split(Replace(Replace(rng.Text, vbCr, " "), "  ", " "), " ")
    For Each word In words
        If Len(word) > 2 Then
            replacedText = replacedText & Left(word, 2)
            ' keep the punctuation, if present at the end of the word
            If InStr(1, sPUNCTUATION_TO_KEEP, Right$(word, 1), vbBinaryCompare) > 0 Then
                replacedText = replacedText & Right$(word, 1)
            End If
            ' now add the space after the letters
            replacedText = replacedText & " "
        Else
            replacedText = replacedText & word & " "
        End If
    Next word
    rng.Text = Trim(replacedText)
End Sub

答案2

得分: 0

以下是您要的翻译部分:

Sub ReplaceWithFirstTwoLetters()
   Dim s As String
   Const PUNTS As String = ",.!?:'add any more you want"
   Dim rng As Range, words As Variant, word As Variant, replacedText As String, punt As String
   
   If Selection.Type = wdSelectionNormal Then Set rng = Selection.Range Else Set rng = ActiveDocument.Content
   words = Split(Replace(Replace(rng.Text, vbCr, " "), vbLf, " "), " ")
   'IF YOU WANT TO KEEP CR & LF COMMENT THE PREVIUS LINE AND UNCOMMENT THE NEXT
   ' words = Split(rng.Text, " ")
   For Each word In words
      word = Trim(word)
      If word <> "" Then
         If Len(word) > 2 Then
            punt = Right(word, 1)
            word = Left(word, 2)
            If InStr(1, PUNTS, punt) > 0 Then
               word = word & punt
            End If
         End If
         replacedText = replacedText & word & " "
      End If
   Next word
   rng.Text = Trim(replacedText)
End Sub
英文:
Sub ReplaceWithFirstTwoLetters()
   Dim s As String
   Const PUNTS As String = &quot;,.!?:&quot;  &#39;add any more you want
   Dim rng As Range, words As Variant, word As Variant, replacedText As String, punt As String
   
   If Selection.Type = wdSelectionNormal Then Set rng = Selection.Range Else Set rng = ActiveDocument.Content
   words = Split(Replace(Replace(rng.Text, vbCr, &quot; &quot;), vbLf, &quot; &quot;), &quot; &quot;)
   &#39;IF YOU WANT TO KEEP CR &amp; LF COMMENT THE PREVIUS LINE AND UNCOMMENT THE NEXT
   &#39; words = Split(rng.Text, &quot; &quot;)
   For Each word In words
      word = Trim(word)
      If word &lt;&gt; &quot;&quot; Then
         If Len(word) &gt; 2 Then
            punt = Right(word, 1)
            word = Left(word, 2)
            If InStr(1, PUNTS, punt) &gt; 0 Then
               word = word &amp; punt
            End If
         End If
         replacedText = replacedText &amp; word &amp; &quot; &quot;
      End If
   Next word
   rng.Text = Trim(replacedText)
End Sub

答案3

得分: 0

这是我实现忽略标点符号的方式。

完整代码太大,无法在这里展示,请参考我的存储库中的TextForCtext/WordVBA/punctuation.cls

希望你觉得它有用。

英文:

This is my way to implement to ignore punctuation OP.

The full code is too large to be here, you can refer to TextForCtext/WordVBA
/punctuation.cls in my repo
.
I hope you find it useful

huangapple
  • 本文由 发表于 2023年6月29日 03:34:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576209.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定