Remove paragraph mark from string

David Gard picture David Gard · Oct 29, 2014 · Viewed 13.8k times · Source

I have a macro that finds all of the 'Heading 1' styles within my document and lists them in a ComboBox on a UserForm.

My problem is that the Find routine I am using is also selecting the paragraph mark () after the text I wish to copy, and that is being displayed in the ComboBox.

How can I remove this from the string? I've tried useing replace(), replacing vbCrLf, vbCr, vbLf, vbNewLine, ^p, v, Chr(244) and Asc(244) with "", but nothing has succeeeded. For example -

sanitizedText = Replace(Selection.Text, "^v", "")

Can anyone please help with this problem? Thanks.

Here is how my form looks -

enter image description here

Answer

Miguel Febres picture Miguel Febres · Oct 29, 2014

You should use ChrW$() for unicode characters:

sanitizedText = Replace(Selection.Text, ChrW$(244), "")

Or, if the paragraph mark is always at the end maybe you can just remove the last character using

myString = Left(myString, Len(myString) - 1)