An app operating under Windows 10 finds a byteOrderMarkUtf8 in a string, but there isn't one in the string - vb.net

I have an app that receives an XML string and tries to clean it up before processing. For some reason, under the Windows 10 operating system, the app thinks there is a byteOrderMarkUtf8 leading the string. There isn't one.
The first character is "<". The app removes the "<", and then removes the rest of the tag, too, creating an invalid XML.
This used to work under Windows 7.
In the code below, I have commented the offending section out.
Is there something about character encoding that has changed with Windows 10 that would be causing this?
Private Sub CleanXML(ByRef InString As String)
' This subroutine cleans trash characters out of XML streams
If (InString = "") Then
MessageBox.Show("Null String passed to CleanXML." & vbCr & _
"String Length: " & InString.Length & vbCr & _
"Instring: " & InString & vbCr)
End If
If (InString.Length = 0) Then
MessageBox.Show("String of 0 length or null String passed to CleanXML." & vbCr & _
"String Length: " & InString.Length & vbCr & _
"Instring: " & InString & vbCr)
End If
Dim CleanString As String = InString
CleanString = CleanString.Trim() ' Trim leading and trailing spaces
CleanString = CleanString.Replace("- ", "") ' Replace the dashes
CleanString = CleanString.Replace(" <", "<") ' Replace some white space
CleanString = CleanString.Replace(" <", "<") ' Replace some white space
CleanString = CleanString.Replace("-<", "<") ' Replace dash+lessthan with lessthan
CleanString = CleanString.Replace("- <", "<") ' Replace dash+space+lessthan with lessthan
CleanString = CleanString.Replace("&", "&") ' Replace & with &
Dim Tempstring As String = ""
If CleanString.Length > 0 Then
Try
Dim byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble())
' This is the offending code that I have commented out.
'-------------------------------------------------------------
'If (CleanString.StartsWith(byteOrderMarkUtf8)) Then
' CleanString = CleanString.Remove(0, byteOrderMarkUtf8.Length)
'End If
'If (CleanString.EndsWith(byteOrderMarkUtf8)) Then
' CleanString = CleanString.Remove(CleanString.Length - 1, byteOrderMarkUtf8.Length)
'End If
'-------------------------------------------------------------
' Make sure the first and last characters are "<" and ">".
Tempstring = CleanString
Do Until (CleanString.StartsWith("<") Or (CleanString.Length = 0))
CleanString = CleanString.Remove(0, 1)
Loop
Do Until (CleanString.EndsWith(">") Or (CleanString.Length = 0))
CleanString = CleanString.Remove(CleanString.Length - 1, 1)
Loop
Catch ex As Exception
MessageBox.Show("Error in CleanXML." & vbCr & _
"String Length: " & CleanString.Length & vbCr & _
"Instring: " & InString & vbCr & _
"CleanString: " & CleanString & _
" Length: " & CleanString.Length.ToString)
MessageBox.Show(ex.Message + " Inner exception: " + ex.InnerException.Message)
MessageBox.Show(Tempstring)
End Try
Else
MessageBox.Show("Clean string of 0 length in CleanXML." & vbCr & _
"String Length: " & CleanString.Length & vbCr & _
"Instring: " & InString & vbCr & _
"CleanString: " & CleanString)
End If
' Remove any BOMs (Byte-Order Marks) from the string.
'Dim i As Integer = InStr(1, CleanString, byteOrderMarkUtf8)
'Do Until i = 0
' CleanString = CleanString.Remove(i - 1, byteOrderMarkUtf8.Length)
' i = InStr(i, CleanString, byteOrderMarkUtf8)
'Loop
InString = CleanString
End Sub

Related

Cannot individual add digits of number together in vba

I want to be able to add together the individual digits of a 4 digit number, but it does not seem to work.
I am doing this purely in VBA code. The result is output to a worksheet.
I have extracted part of my code and put it into a separate macro to test it and still get the same result. It concatenates the digits together.
I have added in lots of msgbox lines to see what it is doing, but cannot work out why, in this case, the value of main is not added up into the intm variable.
The variables intd1 to intd4 get the values correctly, but when I try to add them together into intm, it just concatenates them together.
Sub AddDigits()
'
' Add individual digits of number together
'
Dim intd1, intd2, intd3, intd4, main, intm As Integer
main = 1234
intd1 = Left(main, 1)
MsgBox (intd1)
intd2 = Mid(main, 2, 1)
MsgBox (intd2)
intd3 = Mid(main, 3, 1)
MsgBox (intd3)
intd4 = Right(main, 1)
MsgBox (intd4)
intm = intd1 + intd2 + intd3 + intd4
MsgBox ("intm = " & intm & Chr(13) & _
"intd1 = " & intd1 & Chr(13) & _
"intd2 = " & intd2 & Chr(13) & _
"intd3 = " & intd3 & Chr(13) & _
"intd4 = " & intd4 & Chr(13))
End Sub
When you declare the variables the way you did, the first bit are all "variants", and VBA your use of Mid, Left, and Right are all string functions, so VBA coverts the variant to Strings:
If you dim your variables properly, you get the expected result:
Sub AddDigits()
'
' Add individual digits of number together
'
Dim intd1 As Integer, _
intd2 As Integer, _
intd3 As Integer, _
intd4 As Integer, _
main As Integer, _
intm As Integer
main = 1234
intd1 = Left(main, 1)
MsgBox (intd1)
intd2 = Mid(main, 2, 1)
MsgBox (intd2)
intd3 = Mid(main, 3, 1)
MsgBox (intd3)
intd4 = Right(main, 1)
MsgBox (intd4)
intm = intd1 + intd2 + intd3 + intd4
MsgBox ("intm = " & intm & Chr(13) & _
"intd1 = " & intd1 & Chr(13) & _
"intd2 = " & intd2 & Chr(13) & _
"intd3 = " & intd3 & Chr(13) & _
"intd4 = " & intd4 & Chr(13))
End Sub
In addition to the above answer, you can also convert the data type anywhere later in the code. To convert anything to int use Cint. Similarly for other type conversion you check the link
intm = CInt(intd1) + CInt(intd2) + CInt(intd3) + CInt(intd4)

vba macro display result of loop to msgbox

I creted a loop checking number of characters length with conditions but sadly it's not properly working,
with approriate no. of loops but not reading the next line, I want to post the result in a MsgBox,
but when I use the msgbox inside the loop I will get a msgbox for every result found or only one msgbox with one result.
What I would like is to display every result in 1 msgbox with a line vbNewLine after each result.
Below is my code:
Public Sub Rs()
Dim Text As String
Dim NumChar As String
Dim i As Integer
Dim NumRows As Long
Application.ScreenUpdating = False
'Get Cell Value
Text = Range("B2").Value
'Get Char Length
NumChar = Len(Text)
NumRows = Range("B2", Range("B2").End(xlDown)).Rows.Count
Range("B2").Select
For i = 1 To NumRows
'Character length validation
If Len(Text) <= 15 Then
MsgBox Chr(149) & " SVC_DESC " & Text & " has " & NumChar & " characters " & " and it's Valid !" & vbNewLine
Else
MsgBox Chr(149) & " SVC_DESC " & Text & " has " & NumChar & " characters " & " and Exceeded allowable number of characters!" & vbNewLine
End If
Next i
Application.ScreenUpdating = True
End Sub
Assign the new text to a string variable and display the string variable outside the loop:
Option Explicit
Sub TestMe()
Dim i As Long
Dim displayText As String
For i = 1 To 3
displayText = displayText & vbCrLf & i
Next i
MsgBox displayText
End Sub
Build a string through concatenation and display the strings after exiting the loop.
Public Sub Rs()
Dim Text As String
Dim NumChar As String
Dim i As Integer
Dim NumRows As Long
dim msg1 as string, msg2 as string
Application.ScreenUpdating = False
'Get Cell Value
Text = Range("B2").Value
'Get Char Length
NumChar = Len(Text)
NumRows = Range("B2", Range("B2").End(xlDown)).Rows.Count
Range("B2").Select
For i = 1 To NumRows
'Character length validation
If Len(Text) <= 15 Then
msg1 = msg1 & Chr(149) & " SVC_DESC " & Text & " has " & NumChar & " characters " & " and it's Valid !" & vbLF
Else
msg2 = msg2 & Chr(149) & " SVC_DESC " & Text & " has " & NumChar & " characters " & " and Exceeded allowable number of characters!" & vbLF
End If
Next i
Application.ScreenUpdating = True
if cbool(len(msg1)) then
msg1 = left(msg1, len(msg1)-1)
MsgBox msg1
end if
if cbool(len(msg2)) then
msg2 = left(msg2, len(msg2)-1)
MsgBox msg2
end if
End Sub
A MsgBox uses Chr(10) aka vbLF for new lines; vbNewLine is overkill.

VBA text to columns with "" delimiters

I will be quick.
I have a variable 'strLine' with text:
The exact string will look like that:
"Text1","Text2","Text3","Text4","Text5"
So, delimiter in my case is: "
How I can extract text and write it in columns.
Expecting results in cells:
A1=Text1
B1=Text2
C1=Text3
D1=Text4
E1=Text5
Thanks
Use Split function, it'll return a 0-based array (ergo +1 in cells) :
Sub test_Andy()
Dim StrLine As String
Dim Str() As String
StrLine = Range("A2").Value 'If your value is in A2
'If you input the value manually, not really practical with so much "
StrLine = Chr(34) & "Text1" & Chr(34) & "," & Chr(34) & "Text2" & Chr(34) & "," & Chr(34) & "Text3" & Chr(34) & "," & Chr(34) & "Text4" & Chr(34) & "," & Chr(34) & "Text5" & Chr(34)
Debug.Print StrLine '"Text1","Text2","Text3","Text4","Text5"
Str = Split(StrLine, ",")
Dim i As Integer
For i = LBound(Str) To UBound(Str)
Cells(1, i + 1) = Str(i)
Cells(2, i + 1) = Replace(Str(i), Chr(34), vbNullString)
Next i
End Sub
You can use Split to split the text into an array, then remove the " from the start and the end of the parts using Mid :
strText = """Text1"",""Text2"",""Text3"",""Text4"",""Text5"""
aSplit = Split(strText, ",")
For Each strCurrent in aSplit
MsgBox Mid(strCurrent, 2, Len(strCurrent) - 2)
Next
Remark : You might want to add some checks to ensure that there is a " at the start and end before removing them.
edited to simulate a StrLine loop:
Dim iRow As Long
irow = 1
For Each StrLine In StrLines '<--' assuming a loop through many StrLines
Str = Split(Replace(StrLine, """", ""), ",")
Cells(iRow, 1).Resize(, UBound(Str) - LBound(Str)).Value = Str
iRow = iRow + 1
Next

Trim both newlines and spaces in VBA 7.0

Trim (in VBA for MS-Access 2010) does not remove vbCrLfs, only spaces. In the immediate window, I get
? Len(vbCrLf & "a" & vbCrLf & "b" & vbCrLf)
8
? Len(Trim(vbCrLf & "a" & vbCrLf & "b" & vbCrLf))
8
For spaces however:
? Len(" " & "a" & " " & "b" & " ")
5
? Len(Trim(" " & "a" & " " & "b" & " "))
3
How to make a trim that removes vbCrLFs on the ends only?
If you don't mind removing ALL new lines (and not just edges) you could just do:
myStr = Application.clean(Application.trim(myStr))
For imitating Trim function, you'd need to test each character in your string's edges:
Function TrimNewLines(mtStr As String) As String
Dim pattern As String, c As String
Dim i As Integer: i = 1
pattern = "[" & Chr(10) & Chr(13) & "]"
c = Mid(mtStr, i, 1)
Do While c Like pattern
i = i + 1
c = Mid(mtStr, i, 1)
Loop
mtStr = Mid(mtStr, i, Len(mtStr))
i = Len(mtStr)
c = Mid(mtStr, i, 1)
Do While c Like pattern
i = i - 1
c = Mid(mtStr, i, 1)
Loop
mtStr = Mid(mtStr, 1, i)
TrimNewLines = mtStr
End Function
This seems to have done the trick:
Public Function trimNewlinesAndSpaces(chaine As String) As String
chaine = Trim(chaine)
Do While (left(chaine, 2) = vbCrLf) Or right(chaine, 2) = vbCrLf
If left(chaine, 2) = vbCrLf Then
chaine = right(chaine, Len(chaine) - 2)
End Ifj
If right(chaine, 2) = vbCrLf Then
chaine = left(chaine, Len(chaine) - 2)
End If
chaine = Trim(chaine)
Loop
trimNewlinesAndSpaces = chaine
End Function

Word split fails with more then 3 words

I try to split a string every + then every |, it works fine when I try to read only 3 words from the split words1(0-3), but when I try to read words1(4) the whole function fails... here is the code:
Private Function SetUpdateData()
Try
Dim delimiterChars As Char() = {"+"c}
Dim words As String() = updatelist.Split(delimiterChars)
Dim i As Integer = 1
Do While (i < words.Length)
Dim delimiterChars1 As Char() = {"|"c}
Dim words1 As String() = words(i).Split(delimiterChars1)
Dim name As String = words1(0)
Dim version As String = words1(1)
Dim fileurl As String = words1(2)
Dim size As String = (words1(3) / 1024D / 1024D).ToString("0.00") & " MB"
Dim cversion As FileVersionInfo = FileVersionInfo.GetVersionInfo(Path.Combine(Directory.GetCurrentDirectory() & "\" & name))
If My.Computer.FileSystem.FileExists(Directory.GetCurrentDirectory() & "\" & name) Then
If Not version.Contains(cversion.FileVersion) Then
DataGridView1.Rows.Add(name, version, size)
RichTextBox1.AppendText("+" & words1(0) & "|" & words1(1) & "|" & words1(2) & "|" & words1(3))
End If
Else
DataGridView1.Rows.Add(name, version, size)
RichTextBox1.AppendText("+" & words1(0) & "|" & words1(1) & "|" & words1(2) & "|" & words1(3))
End If
i = (i + 1)
Loop
Catch ex As Exception
MsgBox("error")
End Try
End Function
This above works no problem at all, but when you add words1(4) in like this:
Private Function SetUpdateData()
Try
Dim delimiterChars As Char() = {"+"c}
Dim words As String() = updatelist.Split(delimiterChars)
Dim i As Integer = 1
Do While (i < words.Length)
Dim delimiterChars1 As Char() = {"|"c}
Dim words1 As String() = words(i).Split(delimiterChars1)
Dim name As String = words1(0)
Dim version As String = words1(1)
Dim fileurl As String = words1(2)
Dim size As String = (words1(3) / 1024D / 1024D).ToString("0.00") & " MB"
Dim status As String = words1(4)
Dim cversion As FileVersionInfo = FileVersionInfo.GetVersionInfo(Path.Combine(Directory.GetCurrentDirectory() & "\" & name))
If My.Computer.FileSystem.FileExists(Directory.GetCurrentDirectory() & "\" & name) Then
If Not version.Contains(cversion.FileVersion) Then
DataGridView1.Rows.Add(name, version, size)
RichTextBox1.AppendText("+" & words1(0) & "|" & words1(1) & "|" & words1(2) & "|" & words1(3) & "|" & words(4))
End If
Else
DataGridView1.Rows.Add(name, version, size)
RichTextBox1.AppendText("+" & words1(0) & "|" & words1(1) & "|" & words1(2) & "|" & words1(3) & "|" & words(4))
End If
i = (i + 1)
Loop
Catch ex As Exception
MsgBox("error")
End Try
End Function
ALSO the string that it is splitting is:
+Thing v2.exe|1.0.0.1|http://example.com/uploads/Thing v2.exe|205824|Primary+Thing v2 DLL.dll|1.0.0.1|http://example.com/uploads/Thing DLL.dll|1097728|Secondary
Which all should output:
words1(0) - Thing v2.exe
words1(1) - 1.0.0.1
words1(2) - http://example.com/uploads/Thing v2.exe
words1(3) - 205824
words1(4) - Primary
But as I said above once words1(4) is used it crashes the whole function...
It will catch and fail and give the error message, but when I try to do msgbox(ex) for the exception error, no msgbox pops up and the program just continues.
If anyone can fix the issue or give me some help that would be greatly appreciated, thanks in advance and sorry if this is confusing as it is to me too!
There are two loops in your program:
Loop1:
words1(0)>>Thing v2.exe
words1(1)>>1.0.0.1
words1(2)>>http://example.com/uploads/Thing v2.exe
words1(3)>>205824
words1(4)>>Primary
Loop2:
words1(0)>>Thing v2 DLL.dll
words1(1)>>1.0.0.1
words1(2)>>http://example.com/uploads/Thing DLL.dll
words1(3)>>1097728
words1(4)>>Secondary
it appears that you misspelled your words1(4) to words(4) in below line
RichTextBox1.AppendText("+" & words1(0) & "|" & words1(1) & "|" & words1(2) & "|" & words1(3) & "|" & words(4))
Your split functionality works fine but there's an error on the following lines (there are 2 of them):
RichTextBox1.AppendText("+" & words1(0) & "|" & words1(1) & "|" & words1(2) & "|" & words1(3) & "|" & words(4))
The last words(4) should be words1(4).