Consulting

Results 1 to 5 of 5

Thread: Tricky spacing problem

  1. #1

    Tricky spacing problem

    This task is about spacing around punctuation

    For example:

    The code below adds a spacing next to the period.

    sentence :This is a sample sentence .This sentence is alright.
    sentence :This is a sample sentence. This sentence is alright.
    ActiveDocument.Range.Find.Execute FindText:="([A-Za-z]\.)([A-Za-z])", _
    MatchWildcards:=True, Wrap:=wdFindContinue, Replacewith:="\1 \2", _
    Replace:=wdReplaceAll
    Case 1:

    What if the period is attached to the next word.

    sentence :This is a sample sentence .This sentence is alright.
    expected result: This is a sample sentence. This sentence is alright.

    Case 2:

    What if there needs to be spaces on either side of the period.

    This is a sample sentence.This sentence is alright.
    This is a sample sentence . This sentence is alright.

    Case 3: what if i need to trim spaces on either side.

    This is a sample - sentence.
    This is a sample-sentence.

    Please ignore abbreviations or units that contain period and let's assume only end sentence period is present around the document.

    Please help.

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    You should be able to fix Case 1 (and a bunch more like it), plus the 2nd form of Case 2, with a wildcard Find/Replace, where:
    Find = ([ ^s])([.,:;\!\?])
    Replace = \2\1
    Note that this would leave an extra space after the period for the 2nd form of Case 2; you could clean those up with another F/R.

    Similarly, for the first form of Case 2:
    Find = ([a-z][.,:;\!\?])([A-Z)
    Replace = \1 \2

    I don't know what you expect from Case 3 - not every hyphen indicates a hyphenated word.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Thanks for the reply.

    Case 3 is to trim spaces around Emdash.

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Well, I'm sure you could figure out how to do that with a couple or ordinary F/R expressions...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    Quote Originally Posted by macropod View Post
    Well, I'm sure you could figure out how to do that with a couple or ordinary F/R expressions...
    ^+
    ^+

    solves it.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •