Search code examples
vbams-wordtextboxuserformmultilinestring

How to convert a multi line string input from an textbox (in a userform) to a single line input string (vba word)


I have created a user form on VBA (word) where the user inputs a body of multi line text into TextBox1. I wish to convert this into a single line string. I have tried the following:

'Replace method 
TextBox1.Text = TextBox1.Text.Replace what:=vbFl replacement:=""

This results in 'invalid qualifier' with regards to the .Text

'Replace function
TextBox1.Text = Replace(TextBox1.Text, vbLf, "")

This produces no error but doesn't carry out the required conversion.


Solution

  • In Word, you have to consider the carriage return as well. There are three ways doing it your way:

    'Replace function
    TextBox1.Text = Replace(TextBox1.Text, vbCr + vbLf, "")
    TextBox1.Text = Replace(TextBox1.Text, Chr(10) + Chr(13), "")
    TextBox1.Text = Replace(TextBox1.Text, vbCrLf, "")