#1
|
|||
|
|||
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. |
#2
|
||||
|
||||
Why not just use a Find/Replace?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
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. |
#4
|
||||
|
||||
Quote:
Code:
Selection.Find.Execute "<Replace Me>", , , False, , , True, wdFindStop, , "New Text", wdReplaceOne
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Quote:
I posted a stripped-down sample sub in this thread, but don't confuse that with the actual program I'm working on. |
#6
|
||||
|
||||
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] |
#7
|
||||
|
||||
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 |
|
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 |