FileSystem.WriteAllText adds non-printable characters - vb.net

Here are two methods for writing text to a file in VB.Net 2012. The first one prepends the same three non-printable characters to each file: . The second one works as expected and does not add the three characters. objDataReader is an OleDB datareader.
Any idea why?
Greg
My.Computer.FileSystem.WriteAllText(lblLocation.Text & "\" &
objDataReader("MessageControlId").ToString & ".txt", objDataReader("MsgContents").ToString, False)
Using outfile As New StreamWriter(lblLocation.Text & "\" & objDataReader("MessageControlId").ToString & ".txt")
outfile.Write(objDataReader("MsgContents").ToString)
End Using

Thanks. I found the entry below I after Googled BOM, in case anyone wants a more detailed explanation. While the BOM was not visible in a text editor it did cause problems when I passed the file to our HL7 interface engine.
Greg
Write text files without Byte Order Mark (BOM)?

Related

Indentify line breaks in excel VBA

I'm trying to identify a cell has line breaks(not the menu bar cell option, actual multiple lines through alt+enter), and extract each line separatedely
I have tried both
InStr(str, "\n") < 0
and
Split(str, "\n")
But doesn't seem to be working
VBA is not C# ("\n"). Line breaks you'll find on: vbCr or vbLf or vbCrLf constants.
For further information, please see:
vbCr
vbLf
vbCrLf
[EDIT]
Points to Mat's Mug answer! I forgot about vbNewLine constant.
There are no escape sequences in VBA. Use the built-in vbNewLine constant instead for the equivalent:
hasLineBreaks = InStr(str, vbNewLine) > 0
Per MSDN, vbNewline returns a Platform-specific new line character; whichever is appropriate for current platform, that is:
Chr(13) + Chr(10) [on Windows] or, on the Macintosh, Chr(13)
So you don't need to work with ASCII character codes, or even with their respective built-in constants.
Except Excel will strip CR chars from cell and shape contents, and this has nothing to do with VBA (the CR chars would be stripped all the same and "\n" wouldn't work for correctly reading that Excel data in C#, Javascript, or Python either) and everything to do with the context of where the string came from.
To read "line breaks" in a string with the CR chars stripped, you need to look for line feed chars in the string (vbLf).
But if you systematically treat the line feed character as a line ending, you'll eventually run into problems (esp.cross-platform), because ASCII 10 all by itself isn't an actual line break on either platform, and you'll find ASCII 13 characters in strings you thought you had stripped line breaks from, and they'll still properly line-break on a Mac, but not on Windows.
Consider either:
Split(str, Chr(10))
or
Split(str, Chr(13))
You may need to try both if the data has been imported from external source.

Why is my StreamReader reading the title of the text file?

So I'm working on my A Level coursework and have hit a block that I can't seem to work out. Basically, I have a bit of code that sends an email using text from a text file as well as a bit of hard coded text, all put into one variable before being passed onto the subroutine for sending an email.
I'm using a stream reading for both the email subject and body (as they're each in separate text files) and, while they're working fine elsewhere in the program, they're not working here. Rather than putting the contents of the text file into the variable before it's used to send an email, they're just putting the name of the text file there.
Also, the name of the file does not appear anywhere in the text stored within
E.g. The body of the email that it sent had the hard coded message and then it just said 'AbsenceEmailBody' (name of the text file) rather than what was inside the text file itself
Here's the lines of code where the files are read and put into variables
Dim objReaderSubject As New System.IO.StringReader("AbsenceEmailSubject")
Dim objReaderBody As New System.IO.StringReader("AbsenceEmailBody")
Dim EmailBody As String
Dim EmailSubject As String
EmailBody = "Dear " & CadetDS.Tables("CADET").Rows(0).Item("CadetFirstName") & "," & vbNewLine & vbNewLine & "It has come to the staff's attention that you have attended only " & ProblemAttendances(i, 1, 0, 0, 0) & "% of sessions." & vbNewLine & objReaderBody.ReadToEnd
EmailSubject = objReaderSubject.ReadToEnd
I also check the files exist before any of this
If System.IO.File.Exists("AbsenceEmailSubject") = True And System.IO.File.Exists("AbsenceEmailBody") = True Then
I've looked around everywhere and can't seem to find an answer. Also, this is my first time ever asking a question on here (or anywhere online) so if you need any more information just ask

Escape Characters in a resource file string?

I've been globalizing an application and have been using Resx Manager to make my life easier. I ran into a multi-line string literal and it stumped me.
How would I handle the escape characters when making this string into a resource?
If Not RelayMessage(
"Are you sure you want to do the selected action?" & vbCrLf &
"A confirmation message will be sent to the user." & vbCrLf &
"Please ensure you want to perform this action before hitting accept.",
My.Resources.Confirmation, RelayMessageOptions.Confirm_YesNo) =
DialogResult.Yes
How would I make that string into a resource?
In the standard VS resource manager (is this the manager you're using?) you can enter a multi-line string resource directly in the editor by using shift-Enter:
Note that this is actually stored as a string with CR+LF pairs, assisted by the space="preserve" attribute. Viewing the .resx file in a text editor:
Results using a standard message box:
MessageBox.Show(strings.myString)
I don't know how it is usually handled in globalization problems. But an easy way would be to define your own escape character formats. For example you could define \n as a newline character. When you actually use your ressource you could then use
If Not RelayMessage(Strings.Replace(myResourceString, "\n", vbCrLf),
My.Resources.Confirmation, RelayMessageOptions.Confirm_YesNo) =
DialogResult.Yes
instead of
If Not RelayMessage(myResourceString,
My.Resources.Confirmation, RelayMessageOptions.Confirm_YesNo) =
DialogResult.Yes
Or you could manually add chars with character codes 10 and 13 (e.g. ChrW(10) & ChrW(13)) at the vbCrLf location in your ressource string. This equals a vbCrLf (meaning a carriage return (10) + line feed (13)). This would avoid manipulation of the source code. Other stuff like Tab (9) have codes, too. These are called control characters. Take a look at the wikipedia http://en.wikipedia.org/wiki/Control_character

VB6 app printing Crystal Report to Adobe Distiller PDF - how to set PDF filename?

I've got a legacy app that I'm maintaining. It's a VB6 app that calls a Crystal Report (Crystal 8 it looks like) and then prints programmatically to the default printer, which is set up as Acrobat Distiller (v5.0). When it prints to PDF, it's automatically placing the resulting PDF in a folder (c:\pdf) and naming it as the first 5 characters of the crystal report filename. What's happening is that two reports with the same characters at the front of the filename are getting printed one after another, and the second is overwriting the first.
Are there settings somewhere for how distiller produces output? Can I adjust the output path, or the filename? Where/why is it only using the first five characters of the report filename as output? Or is that a Crystal function?
Is there a way to define the output PDF filename when printing from Crystal? It is printing the report like so:
With CrPt
.Connect = "DSN=" & Trim(sServerName) & ";UID=usernam;PWD=password;DSQ=database"
.ReportFileName = sReport
.Formulas(0) = "version=""" & App.Major & "." & App.Minor & "." & App.Revision & """"
.Destination = crptToPrinter
.Action = 1
End With
Any ideas?
I assume this is just a code snippet and there's more to the entire process. Try searching your entire VB6 code for Sreport. If you can find how Sreport is defined, that may answer your question.
I'm guessing somewhere is a line that says something like,
Sreport="C:\pdf\" & left(somevar,5)
Change that 5 to a 10 and you're good to go.

New line character in VB.Net?

I am trying to print a message on a web page in vb.net. I am trying to get the messages in new lines. I tried using the "\r\n" and the new line character. But this is getting printed in the page instead of it comming to the next line. Please let me know if there is any alternative.
Check out Environment.NewLine. As for web pages, break lines with <br> or <p></p> tags.
Environment.NewLine is the most ".NET" way of getting the character, it will also emit a carriage return and line feed on Windows and just a carriage return in Unix if this is a concern for you.
However, you can also use the VB6 style vbCrLf or vbCr, giving a carriage return and line feed or just a carriage return respectively.
The proper way to do this in VB is to use on of the VB constants for newlines. The main three are
vbCrLf = "\r\n"
vbCr = "\r"
vbLf = "\n"
VB by default doesn't allow for any character escape codes in strings which is different than languages like C# and C++ which do. One of the reasons for doing this is ease of use when dealing with file paths.
C++ file path string: "c:\\foo\\bar.txt"
VB file path string: "c:\foo\bar.txt"
C# file path string: C++ way or #"c:\foo\bar.txt"
You need to use HTML on a web page to get line breaks. For example "<br/>" will give you a line break.
If you are using something like this.
Response.Write("Hello \r\n")
Response.Write("World \r\n")
and the output is
Hello\r\nWorld\r\n
Then you are basically looking for something like this
Response.Write("Hello <br/>")
Response.Write("World <br/>")
This will output
Hello
World
you can also just define "<br />" as constant and reuse it
eg.
Public Const HtmlNewLine as string ="<br />"
Response.Write("Hello " & HtmlNewLine)
Response.Write("World " & HtmlNewLine)
it's :
vbnewline
for example
Msgbox ("Fst line" & vbnewline & "second line")
Try Environment.NewLine.
Your need to use the html/xhtml break character:
<br />
you can solve that problem in visual basic .net without concatenating your text, you can use this as a return type of your overloaded Tostring:
System.Text.RegularExpressions.Regex.Unescape(String.format("FirstName:{0} \r\n LastName: {1}", "Nordanne", "Isahac"))
In asp.net for giving new line character in string you should use <br> .
For window base application Environment.NewLine will work fine.
VbCr
Try that.
In this case, I can use vbNewLine, vbCrLf or "\r\n".
vbCrLf is a relic of Visual Basic 6 days. Though it works exactly the same as Environment.NewLine, it has only been kept to make the .NET api feel more familiar to VB6 developers switching.
You can call the String.Replace() function to avoid concatenation of many single string values.
MsgBox ("first line \n second line.".Replace("\n", Environment.NewLine))
Environment.NewLine or vbCrLf or Constants.vbCrLf
More information about VB.NET new line:
http://msdn.microsoft.com/en-us/library/system.environment.newline.aspx
I had the need to store line breaks in a string in a SQL table and have them displayed in vb.NET. My solution was to include a string like this in my database:
"This is the first line{0}This is the second{0}This is the third"
In vb.NET, I processed the string like this before using it:
Label2.Text = String.Format(stringFromSQLquery, vbCrLf)
This replaces every occurance of {0} with vbCrLf