Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How Can I find and trim spaces between quotation text?

for example: if the word contains the following string: I say to him ' why should I? ' he answers... It will replace: I say to him 'why should I?' he answers...

I know that the regular expression to find text in the quotation is:('*?') but from here I could not progress.

Any help will be highly appreciated Asi

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
172 views
Welcome To Ask or Share your Answers For Others

1 Answer

For a VBA solution, try:

Sub Demo()
Dim Rng As Range, Rslt
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "[‘'][!^13^l^t]@['’]"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    .Execute
  End With
  Do While .Find.Found
    .Select
    Set Rng = .Duplicate
    With Rng
      .Start = .Start + 1
      .End = .End - 1
      If .Text <> Trim(.Text) Then
        Rslt = MsgBox("Trim this instance?", vbYesNoCancel)
        If Rslt = vbCancel Then Exit Sub
        If Rslt = vbYes Then
          Do While .Characters.Last = " "
            .Characters.Last = vbNullString
          Loop
          Do While .Characters.First = " "
            .Characters.First = vbNullString
          Loop
        End If
      End If
    End With
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
End Sub

Note: If the strings don't have any formatting applied, you could reduce:

  Do While .Characters.Last = " "
    .Characters.Last = vbNullString
  Loop
  Do While .Characters.First = " "
    .Characters.First = vbNullString
  Loop

to:

  .Text = Trim(.Text)

To work with just a selected range, change:

Dim Rng As Range, Rslt
With ActiveDocument.Range

to:

Dim Rng As Range, RngSel As Range, Rslt
With Selection.Range
  Set RngSel = .Duplicate

and insert:

    If .InRange(RngSel) = False Then Exit Sub

before:

    .Select

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...