vbams-wordword-2010

Remove paragraph mark from string


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


Solution

  • 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)