Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-28-2019, 02:48 PM
Alsadius Alsadius is offline Deleting text but keeping adjacent spaces Windows 7 64bit Deleting text but keeping adjacent spaces Office 2010 32bit
Novice
Deleting text but keeping adjacent spaces
 
Join Date: Nov 2017
Posts: 25
Alsadius is on a distinguished road
Default Deleting text but keeping adjacent spaces

I'm doing a text replacement macro that deletes text in one sub, and then has another sub insert different text into the same spot. However, when I delete text and there's a space on either side, Word's default text deletion will eliminate one of the spaces, which means that the inserted text is now lacking a space between it and an adjacent word. It's also messing up formatting when highlighting changes at the same spot.

Original text: Intro <Replace Me> More



Sub Test()
Dim VarRange As Range
SelectText ("<Replace Me>")
Set VarRange = ActiveDocument.Range(Start:=Selection.Start, End:=Selection.End)
VarRange.Delete
Selection.InsertAfter ("New Text")
End Sub

Should produce: Intro New Text More
Actually produces: Intro New TextMore

What I'd like is a flag I can set to make it not delete adjacent spaces, but all my searching hasn't shown me one. I know of a couple hacky workarounds that I can use if need be - find-and-replace, in particular - but because of the structure of the code, doing it that way would make the overall program much more complex.

Thanks.
Reply With Quote
  #2  
Old 03-01-2019, 02:57 AM
macropod's Avatar
macropod macropod is offline Deleting text but keeping adjacent spaces Windows 7 64bit Deleting text but keeping adjacent spaces Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Why not just use a Find/Replace?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 03-01-2019, 07:54 AM
Alsadius Alsadius is offline Deleting text but keeping adjacent spaces Windows 7 64bit Deleting text but keeping adjacent spaces Office 2010 32bit
Novice
Deleting text but keeping adjacent spaces
 
Join Date: Nov 2017
Posts: 25
Alsadius is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Why not just use a Find/Replace?
The biggest reason is that I'd need to refactor a ton of code to make it work that way, and do so in a way inconsistent with the rest of my code. Normally I'd just do that, but in context of the whole project it's a lot more work than it sounds like.

My hope is that there's a simple flag I can use for "Don't delete adjacent spaces" - I think I've seen info about how to set that in Word option menus, but I can't find anything about doing it in VBA code.

EDIT: Never mind, figured it out. If I replace VarRange.Delete with VarRange.Text = "" then it works fine. It seems like the adjacent space cleanup only applies when the .Delete procedure is run, and any other method of deleting it will leave the spaces.
Reply With Quote
  #4  
Old 03-01-2019, 01:39 PM
macropod's Avatar
macropod macropod is offline Deleting text but keeping adjacent spaces Windows 7 64bit Deleting text but keeping adjacent spaces Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Alsadius View Post
The biggest reason is that I'd need to refactor a ton of code to make it work that way
I hardly think so. It could all be done with a single line of code:
Code:
Selection.Find.Execute "<Replace Me>", , , False, , , True, wdFindStop, , "New Text", wdReplaceOne
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 03-04-2019, 06:56 AM
Alsadius Alsadius is offline Deleting text but keeping adjacent spaces Windows 7 64bit Deleting text but keeping adjacent spaces Office 2010 32bit
Novice
Deleting text but keeping adjacent spaces
 
Join Date: Nov 2017
Posts: 25
Alsadius is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
I hardly think so. It could all be done with a single line of code:
Code:
Selection.Find.Execute "<Replace Me>", , , False, , , True, wdFindStop, , "New Text", wdReplaceOne
I hardly think so. The deletion and replacement are in separate subs, each of which spends a lot of lines of code figuring out what to delete/add, and it's done that way to stay consistent with significantly larger blocks of code elsewhere in the macro. I'd have to re-arrange a goodly part of my program to do it the way you suggest. When I'm demoing this to senior management this week and want it to run perfectly, and when the program as a whole has shown a fondness for subtle bugs.

I posted a stripped-down sample sub in this thread, but don't confuse that with the actual program I'm working on.
Reply With Quote
  #6  
Old 03-04-2019, 02:06 PM
macropod's Avatar
macropod macropod is offline Deleting text but keeping adjacent spaces Windows 7 64bit Deleting text but keeping adjacent spaces Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Alsadius View Post
I hardly think so. The deletion and replacement are in separate subs, each of which spends a lot of lines of code figuring out what to delete/add, and it's done that way to stay consistent with significantly larger blocks of code elsewhere in the macro.
You can, of course, pass the Find & Replace strings to that line as arguments... But, hey, if you don't want to improve your code, who am I to argue!
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 03-04-2019, 02:50 PM
Guessed's Avatar
Guessed Guessed is offline Deleting text but keeping adjacent spaces Windows 10 Deleting text but keeping adjacent spaces Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
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

There is an option setting that determines whether Word helps out by deleting extra spaces when removing text. You could change that optional setting and see if it fixes your code issue.

The option is in File > Options > Advanced > Cut, copy and paste > Use smart cut and paste
Turn that off and try your code.

If that works, you can investigate the settings further at the next level down - it appears the likely culprit would be the "adjust sentence and word spacing automatically"
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I stop check boxes moving rows when text wraps in an adjacent cell? PatrickYork Excel 2 04-04-2018 11:23 AM
Big Spaces/pages between text tissueboxer Word 6 03-03-2018 07:52 AM
Vertical Text Alignment with Adjacent Columns kdutrisac Word Tables 2 06-26-2017 12:38 PM
Deleting grid lines but keeping the axis lines CoffeeNut Excel 0 04-01-2013 01:50 PM
Conditional Formatting Expiration Dates Based on Text in Adjacent Cell Frogggg Excel 1 10-25-2011 08:44 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:35 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