![]() |
#1
|
|||
|
|||
![]()
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 |
#2
|
||||
|
||||
![]()
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 |
#3
|
|||
|
|||
![]()
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?
|
#4
|
|||
|
|||
![]()
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 |
#5
|
||||
|
||||
![]()
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 |
#6
|
|||
|
|||
![]()
Hi, Vivka! Yours is certainly a nice and fast solution. And a special 'Thank you!' for having explained the problem presented by Chr(146).
|
#7
|
|||
|
|||
![]()
Hi, Guessed! That's it !! Thank you! This explains the Save? message on closing the document. Everything is fine now (sorry, Vivka!).
|
#8
|
|||
|
|||
![]()
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. |
#9
|
|||
|
|||
![]()
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 |
#10
|
|||
|
|||
![]()
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 |
#11
|
|||
|
|||
![]()
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 |
#12
|
|||
|
|||
![]()
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. |
#13
|
|||
|
|||
![]()
RobiNew, you are welcome! Zpy2, thank you for another approach! Regexp seems more functional than standard Word means. I need to learn it.
|
#14
|
|||
|
|||
![]()
RobiNow,you are welcome!vivka,thank you for the nice solution!
|
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
lalywizz | Word | 5 | 10-08-2021 09:27 AM |
![]() |
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 |
![]() |
laucn | Excel Programming | 14 | 05-17-2015 12:12 PM |
![]() |
Natalie | Word | 2 | 04-26-2012 05:17 AM |