View Single Post
 
Old 02-28-2019, 02:48 PM
Alsadius Alsadius is offline Windows 7 64bit Office 2010 32bit
Novice
 
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