字VBA上的宏parentheticals

0

的问题

我一直在使用下面的宏拉出的项目在括号意见中的话:

'
' CommentBubble Macro
'
'
Dim myRange As Range
Set myRange = ActiveDocument.Content
searchtext = "\(*\)"

With myRange.Find
    .MatchWildcards = True
    Do While .Execute(findText:=searchtext, Forward:=True) = True
      If Len(myRange.Text) > 4 Then
        ActiveDocument.Comments.Add myRange, myRange.Text
        myRange.Text = ""
      End If
    Loop
 End With
End Sub

其原因我已经长度的案文是>4是因为这些法律文件,我不想要隔离弦,有东西像"在以下情况:(i)1的条件,(ii)的条件2,等等。"

然而,这一段的文本的上述码休息:

This is sample text (with some additional text) that does stuff (with more stuff) and represents 39.4% of shares on the effective date (before giving effect, with some conditions such as ( some stuff (i) and some stuff (ii) with final stuff) and more final stuff) which is subject to  (some conditions here) and conclude here. 

如果你跑这样你会得到以下结果:

This is sample text  that does stuff  and represents 39.4% of shares on the effective date  and some stuff (ii) with final stuff) and more final stuff) which is subject to   and conclude here. 

正如你可以看到的嵌套括号引起一些麻烦。 任何建议?

谢谢!

ms-word vba
2021-11-20 22:17:27
1

最好的答案

2

你是想配括号内的这一词是一个困难和吃力不讨好的任务,因为词只能看到开启和关闭括号内作为个人的人物并不是自动匹配的词。 代码下面发现匹配的括号,消除了尾的空间,habdles的情况下没有括号内的被本和差错,如果你有不平衡的错误。 我已经离开调试声明所以,你可以取消它们来看看发生了什么。

Option Explicit

Public Sub ttest()

    Dim myRange As Word.Range
    Set myRange = ActiveDocument.StoryRanges(wdMainTextStory)
    myRange.Collapse direction:=wdCollapseStart
    
    
    Set myRange = NextParenRange(myRange)
    
    Do Until myRange Is Nothing
    
        DoEvents
        
        Debug.Print myRange.Text
        Dim myDupRange As Word.Range
        Set myDupRange = myRange.Duplicate
        myRange.Collapse direction:=wdCollapseEnd
        If myDupRange.Characters.Last.Next.Text = " " Then myDupRange.MoveEnd Count:=1
        myDupRange.Delete
        Set myRange = NextParenRange(myRange)
        
    
    Loop
    
End Sub

Public Function NextParenRange(ByVal ipRange As Word.Range) As Word.Range

    Const OpenP As String = "("
    Const CloseP As String = ")"
    
    Dim myRange As Word.Range
    Set myRange = ipRange.Duplicate
    
    'If myRange.Start <> myRange.End Then myRange.Collapse direction:=wdCollapseStart
    
    'exit if no parentheses exist
    'Debug.Print myRange.Start
    If myRange.MoveUntil(cset:=OpenP) = 0 Then
    
        Set NextParenRange = Nothing
        Exit Function
        
        
    Else
    
        'Debug.Print myRange.Start
        Dim myParenCount As Long
        myParenCount = 1
        myRange.MoveEnd Count:=1

    End If
    
    
    Do Until myParenCount = 0
    
        ' allows VBA to respond to a break key press
        DoEvents
        
        ' if we run out of parentheses before we get back to zero then flag an error
        If myRange.MoveEndUntil(cset:=OpenP & CloseP) = 0 Then
        
            VBA.Err.Raise 17, "Unbalanced parentheses in document"
            
            
        End If
        
        myRange.MoveEnd Count:=1
        'Debug.Print myRange.Characters.Last.Text
        'Debug.Print myRange.Characters.Last.Next.Text
        myParenCount = myParenCount + IIf(myRange.Characters.Last.Text = OpenP, 1, -1)
        
    
    Loop
    
    Set NextParenRange = myRange.Duplicate
    
End Function
2021-11-21 14:57:37

很好的解决方案(y)
Brett

相当整洁,谢谢你!
user1357015

@信号:任何建议如何改变你的代码的工作与特定的选择吗? 我改变了 Set myRange = Selection.StoryRanges(wdMainTextStory) 但是,这似乎不够的。
user1357015

你会需要做两件事。 改变设置myRange=ActiveDocument.StoryRanges(wdMainTextStory)'到'集myRange=选择。范围'. 你还需要来捕获底myrange在一个独立的变量,然后在做到循环试验,目前开始的范围不超过保存端的范围。 例如喜欢的东西'做,直到myRange.开始>=mySavedEnd'
freeflow

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................