I'm trying to write an array of strings/numbers to a text file from VBA. It works well but when there are some RTL strings (Hebrew) the proper order of the strings in the text file seems reversed/corrupted.
Not sure why but It seems to work properly when only one RTL string is involved in the process
I'm using the VBA Print command and a Public Function (Padleft) (Code below)
fff(14) = PadLeft("RTL string1", 15, " ")
fff(15) = PadLeft(" ", 1, " ")
fff(16) = PadLeft("RTL string2", 15, " ")
For lCtr = 14 To 16
If lCtr < lFieldCount Then
Print #iFileNum, fff(lCtr) ;
Public Function PadLeft(text As Variant, totalLength As Integer,
padCharacter As String) As String
PadLeft = String(totalLength - Len(CStr(text)), padCharacter) &
CStr(text)
End Function
Related
I am trying to run the following line of code to replace the Microsoft Word quotes with ones our database can store. I need to work around users copying strings from Microsoft Word into my textareas.
instrText = instrText.Replace("“", """).Replace("”", """)
I am getting syntax errors for the number of arguments.
I have tried character escapes and a couple other ways of formatting the arguments with no luck.
This changes the 'smart' quotes from word,
'non standard quotes
Dim Squotes() As Char = {ChrW(8216), ChrW(8217)} 'single
Dim Dquotes() As Char = {ChrW(8220), ChrW(8221)} 'double
'build test string
Dim s As String = ""
For x As Integer = 0 To Squotes.Length - 1
s &= x.ToString & Squotes(x) & ", "
Next
For x As Integer = 0 To Dquotes.Length - 1
s &= (x + Squotes.Length).ToString & Dquotes(x) & ", "
Next
'replace
For Each c As Char In Squotes
s = s.Replace(c, "'"c)
Next
For Each c As Char In Dquotes
s = s.Replace(c, ControlChars.Quote)
Next
Try the following:
Private Function CleanInput(input As String) As String
DisplayUnicode(input)
'8216 = &H2018 - left single-quote
'8217 = &H2019 - right single-quote
'8220 = &H201C - left double-quote
'8221 = &H201D - right double-quote
'Return input.Replace(ChrW(&H2018), Chr(39)).Replace(ChrW(&H2019), Chr(39)).Replace(ChrW(&H201C), Chr(34)).Replace(ChrW(&H201D), Chr(34))
Return input.Replace(ChrW(8216), Chr(39)).Replace(ChrW(8217), Chr(39)).Replace(ChrW(8220), Chr(34)).Replace(ChrW(8221), Chr(34))
End Function
Private Sub DisplayUnicode(input As String)
For i As Integer = 0 To input.Length - 1
Dim lngUnicode As Long = AscW(input(i))
If lngUnicode < 0 Then
lngUnicode = 65536 + lngUnicode
End If
Debug.WriteLine(String.Format("char: {0} Unicode: {1}", input(i).ToString(), lngUnicode.ToString()))
Next
Debug.WriteLine("")
End Sub
Usage:
Dim cleaned As String = CleanInput(TextBoxInput.Text)
Resources:
ASCII table
C# How to replace Microsoft's Smart Quotes with straight quotation marks?
How to represent Unicode character in VB.Net String literal?
Note: Also used Character Map in Windows.
You have a solution that works above, but in keeping with your original form:
instrText = instrText.Replace(ChrW(8220), """"c).Replace(ChrW(8221), """"c)
I have a user form with multiline textbox. EnterKeyBehavior = True. I run the macro, the form opens, I type a few lines of text, pressing Enter after each line. The value from that textbox is put in a variable which is later put in the word document via Find/replace of placeholder text <>.
The text is put in the document and the first line in the multiline textbox entry looks OK, but every other line starts with a little empty box (some Ascii/unicode character I don't know). See pic.
I've tried to replace that little white box by doing a replace on vbCr, vbLf, vbCrLf, Char(10), Char(11), Char(13), from other posts I've found with those solutions, but none of them work for me. What's the fix?
This is what I have in the form right now and from where I'm trying to clean up the contents of that textbox before passing on the value to the variable that I then put in the word document.
Private Sub cmdCompInfoOK_Click()
txtCompanyInfo.Text = Replace(txtCompanyInfo.Text, Chr(11), "")
slkCompanyInfo = frmLtrAddress.txtCompanyInfo
Me.Hide
End Sub
The multiline field is called txtCompanyInfo.
slkCompanyInfo is the variable where I want to put the value from the textbox, and I'm declaring it as Public in the main module, which calls the userform.
Put the following code into a regular module, it will create a string with information about all characters in the input string.
Function DumpString(s As String, Optional specialCharsOnly As Boolean = False) As String
' Write all chars of String as ASCII-Value, concatenated as string
Dim i As Long
For i = 1 To Len(s)
Dim x As String
Dim a As Long, dumpMe As Boolean, c As String
a = AscW(Mid(s, i, 1))
dumpMe = Not specialCharsOnly Or a < 32 Or a > 128
If a = 13 Then
c = "<CR>"
ElseIf a = 10 Then
c = "<LF>"
ElseIf a = 9 Then
c = "<TAB>"
ElseIf a = 0 Then
c = "<NUL>"
Else
c = Mid(s, i, 1)
End If
x = a & "(" & c & ")"
If dumpMe Then
DumpString = DumpString & IIf(DumpString = "", "", ",") & x
End If
Next i
End Function
In your Event-Routine, put a statement like
Debug.Print DumpString(slkCompanyInfo)
If your input string is to long, you can set the 2nd parameter to True, in that case all regular characters are skipped.
I'm trying to convert WText into its ASCII code and put it into a TextBox; Numencrypt. But I don't want to convert the spaces into ASCII code.
How do I replace the spaces with null?
Current code:
Dim withSpace As String = Numencrypt.Text
For h = 1 To lenText
wASC = wASC & CStr(Asc(Mid$(WText, h, 1)))
Next h
Numencrypt.Text = wASC
Numencrypt2.Text = Numencrypt2.Replace(Numencrypt.Text, " ", "")
By the way, the TextBox Numencrypt2 is the WText without a space inside it.
Without knowing whether or not you want the null character or empty string I did the following in a console app so I don't have your variables. I also used a string builder to make the string concatenation more performant.
Dim withSpaces = "This has some spaces in it!"
withSpaces = withSpaces.Replace(" "c, ControlChars.NullChar)
Dim wASC As New StringBuilder
For h = 1 To withSpaces.Length
wASC.Append($"{AscW(Mid(withSpaces, h, 1))} ") ' Added a space so you can see the boundaries ascii code boundaries.
Next
Dim theResult = wASC.ToString()
Console.WriteLine(theResult)
You will find that if you use ControlChars.NewLine as I have, the place you had spaces will be represented by a zero. That position is completely ignored if you use Replace(" ", "")
I have thing problem here is the debugging outputs
"?uƒn74tn5187r&key=6e6e0936c4e6c48be56a72eba8964df0"
should be
"?u=83n74tn5187r&key=6e6e0936c4e6c48be56a72eba8964df0"
I have tried solution from another similar question and it failed me.
Dim uni As Byte() = Encoding.GetEncoding(437).GetBytes("?uƒn74tn5187r&key=6e6e0936c4e6c48be56a72eba8964df0")
Dim Ascii As String = Encoding.ASCII.GetString(uni)
Ascii =
"?u?n74tn5187r&key=6e6e0936c4e6c48be56a72eba8964df0"
I'm guessing I have to guess the 437.. maybe a brute force attack on all numbers until the match of ?u=83 from ?uƒ
Really I am trying to read a Unicode-32 (Brasil formatted text from email (POP3). Now that I think about it =83 could be messed up using this function here.
But without this function, the body of the POP3 email will contain maybe useless like variant of urlencode() but.. instead of %20 it uses =20.
I wonder how to fix this.
Public Shared Function DecodeQuotedPrintable(ByVal Message As String, Optional ByVal QuickClean As Boolean = False) As String
'set up StringBuilder object with data stripped of any line continuation tags
Dim Msg As New StringBuilder(Message.Replace("=" & vbCrLf, vbNullString))
If QuickClean Then 'perform a quick clean (clean up common basics)
Return Msg.Replace("=" & vbCrLf, vbNullString).Replace("=0D", vbCr).Replace("=0A", _
vbLf).Replace("=20", " ").Replace("=3D", "=").ToString
Else 'perform total cleaning
'store 2-character hex values that require a leading "0"
Dim HxData As String = "X0102030405060708090A0B0C0D0E0F"
For Idx As Integer = 1 To &HF 'initially process codes 1-15, which require a leading zero
Msg.Replace("=" & Mid(HxData, Idx << 1, 2), Chr(Idx)) 'replace hex data with single character code (SHIFT is faster)
Next
For idx As Integer = &H10 To &HFF 'process the whole 8-bit extended ASCII gambit
Msg.Replace("=" & Hex(idx), Chr(idx)) 'replace hex data with single character code
Next
Return Msg.ToString 'return result string
End If
End Function
Edit:
My attempt at fixing the function (if it really causes the problem? I'll never know)
Public Shared Function DecodeQuotedPrintable(ByVal Message As String, Optional ByVal QuickClean As Boolean = False) As String
'set up StringBuilder object with data stripped of any line continuation tags
Dim Msg As New StringBuilder(Message.Replace("=" & vbCrLf, vbNullString))
If QuickClean Then 'perform a quick clean (clean up common basics)
Return Msg.Replace("=" & vbCrLf, vbNullString).Replace("=0D", vbCr).Replace("=0A", _
vbLf).Replace("=20", " ").Replace("=3D", "=").ToString
Else 'perform total cleaning
'store 2-character hex values that require a leading "0"
Msg.Replace("=" & vbCrLf, vbNullString).Replace("=0D", vbCr).Replace("=0A", _
vbLf).Replace("=20", " ").Replace("=3D", "%$##[EQUALS]##$%").ToString()
Dim HxData As String = "X0102030405060708090A0B0C0D0E0F"
For Idx As Integer = 1 To &HF 'initially process codes 1-15, which require a leading zero
Msg.Replace("=" & Mid(HxData, Idx << 1, 2), Chr(Idx)) 'replace hex data with single character code (SHIFT is faster)
Next
For idx As Integer = &H10 To &HFF 'process the whole 8-bit extended ASCII gambit
Msg.Replace("=" & Hex(idx), Chr(idx)) 'replace hex data with single character code
Next
Msg.Replace("%$##[EQUALS]##$%", "=")
Return Msg.ToString 'return result string
End If
End Function
"ƒ" is represented by =83 in Quoted Printable encoding in the Windows-1252 character set.
I have a string of text i captured within AutoCAD (0.000000, 0.000000, 0.000000) wich is saved to a text based file named position.txt.
as you probably have gatherd with a file name such as position.txt the text could be composed of any random number combination eg: (5.745379, 0.846290, 150.6459046).
However for it to be of any use to me I need the captured string to exist without spaces or brackets how can i achiev this in VB.net?
Use String.Replace. Its probably not the most efficient way but it will get the job done.
Dim file as String = My.Computer.FileSystem.ReadAllText("position.txt")
Dim output as String = file.Replace(" ", "") _
.Replace("(", "") _
.Replace(")", "")
My.Computer.FileSystem.WriteAllText("output.txt", output, false)
as above
s = "(5.745379, 0.846290, 150.6459046)"
s = s.replace("(","")
s = s.replace(")","")
and then
dim answer() as string = s.split(",")
dim number as double
For each a as string in answer
if double.tryparse(a,n) then
console.writeline(n.tostring & " is a number")
else
console.writeline(n.tostring & " is rubbish")
next