Parola di macro VBA su parentheticals

0

Domanda

Sono stato con la macro riportata di seguito per estrarre gli elementi tra parentesi per i commenti in word:

'
' 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

La ragione per cui la lunghezza del testo, > 4 è perché questi sono documenti legali e non voglio isolare le stringhe che sono cose come "con le seguenti condizioni: (i) la condizione 1, (ii) la condizione 2, etc."

Tuttavia, qui è un frammento di testo per i quali il codice di cui sopra si rompe:

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. 

Se si esegue questa si ottiene il seguente risultato:

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. 

Come si può vedere la parentesi nidificate causare alcuni problemi. Qualche consiglio?

Grazie!

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

Migliore risposta

2

Si sta cercando di abbinare parentesi che in Word è un compito difficile e ingrato come Word vede solo l'apertura e la chiusura delle parentesi come singoli caratteri e non associati automaticamente da word. Il codice riportato di seguito trova la corrispondenza tra parentesi, elimina gli spazi finali, habdles caso di assenza di parentesi di essere presente, e di errori se si sono sbilanciati errori. Ho lasciato il debug di istruzioni in modo che è possibile rimuovere il commento di vedere ciò che sta accadendo.

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

bella soluzione (y)
Brett

Abbastanza carino, grazie!
user1357015

@freeflow: suggerimenti su come modificare il codice per lavorare con una specifica selezione solo? Ho cambiato Set myRange = Selection.StoryRanges(wdMainTextStory) ma questo non sembra sufficiente.
user1357015

Hai bisogno di fare due cose. Cambiare 'Imposta myRange = ActiveDocument.StoryRanges(wdMainTextStory)' a 'Impostare myRange=selezione.Gamma'. Avrete anche bisogno di acquisire la fine di myrange in una stanza separata variabile e quindi nel ciclo do until prova che l'attuale inizio dell'intervallo non più di salvati fine dell'intervallo. ad esempio, qualcosa di simile a 'Fare fino a myRange.start>=mySavedEnd'
freeflow

In altre lingue

Questa pagina è in altre lingue

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