Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-28-2023, 09:15 AM
RobiNew RobiNew is offline Replace dashes between two digits without using Selection Windows 10 Replace dashes between two digits without using Selection Office 2016
Competent Performer
Replace dashes between two digits without using Selection
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Question Replace dashes between two digits without using Selection

Hi! I'm trying to replace m-dashes with n-dashes between two digits (text and footnotes) without using Selection. How can I modify this code? Thanks!




Sub DASHES()
Dim mStory As Range
Dim mRange As Range
For Each mStory In ActiveDocument.StoryRanges
mStory.Find.Execute FindText:="^#" & Chr(150) & "^#", ReplaceWith:=Chr$(45), Replace:=wdReplaceAll
Next mStory
Set mStory = Nothing
End Sub
Reply With Quote
  #2  
Old 09-28-2023, 09:17 AM
RobiNew RobiNew is offline Replace dashes between two digits without using Selection Windows 10 Replace dashes between two digits without using Selection Office 2016
Competent Performer
Replace dashes between two digits without using Selection
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Hi! I'm trying to replace m-dashes with n-dashes between two digits (text and footnotes) without using Selection. How can I modify this code? Thanks!


Quote:
Sub DASHES()
'Testo e Note: Sostituisce m-dash con n-dash
Dim mStory As Range 'Nel testo e nelle note
Dim mRange As Range
For Each mStory In ActiveDocument.StoryRanges 'Ranges = testo e note
mStory.Find.Execute FindText:="^#" & Chr(150) & "^#", ReplaceWith:=Chr$(45), Replace:=wdReplaceAll
Next mStory
Set mStory = Nothing
End Sub
Reply With Quote
  #3  
Old 09-28-2023, 09:44 AM
rollis13's Avatar
rollis13 rollis13 is offline Replace dashes between two digits without using Selection Windows 11 Replace dashes between two digits without using Selection Office 2016
Competent Performer
 
Join Date: Jan 2021
Location: Cordenons
Posts: 140
rollis13 will become famous soon enough
Default

What's your issue ? the code does its job.
__________________
Difficult is not to know but to share what you know (Han Fei Tzu reworked)
Reply With Quote
  #4  
Old 09-28-2023, 01:35 PM
RobiNew RobiNew is offline Replace dashes between two digits without using Selection Windows 10 Replace dashes between two digits without using Selection Office 2016
Competent Performer
Replace dashes between two digits without using Selection
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Many thanks for your interest, Rollis13! The code seems perfect, the only trouble is that it rarely works. In most attempts it runs correctly through each line but without changing what it is expected to change.
Reply With Quote
  #5  
Old 09-28-2023, 01:48 PM
rollis13's Avatar
rollis13 rollis13 is offline Replace dashes between two digits without using Selection Windows 11 Replace dashes between two digits without using Selection Office 2016
Competent Performer
 
Join Date: Jan 2021
Location: Cordenons
Posts: 140
rollis13 will become famous soon enough
Default

With "^#" & Chr(150) & "^#" it only works if it finds a m-dash between two numbers (and no spaces in between); ^# is jolly for a number.
__________________
Difficult is not to know but to share what you know (Han Fei Tzu reworked)
Reply With Quote
  #6  
Old 09-28-2023, 11:26 PM
RobiNew RobiNew is offline Replace dashes between two digits without using Selection Windows 10 Replace dashes between two digits without using Selection Office 2016
Competent Performer
Replace dashes between two digits without using Selection
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Thanks! I'm well aware of that. I was just asking: how can I modify this code to make it replace m-dashes between two digits without using Selection?
Reply With Quote
  #7  
Old 09-29-2023, 12:02 AM
rollis13's Avatar
rollis13 rollis13 is offline Replace dashes between two digits without using Selection Windows 11 Replace dashes between two digits without using Selection Office 2016
Competent Performer
 
Join Date: Jan 2021
Location: Cordenons
Posts: 140
rollis13 will become famous soon enough
Default

In your macro you are using function .Find, I don't see any .Select. Or, do you want to prevent users from selecting.
__________________
Difficult is not to know but to share what you know (Han Fei Tzu reworked)
Reply With Quote
  #8  
Old 09-29-2023, 03:19 AM
RobiNew RobiNew is offline Replace dashes between two digits without using Selection Windows 10 Replace dashes between two digits without using Selection Office 2016
Competent Performer
Replace dashes between two digits without using Selection
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Sorry, but I don't understand what you mean. As I wrote before, I'm trying to replace m-dashes with n-dashes between two digits (text and footnotes) without using Selection. Hope to get some help to modify this code. Thanks!


Code:
Sub DASHES()
'Testo e Note: Sostituisce m-dash con n-dash
Dim mStory As Range 'Nel testo e nelle note
Dim mRange As Range
For Each mStory In ActiveDocument.StoryRanges 'Ranges = testo e note
mStory.Find.Execute FindText:="^#" & Chr(150) & "^#", ReplaceWith:=Chr$(45), Replace:=wdReplaceAll
Next mStory
Set mStory = Nothing
End Sub
Reply With Quote
  #9  
Old 09-29-2023, 03:33 AM
rollis13's Avatar
rollis13 rollis13 is offline Replace dashes between two digits without using Selection Windows 11 Replace dashes between two digits without using Selection Office 2016
Competent Performer
 
Join Date: Jan 2021
Location: Cordenons
Posts: 140
rollis13 will become famous soon enough
Default

I can't be of any other help: you're not using Selection in your macro. Otherwise, explain better what you mean by "Selection".
__________________
Difficult is not to know but to share what you know (Han Fei Tzu reworked)
Reply With Quote
  #10  
Old 09-29-2023, 08:31 AM
RobiNew RobiNew is offline Replace dashes between two digits without using Selection Windows 10 Replace dashes between two digits without using Selection Office 2016
Competent Performer
Replace dashes between two digits without using Selection
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Please ignore the word 'Selection'. What I need is a modification of the code below so as to replace each m-dash with an n-dash.

Code:
Sub DASHES()
'Testo e Note: Sostituisce m-dash con n-dash
Dim mStory As Range 'Nel testo e nelle note
Dim mRange As Range
For Each mStory In ActiveDocument.StoryRanges 'Ranges = testo e note
mStory.Find.Execute FindText:="^#" & Chr(150) & "^#", ReplaceWith:=Chr$(45)
Next mStory
Set mStory = Nothing
End Sub
Reply With Quote
  #11  
Old 09-29-2023, 09:30 AM
vivka vivka is offline Replace dashes between two digits without using Selection Windows 7 64bit Replace dashes between two digits without using Selection Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi, RobiNew! If undersatand correctly what you want, try the following. But note that m-dash has code 151 and n-dash has code 150.
Code:
Sub DASHES()
'Testo e Note: Sostituisce m-dash con n-dash

Dim mStory As range 'Nel testo e nelle note
Dim mRange As range
  For Each mStory In ActiveDocument.StoryRanges
    mStory.Find.Execute FindText:="([0-9])" & Chr(151) & "([0-9])", _
    ReplaceWith:="\1" & Chr(150) & "\2", _
    MatchWildcards:=True, _
    Replace:=wdReplaceAll
  Next mStory
End SUb
Reply With Quote
  #12  
Old 09-29-2023, 10:58 AM
rollis13's Avatar
rollis13 rollis13 is offline Replace dashes between two digits without using Selection Windows 11 Replace dashes between two digits without using Selection Office 2016
Competent Performer
 
Join Date: Jan 2021
Location: Cordenons
Posts: 140
rollis13 will become famous soon enough
Default

Em-dash is Chr$(151) while En-dash is Chr$(150) so change your line of code to this:
Code:
mStory.Find.Execute FindText:="([0-9])" & Chr$(151) & "([0-9])", ReplaceWith:="\1" & Chr$(150) & "\2", MatchWildcards:=True, Replace:=wdReplaceAll
__________________
Difficult is not to know but to share what you know (Han Fei Tzu reworked)
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
find and replace only in current selection Bikram Word VBA 4 11-12-2021 07:31 AM
Find and replace numbers (up to three digits) ballpoint Word 3 06-13-2018 08:58 AM
Replace dashes between two digits without using Selection Search+replace all --- [3 dashes] with square wingding imported_contiw Word 14 10-04-2017 01:03 PM
Macro to replace digits with letters Bananabean Word VBA 6 09-14-2013 09:28 PM
Replace dashes between two digits without using Selection Problems merging in last 4 digits of an account higher than 16 digits Glynda Mail Merge 1 04-08-2011 12:17 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:16 AM.


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