-->

Remove paragraph mark from string

2019-04-25 15:05发布

问题:

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 -

回答1:

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)


回答2:

I used sanitizedText = Replace(Selection.Text, Chr(13), "") successfully; 13 is the ASCII value for 'carriage return'.



回答3:

This tiny script replaces, in a piece of text selected in the document (i.e. marked using the cursor) hyphens that are at the beginning of a line. It replaces them by an improvised bullet point: (o)

The script searches for occurances of "Paragraph mark followed by hyphen".

I had a similar problem as in the question above, as I was sure paragraph marks should be 'Chr(13) & Chr(10)', which is equal 'VbCrLF', which is equal 'Carriage Return, Line Feed'. However, 'Chr(13) & Chr(10)' were wrong. The naked Chr(13) did the job.

Sub MakeAppFormListPoints()

'Replace list hyphens by (o)

   Dim myRange As Range
   Set myRange = Selection.Range   'What has been marked with the cursor

      Debug.Print myRange  ' Just for monitoring what it does

   myRange = replace(myRange, Chr(13) & "-", Chr(13) & "(o)")

      Debug.Print myRange  ' Just for monitoring what it does

End Sub

(I use this for adjusting text written in Word to the insanely restricted character set of the official application form for the European Union Erasmus+ programme to promote lifelong learning activities. Well, I learned something.)