0

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Donfernando
  • 5
  • 1
  • 4
  • Your second attempt has the correct syntax which is why it doesn't produce an error, but the What-string isn't found because vbLf is a long, not a string, and though VBA probably manages to coerce the number into a string it isn't a number you are looking for. Off the top of my head I don't know which string the TextBox uses. It could be Chr(10) or Chr(11) or perhaps Chr(10) & Chr(13) - or the other way around. You can enter this like `What:=Chr(11)` – Variatus Apr 19 '17 at 10:37
  • @Variatus `vbLF` is undoubtedly a string. It's just not the correct string to use in this context, apparently :) (Try `?TypeName(vbLF)` in the Immediate window if you'd like) – David Zemens Apr 25 '17 at 15:18
  • Thank you @David Zemens. Looks can be deceiving, then :-) – Variatus Apr 25 '17 at 23:47

1 Answers1

1

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, "")
interesting-name-here
  • 1,851
  • 1
  • 20
  • 33