Consulting

Results 1 to 4 of 4

Thread: Find & replace possessive 's (eg. Adam's) using macro

  1. #1

    Find & replace possessive 's (eg. Adam's) using macro

    I'm working on a macro that will replace all English 's possessive form with the Vietnamese equivalence. Here's my code so far:
        With Selection.Find
            .Text = "(<[A-Z]*>)’s"
            .Replacement.Text = "của \1"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = True
            .MatchWholeWord = False
            .MatchWildcards = True
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
    The problem I'm having is the wildcard expression. I want it to identify anything with a capitalized first letter and a 's at the end


    I've tried <[A-Z]*>’s and <[A-Z]*’s> but both are useless. <[A-Z]*’s> would find an entire string (eg. Smith didn't take Jane's), while <[A-Z]*>’s can't find anything. Curiously, when I just use <[A-Z]*>, it include possessive noun in the search (eg. Smith and Smith's are both found and replaced)

    Is there any other alternative expression? If not can I get around this somehow without using wildcard?

    UPDATE: I'm now using a workaround that's slightly better, but still has bugs
    Sub test()
        Application.ScreenUpdating = True
        Selection.HomeKey Unit:=wdStory
        
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        Selection.Find.Replacement.Highlight = True
        Selection.Words(1).Select
        With Selection.Find
            .Text = "<([A-Z]*)'s>"
            .Replacement.Text = "c" & ChrW(7911) & "a" & " \1"
            .Forward = True
            .Wrap = wdFindStop
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = True
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
            Selection.Find.Execute Replace:=wdReplaceAll
            
        Do Until ActiveDocument.Bookmarks("\Sel").Range.End = ActiveDocument.Bookmarks("\EndOfDoc").Range.End
            Selection.MoveRight Unit:=wdCharacter, Count:=1
            Selection.Words(1).Select
            With Selection.Find
                .Text = "<([A-Z]*)'s>"
                .Replacement.Text = "c" & ChrW(7911) & "a" & " \1"
                .Forward = True
                .Wrap = wdFindStop
                .Format = True
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = True
                .MatchSoundsLike = False
                .MatchAllWordForms = False
            End With
                Selection.Find.Execute Replace:=wdReplaceAll
        Loop
        ' Searching the remaning (till the end of document)
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        PlayTheSound "W34 - Checklist Completed.wav"
    End Sub
    Basically, this force Word to limit the search range to an individual word, see if it has a capital first letter and a 's ending, then do the replacement. After that, it move to the next word, and loop until the end.

    A problem arise when I tested it on this text:
    St Peter's and one of its churchwardens featured in a court case in 1607 involving the theft of a bell from St Leonard's Church, Aldrington, a parish church a few miles away.[16][17]Aldrington parish was then depopulated and the church was ruinous, and arrangements were made to sell its bell. Although this arrived at St Peter's, "it did not travel to Henfield with the consent of the Aldrington parishioners": the churchwarden admitted helping to steal it, and "the authorities at Henfield Church admitted that they had received 650 pounds (290 kg) of bell material" which was used to cast a new bell for the tower. They agreed to pay compensation of £16.5s. to Aldrington parish.[16][17]
    The 19th century saw extensive rebuilding of the fabric of the church. First of all the south aisle, which had been said to be badly out of repair as early as 1637, was replaced before 1833 at the expense of the notable botanist William Borrer, a local resident, and a gallery was included for the use of schoolchildren. In 1855 the tower was restored by an unknown architect.[14][8] In 1870 and 1871 a large-scale restoration was undertaken to the designs of the architects William Slater and Richard Carpenter. Most of the walls were refaced with flint, the chancel was lengthened eastward and raised in height, a new south chancel chapel was built, both aisles were entirely rebuilt, the roof of the nave was opened up and new clerestory lancet windows installed.[8][14][18]
    St Peter's Church was designated a Grade II* listed building on 15 March 1955.[19] Such buildings are defined as being "particularly important ... [and] of more than special interest".[20] In February 2001, it was one of 54 Grade II* listed buildings, and 1,028 listed buildings of all grades, in the district of Mid Sussex.[21] In 2008 a new stone floor was laid, under-floor heating installed, and the 19th-century pews were replaced with chairs.[18][22]
    I added Selection.Find.Replacement.Highlight = True to track its selection range, and turned out, after the first few word, some how it ignore the select word-by-word order, and went for an entire section of the text, as you can see in this image: https://s28.postimg.org/a0780fn8t/image.png

    But since I have Application.ScreenUpdating = True, I saw that after it replaced an entire section, it still continue to scan each word within the highlighted/replaced section, as if it hasn't even touched it yet.

    Anybody got any idea?
    Last edited by vkhu; 08-27-2017 at 12:06 AM.

  2. #2
    UPDATE: just change up my code a bit. See 1st post.

  3. #3
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    .Text = "(<[A-Z][a-z]{1,})'s>"

  4. #4
    Quote Originally Posted by mana View Post
    .Text = "(<[A-Z][a-z]{1,})'s>"
    Holy Schitte, that worked like a charm! And simple too! Thanks mate

Posting Permissions

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