check characters to the left of cursor from within a macro in Word 2007

am writing a macro to convert a set of text lines to a continuous series of lines:

    Do Until chk = numrows
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.EndKey Unit:=wdLine
    Selection.TypeText Text:=",  "
    Selection.MoveUp Unit:=wdLine, Count:=1
    Selection.EndKey Unit:=wdLine
    Selection.Delete Unit:=wdCharacter, Count:=1
    chk = chk + 1
    Loop

Unfortunately when the growing string textwraps, the Delete 1 character action deletes the first letter of the next item.
so want to check, pres with an If then statement, whether last characters at end of line are ",  " and if not, move down to the new line created by text-wrapping before doing the delete action.
Unless someone can suggest a different and smarter coding to get the same result, which is to add a comma and two spaces to the end of a line, delete the paragraph marker, go to the end of the next line down, and repeat until the last row of text.
The original text has been pasted in from excel, and then the resulting table converted to text, just to clarify where I am coming from.
Am using Word 2007 in Windows 7.
Answer
Answer

Better to use the Find>Replace facility to replace whatever is at the end of each line (a paragraph mark (¶) or a manual line break with a space,

 

If it is paragraph marks, used ^p in the find what control and if it is manual line breaks, use ^l

Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
It's time to replace ‘Diversity, Equity & Inclusion’ with ‘Excellence, Opportunity & Civility’ - V Ramaswamy

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 552 Applies to: