Results 1 to 9 of 9
  1. #1
    Star Lounger
    Join Date
    Jul 2008
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need macro to find non-slashed apostrophes (MS Off

    Edited by HansV to correct <!t>[/code] tag

    I need macro to put a backslash in front of any apostrophe that does not already have a backslash in front of the apostrophe.

    This macro does not do what I need a new macro to do because it finds every apostrophe and does not skip over apostrophes that are already preceded by a backslash.
    <code>
    Sub FindBackslahedApostrophe()
    '
    ' FindBackslahedApostrophe Macro
    '
    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = "'"
    .Replacement.Text = "^p"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.TypeText Text:=""
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    End Sub
    </code>
    Thank you in advance for your replies.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Need macro to find non-slashed apostrophes (MS Off

    Try this:
    <code>
    Sub FindNonBackslashedApostrophe()
    With Selection.Find
    .ClearFormatting
    .Text = "[!]'"
    .Replacement.Text = "^92'"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
    End With
    End Sub
    </code>
    Notes: this is a wildcard search/replace.
    In the Find what text, [!x] indicates anything but x. A backslash is a special character in wildcard searches, you must use if you want to search for a wildcard.
    You can't use as a character in the Replace with text when using wildcards, so I used ^92, where 92 is the ascii code for .

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Need macro to find non-slashed apostrophes (MS Off

    Isn't an apostrophe the <code>'</code> character instead of a dash? <img src=/S/scratch.gif border=0 alt=scratch width=25 height=29>

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Need macro to find non-slashed apostrophes (MS Off

    <P ID="edit" class=small>(Edited by macropod on 18-Aug-08 22:26. Dud code)</P>Hi HowdeeDoodee,

    I think you need to learn about using Wildcards in Find/Replace operations. In the meantime, try this:
    Sub MakeBackslahedApostrophe()
    With ActiveDocument.Content.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    ' Oops!!
    '.Text = "[!""""][-^+^=]"
    .Replacement.Text = "^92-"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
    End With
    End Sub

    Note: The find text includes 'normal' hyphens, 'em dashes' and 'en dashes'.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. #5
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Need macro to find non-slashed apostrophes (MS Off

    Hi Hans,

    I realised that almost immediatly after I'd posted. <img src=/S/blush.gif border=0 alt=blush width=15 height=15>

    The problem I ran into when trying to implement a suitable replacement that deals also with formatted apostrophes (eg .Text = "[!][^039^145^146]") is that I found that using [!] or [!""""] results in the character to the left of the apostrophe being deleted.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  6. #6
    Star Lounger
    Join Date
    Jul 2008
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Need macro to find non-slashed apostrophes (MS

    Thank you for your responses. I think I may be in the middle of crossposting here but...

    Hans, your code erases a preceding character before the backslash is inserted.
    I need all characters to remain in the text before the insertion of the backslash..


    Hans before…
    'The c'ow jumpe'd ov'er the mo'on.

    Hans after
    'The 'ow jumpe'd o'er the mo'on.

    Macropod...

    "The find text includes 'normal' hyphens, 'em dashes' and 'en dashes'."

    I only want the apostrophe backslashed, no other characters.
    Your code works on dashes but does not work on non-backslashed characters.

    My current project is related to establishing text ready for php where some characters need to be backslahed. I have a macro to backslash all escaped characters. However, the text is processed and reprocessed several times in several different ways, converted into html, converted to text, then back to MSWord. In the text are Greek and Hebrew characters that, after all the processing, come out converted with some characters equal to an apostrophe ('). All apostrophes are backslashed except the converted characters php interprets as apostrophes. Currently, the only problem character I am having is with the apostrophe. In the future, I do not know what I will face as a million pages are yet to be put through the conversion process.

    Thank you again for your responses.

  7. #7
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Need macro to find non-slashed apostrophes (MS

    Hi HowdeeDoodee,

    Try:
    Sub MakeBackslahedApostrophe()
    With ActiveDocument.Content.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "[‘'’]"
    .Replacement.Text = " '"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
    End With
    With ActiveDocument.Content.Find
    .Text = " '"
    .Replacement.Text = "'"
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
    End With
    With ActiveDocument.Content.Find
    .Text = " '"
    .Replacement.Text = "'"
    .Execute Replace:=wdReplaceAll
    End With
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Need macro to find non-slashed apostrophes (MS

    I apologize, I didn't test the code enough. Macropod has already posted a solution, here is another one:

    Sub FindNonBackslashedApostrophe()
    With Selection.Find
    .ClearFormatting
    .Text = "([!])(')"
    .Replacement.Text = "1^92'"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
    End With
    End Sub

  9. #9
    Star Lounger
    Join Date
    Jul 2008
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Need macro to find non-slashed apostrophes (MS

    Thank you to both of you. The macros work fine. Your time and effort is really appreciated.

Posting Permissions

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