Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-20-2023, 06:42 AM
RobiNew RobiNew is offline Replace apostrophe mark before a digit Windows 10 Replace apostrophe mark before a digit Office 2016
Competent Performer
Replace apostrophe mark before a digit
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Question Replace apostrophe mark before a digit


The following macro does its job, but I'm not satisfied with it. Is there a better way to obtain the same result? Thanks!


Code:
Sub SingleBeforeDigit()
'Replace Chr(145) with Chr(146) before a digit (as in ’95)
  Dim oRng As Range, iType As Integer
  For iType = 1 To 2
Set oRng = ActiveDocument.StoryRanges(iType)
With oRng.Find
    .ClearFormatting
    .Text = Chr(145) & "([0-9])"
    .Font.Superscript = False 'to avoid footnote reference marks
    .Forward = True
    .Wrap = wdFindContinue
    .MatchWildcards = True
While .Execute
oRng.Characters.First = Chr(146)
Wend
End With
   Next iType
End Sub
Reply With Quote
  #2  
Old 10-20-2023, 05:49 PM
Guessed's Avatar
Guessed Guessed is offline Replace apostrophe mark before a digit Windows 10 Replace apostrophe mark before a digit Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

You can just use ReplaceAll. Also the direction doesn't matter when you are working on the whole range and wrapping.
Code:
Sub SingleBeforeDigit()
  'Replace Chr(145) with Chr(146) before a digit (as in ’95)
  Dim oRng As Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .ClearFormatting
    .Text = Chr(145) & "([0-9])"
    .Replacement.Text = Chr(146) & "\1"
    .Font.Superscript = False       'to avoid footnote reference marks
    .Replacement.Highlight = True
    .Wrap = wdFindContinue
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 10-20-2023, 11:20 PM
RobiNew RobiNew is offline Replace apostrophe mark before a digit Windows 10 Replace apostrophe mark before a digit Office 2016
Competent Performer
Replace apostrophe mark before a digit
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Thanks, Guessed! That is certainly the best solution. Unfortunately, it doesn't work and I cannot imagine why. Apparently, the apostrophe remains untouched, but when I close the document I get the Save? message, as if something had been modified. Any idea?
Reply With Quote
  #4  
Old 10-21-2023, 02:49 AM
vivka vivka is offline Replace apostrophe mark before a digit Windows 7 64bit Replace apostrophe mark before a digit Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi, Robinew! The problem with chr(146) is that it can't be inserted (replace any char) in a position preceded by space (instead, chr(145) is inserted, that's why the 'Save Changes?' message pops up). For small docs your code is OK. But if you want to gain milliseconds, I offer a tricky workaround: first, replace all instances of chr(145) and the digit after it with something quite rare (in the code below it's a hash) + chr(146) + that digit, then delete hashes (replace them with nothing). There'll be only two code executions however big is a document instead of as many executions as many instances of chr(145) are in the doc using your code. I hope I have formulated my idea clearly.

Code:
Sub SingleBefore_Digit()
'Replace Chr(145) with Chr(146) before a digit (as in ’95)

Dim oRng As range, iType As Integer
  
    For iType = 1 To 2
        Set oRng = ActiveDocument.StoryRanges(1)
        Set oRng = selection.range
        With oRng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .text = Chr(145) & "([0-9])"
            .Font.Superscript = False
            .Replacement.text = "#" & Chr(146) & "\1"
            .MatchWildcards = True
            .Execute Replace:=wdReplaceAll
            .text = "#"
            .Replacement.text = ""
            .Execute Replace:=wdReplaceAll
        End With
   Next iType
End Sub
Reply With Quote
  #5  
Old 10-21-2023, 03:33 AM
Guessed's Avatar
Guessed Guessed is offline Replace apostrophe mark before a digit Windows 10 Replace apostrophe mark before a digit Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

The macro IS replacing the quotes but there is a sneaky little option which tells Word that all quotes need to be converted to smart quotes and this is undoing the macro's work instantly. To actually get the results to stick around and keep the smart quotes function from running, you need to temporarily turn this setting off and turn it on again after the macro has run.
Code:
Sub SingleBeforeDigit()
  'Replace Chr(145) with Chr(146) before a digit (as in ’95)
  Dim oRng As Range
  Set oRng = ActiveDocument.Range
  Options.AutoFormatAsYouTypeReplaceQuotes = False
    With oRng.Find
      .ClearFormatting
      .Text = Chr(145) & "([0-9])"
      .Replacement.Text = Chr(146) & "\1"
      .Font.Superscript = False       'to avoid footnote reference marks
      '.Replacement.Highlight = True
      .Wrap = wdFindContinue
      .MatchWildcards = True
      .Execute Replace:=wdReplaceAll
    End With
  Options.AutoFormatAsYouTypeReplaceQuotes = True
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia

Last edited by Guessed; 10-21-2023 at 04:42 AM. Reason: fixed typo spotted by vivka - thanks for that quality control
Reply With Quote
  #6  
Old 10-21-2023, 03:33 AM
RobiNew RobiNew is offline Replace apostrophe mark before a digit Windows 10 Replace apostrophe mark before a digit Office 2016
Competent Performer
Replace apostrophe mark before a digit
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Hi, Vivka! Yours is certainly a nice and fast solution. And a special 'Thank you!' for having explained the problem presented by Chr(146).
Reply With Quote
  #7  
Old 10-21-2023, 03:48 AM
RobiNew RobiNew is offline Replace apostrophe mark before a digit Windows 10 Replace apostrophe mark before a digit Office 2016
Competent Performer
Replace apostrophe mark before a digit
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Hi, Guessed! That's it !! Thank you! This explains the Save? message on closing the document. Everything is fine now (sorry, Vivka!).
Reply With Quote
  #8  
Old 10-21-2023, 04:05 AM
vivka vivka is offline Replace apostrophe mark before a digit Windows 7 64bit Replace apostrophe mark before a digit Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Thanks, Guessed! The more we know, the better! And thanks to RobiNew for another challenge!

Last edited by vivka; 10-21-2023 at 10:21 AM.
Reply With Quote
  #9  
Old 10-21-2023, 07:23 AM
RobiNew RobiNew is offline Replace apostrophe mark before a digit Windows 10 Replace apostrophe mark before a digit Office 2016
Competent Performer
Replace apostrophe mark before a digit
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Sorry, but I'm here again. The code variant here below changes '95 to '9. Why? Thanks!
Code:
Sub SingleBeforeDigit()
  'Replace Chr(145) with Chr(146) before a digit (as in ’95)
  Dim oRng As Range
  Set oRng = ActiveDocument.Range
  Options.AutoFormatAsYouTypeReplaceQuotes = False
With oRng.Find
    .ClearFormatting
    .Text = Chr(145) & "([0-9])" & "[!^02]" 'to avoid footnote ref. marks
    .Replacement.Text = Chr(146) & "\1"
    .Forward = True
    .Wrap = wdFindContinue
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
End With
  Options.AutoFormatAsYouTypeReplaceQuotes = True
End Sub
Reply With Quote
  #10  
Old 10-21-2023, 10:34 AM
vivka vivka is offline Replace apostrophe mark before a digit Windows 7 64bit Replace apostrophe mark before a digit Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Explanation: your code looks for chr(145) followed by any digit & any char but ^02 and then replaces these THREE chars with TWO chars, which are chr(146) & the digit, thus loosiing the third char.

Code:
 Sub SingleBeforeDigit()
'Replace Chr(145) with Chr(146) before a digit (as in ’95)

Dim oRng As range
  Set oRng = ActiveDocument.range
  Options.AutoFormatAsYouTypeReplaceQuotes = False
    With oRng.Find
        .ClearFormatting
        .text = Chr(145) & "([0-9][!^02])" 'to avoid footnote ref. marks
        .Replacement.text = Chr(146) & "\1"
        .Forward = True
        .Wrap = wdFindStop
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
    End With
  Options.AutoFormatAsYouTypeReplaceQuotes = True
End Sub
Reply With Quote
  #11  
Old 10-21-2023, 08:07 PM
zpy2 zpy2 is offline Replace apostrophe mark before a digit Windows 10 Replace apostrophe mark before a digit Office 2013
Novice
 
Join Date: Sep 2023
Location: China
Posts: 28
zpy2 is on a distinguished road
Default

Sub SingleBeforeDigit()

Dim regEx As Object

Dim match As Object

Dim fullNamePattern As String



' Create a RegExp object

Set regEx = CreateObject("VBScript.RegExp")



' Set the regular expression pattern for a full name

fullNamePattern = "‘(?=\d)"



With regEx

.Global = True ' Set global matching mode

.Pattern = fullNamePattern ' Assign the pattern



' Loop through all matches of the full name pattern

For Each match In .Execute(ActiveDocument.Content.Text)

' Replace the matched text

ActiveDocument.Range(match.FirstIndex, match.FirstIndex + Len(match.Value)).Text = chr(136)

Next match

End With

End Sub
Reply With Quote
  #12  
Old 10-22-2023, 12:13 AM
RobiNew RobiNew is offline Replace apostrophe mark before a digit Windows 10 Replace apostrophe mark before a digit Office 2016
Competent Performer
Replace apostrophe mark before a digit
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Thank you, Vivka! Now I see the missing round brackets in the code I posted.
Thanks, Xpy2! An interesting variant with a slightly different result: ^95.
Reply With Quote
  #13  
Old 10-22-2023, 12:34 AM
vivka vivka is offline Replace apostrophe mark before a digit Windows 7 64bit Replace apostrophe mark before a digit Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

RobiNew, you are welcome! Zpy2, thank you for another approach! Regexp seems more functional than standard Word means. I need to learn it.
Reply With Quote
  #14  
Old 10-22-2023, 05:37 PM
zpy2 zpy2 is offline Replace apostrophe mark before a digit Windows 10 Replace apostrophe mark before a digit Office 2013
Novice
 
Join Date: Sep 2023
Location: China
Posts: 28
zpy2 is on a distinguished road
Default

RobiNow,you are welcome!vivka,thank you for the nice solution!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace apostrophe mark before a digit Find/replace - remove a space in 4 digit numbers lalywizz Word 5 10-08-2021 09:27 AM
Replace apostrophe mark before a digit Trying to change the last digit in a column of 10 digit entries arkansawyer16 Excel 10 04-06-2020 02:03 PM
Regex/wildcard search for dates with 2-digit and 4-digit years Marrick13 Word VBA 2 01-29-2016 07:04 AM
Replace apostrophe mark before a digit A challenging digit by digit manipulation, rotate the digit in range of 0 to 9 laucn Excel Programming 14 05-17-2015 12:12 PM
Replace apostrophe mark before a digit Apostrophe-like mark appearing next to page numbers Natalie Word 2 04-26-2012 05:17 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:59 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft