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,etc."

그러나,여기에 조각의 텍스트는 위의 코드를 나누기:

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ý
..................................................................................................................