How to use \n new line in VB msgbox() ...? - vb6

What is the alternative to \n (for new line) in a MsgBox()?

for VB: vbCrLf or vbNewLine
for VB.NET: Environment.NewLine or vbCrLf or Constants.vbCrLf
Info on VB.NET new line: http://msdn.microsoft.com/en-us/library/system.environment.newline.aspx
The info for Environment.NewLine came from Cody Gray and J Vermeire

Try using vbcrlf for a newline
msgbox "This is how" & vbcrlf & "to get a new line"

These are the character sequences to create a new line:
vbCr is the carriage return (return to line beginning),
vbLf is the line feed (go to next line)
vbCrLf is the carriage return / line feed (similar to pressing Enter)
I prefer vbNewLine as it is system independent (vbCrLf may not be a true new line on some systems)

Use the Environment.NewLine property

Add a vbNewLine as:
"text1" & vbNewLine & "text2"

An alternative to Environment.NewLine is to use :
Regex.Unescape("\n\tHello World\n")
from System.Text.RegularExpressions
This allows you to escape Text without Concatenating strings as you can in C#, C, java

The correct format is :
"text1" + vbNewLine + "text2"

Use the command "vbNewLine"
Example
Hello & vbNewLine & "World"
will show up as
Hello on one line and World on another

You can use Environment.NewLine OR vbCrLF OR vbNewLine
MsgBox($"Hi!{Environment.NewLine}I'M HERE")

You can use carriage return character (Chr(13)), a linefeed character (Chr(10)) also like
MsgBox "Message Name: " & objSymbol1.Name & Chr(13) & "Value of BIT-1: " & (myMessage1.Data(1)) & Chr(13) & "MessageCount: " & ReceiveMessages.Count

Module MyHelpers
<Extension()>
Public Function UnEscape(ByVal aString As String) As String
Return Regex.Unescape(aString)
End Function
End Module
Usage:
console.writeline("Ciao!\n".unEscape)

On my side I created a sub MyMsgBox replacing \n in the prompt by ControlChars.NewLine

A lot of the stuff above didn't work for me.
What did end up working is
Chr(13)

do not forget to set the Multiline property to true in textbox

msgbox("your text here" & Environment.NewLine & "more text") is the easist way. no point in making your code harder or more ocmplicated than you need it to be...

This work for me:
MessageBox.Show("YourString" & vbcrlf & "YourNewLineString")

The message box must end with a text and not with a variable

Related

How to find string by filter in VBScript?

I have a problem here:
I have an text like this
a lot of html tags
MPI-START
Hello world!
Hello world 2!
MPI-END
a lot of html tags again
And somehow I need to get text between MPI-START and MPI-END
It can contains many lines and text so I need to get them all
I tried to search but there are nothing
Any ideas?
Also sorry for my english, i from Russia
UPD: That text what i needed contains in < p data-placeholder="Your story...">TEXT</ p>
Ok, i found answer
Not perfect but works
Dim strid, outstr
strid = "many hell tags MPI-START" & vbNewLine & "Hello World!" & vbNewLine & "Hello World 2 !" & vbNewLine & "MPI-END there too"
outstr = Split (strid, "MPI-START")(1)
outstr = Split (outstr, "MPI-END")(0)
MsgBox outstr
Here is another approch using RegEx in vbscript to extract data between two delimiters :
Option Explicit
Dim Full_String,First_Delimiter,Second_Delimiter,Extracted_Data
Full_String = "a lot of html tags" & vbNewLine &_
"< p data-placeholder=""Your story..."">" & vbNewLine &_
"Hello world!" & vbNewLine &_
"Hello world 2!" & vbNewLine &_
"</ p>" & vbNewLine &_
"a lot of html tags again"
wscript.echo Full_String
First_Delimiter = "< p data-placeholder=""Your story..."">"
Second_Delimiter = "</ p>"
Extracted_Data = ExtractData(Full_String,First_Delimiter,Second_Delimiter)
wscript.echo Extracted_Data
'------------------------------------------------------------------------------------------------
Function ExtractData(Full_String,Start_Delim,End_Delim)
Dim r,Matches,Data
Set r=new regexp
r.pattern = "(?:^|(?:\r\n))(:?"& Start_Delim &"\r\n)([\s\S]*?)(?:\r\n)(?:"& End_Delim &")"
Set Matches = r.Execute(Full_String)
If Matches.Count > 0 Then Data = Matches(0).SubMatches(1)
ExtractData = Data
End Function
'------------------------------------------------------------------------------------------------

how to add an X=msgbox command using sendkeys vbs

when I try adding the " quotes in
X=msgbox"Writing",48,"Title"
but the quotes wont recognise.
I've tried using {} but that doesn't help either?
If you want to capture the result of the MsgBox in variable X, you need to put brackets around the parameters like so:
X = Msgbox("Writing",48,"Title")
you have to put in chr(34) thats quotes so you should be using the code vvv
thewshkey.sendkeys "x=msgbox" & Chr(34) & "writing" & Chr(34) & ",48," & Chr(34) & "Title" & Chr(34)

New Line for Chatbot

If ChatBot.Caption = ("Bob" & ": " & "Hello! My name is bob. What's your name?") Then
ChatBot.Caption = vbNewLine(Text.Text & ": " & Text.Text)
This is my code so far. How do you add a new line in this case, I've been trying and searching and couldn't find a thing. With every new line from the textbox, it will add a new line to the ChatBot RichTextBox like: "[username]: blah blah".
I've found out that vbCRLF can also make a new line but honestly have no idea where to place it.
For a RichTextBox the best way is to:
'// move cursor to the end of the text
rtb.SelStart = Len(rtb.Text)
'// append the text ending with a new line
rtb.SelText = "Hello" & vbCrLf
A lesser alternative is:
rtb.Text = rtb.Text & vbCrLf & "New Line 1" & vbCrLf & "New Line 2" & vbCrLf & "New Line 3 ..."

how to assign XML values to string in Vb6

I want to assign below mentioned xml values to a string like this
Dim test As String
test = ... ?
Where the XML should contain:
<RptVer>1</RptVer>
<RptTyp>1</RptTyp>
</RptInfo>
</InstRptRoot>
How can I do this and also preserve the formatting (ie linebreaks, spacing, etc.)?
Mark answered your question, I'll answer your second question:
Dim test As String
test = "<RptInfo>" & vbCrLf & vbCrLf & _
vbTab & "<RptVer>1</RptVer>" & vbCrLf & vbCrLf & _
vbTab & "<RptTyp>1</RptTyp> & vbCrLf & vbCrLf & _
"</RptInfo>"
Assuming you want it double-spaced and indented. You had also missed the leading tag, but MarkL caught that as well.

VBS End of the statement expected error in

strCode = "Private Sub AcclvsTime() " & vbCr _
& "Set myChtObj = oExcelWriteWorkSheet.ChartObjects.Add(100,375,75,225) "& vbCr _
& "myChtObj.Chart.ChartType = 4 " & vbCr _
& "myChtObj.Chart.SetSourceData objWriteWorkbook.Sheets("sheet2").Range("A1:B15")" & vbCr _
& "End Sub"
objWriteExcel.VBE.ActiveVBProject.VBComponents.Item("Sheet1").CodeModule.AddFromString(strCode)
When I executed this code i got the error “end of the statement expected in line 4” (& "myChtObj.Chart.SetSourceData objWriteWorkbook.Sheets("sheet2").Range("A1:B15")" & vbCr _)
Can any one help me where is the mistake?
#paxdiablo: I would comment, but don't see a comment button.
Notice, though, the second quote from Range("A1:B15")" & vbCr is started from "myChtObj.Chart.SetSourceData
Having the full block of code would help better, as we can't tell what kind of end statement you will need. I.e, your "End Sub" is in double quotes. If that's the end of the sub, you need to take them out.
Building strings by concatenation is cumbersome and errorprone. Especially, if the result is a multiline string, use Join:
strCode = Join( Array( _
"Private Sub AcclvsTime()" _
, " Set myChtObj = oExcelWriteWorkSheet.ChartObjects.Add(100,375,75,225)" _
, " myChtObj.Chart.ChartType = 4" _
, " myChtObj.Chart.SetSourceData objWriteWorkbook.Sheets(""sheet2"").Range(""A1:B15"")" _
, "End Sub" _
), vbCrLf)
WScript.Echo strCode
output:
Private Sub AcclvsTime()
Set myChtObj = oExcelWriteWorkSheet.ChartObjects.Add(100,375,75,225)
myChtObj.Chart.ChartType = 4
myChtObj.Chart.SetSourceData objWriteWorkbook.Sheets("sheet2").Range("A1:B15")
End Sub
to reduce the noise caused by & and the repeating stuff vbCr(Lf). That will improve your chances to see the problems/mistakes. (Exactly two literals - "sheet2", "A1:B15" - to quote).
Addional Remark:
Given that the culprit is:
"whatever("sheet2").Range("A1:B15")"
it is obvious, that remedy
"whatever(""sheet2"").Range(""A1:B15"")"
is easier to read/check/write and less errorprone than
"whatever(" & Chr(24) & "sheet2" & Crh(34) & ").Range(" & Chr(34) + "A1:B15" & Chr(32) & ")"
Avoiding "" in literals by splicing in & Chr(34) &s is a bad strategy.
" ... Range("A1:B15")" & vbCr
Note those quotes within quotes on your fourth line (for both "sheet2" and "A1:B15") - you need to fix that.
If you want to put quotes within quotes, you can do it thus, by escaping. Two consecutive " characters within a double-quoted string will be translated to a single ".
"the word ""xyzzy"" is quoted"
Alternatively, you can also use chr(34) to get the quote:
"the word " & chr(34) & "xyzzy" & chr(34) & " is quoted"
This may be preferable in more complex cases, though I've rarely had a need for it.

Resources