Insert Persian Text from a file into PowerPoint - vba

I am trying to read a plaintext store as UTF-8 and split it into slides in PowerPoint. However, my text doesn't work. Persian is a subset of Arabic typesetting codes.
Function convertANSIPersiande(inputStr As String) As String
Dim n As Integer
Dim i As Integer
Dim inBytes() As Byte
Dim sUnicode As String
' Convert input string to byte array
n = Len(inputStr)
ReDim inBytes(n + 1)
For i = 1 To n
inBytes(i) = AscB(Mid(inputStr, i, 1))
Next
' Convert byte array to unicode using Persian coding
sUnicode = StrConv(inBytes, vbUnicode, &H429)
' remove starting null
iPos = InStr(sUnicode, Chr(0))
If iPos > 0 Then sUnicode = Mid(sUnicode, iPos + 1)
convertANSIPersian2Unicode = sUnicode
End Function
Now when I write
pptTable.Table.Cell(1, Count Mod 2).Shape.TextFrame.TextRange.Text = convertANSIPersian2Unicode(DataLine)
The text is a gibberish mix of Arabic characters with special chars. I have Widnows 10 set to show Persian text.
Any help would be greatly appreciated.

Related

Converting string to ASCII number

I am trying to write a function using Excel VBA to convert a string to its respective ASCII number. For example:
"ABCD" => "65666768"
I have written this code but it's failed to do the conversion:
Public Function asciien(s As String) As String
' Returns the string to its respective ascii numbers
Dim i As Integer
For i = 1 To Len(s)
asciien = asciien & CStr(Asc(Mid(s, x, 1)))
Next i
End Function
This line
asciien = asciien & CStr(Asc(Mid(s, x, 1)))
should read
asciien = asciien & CStr(Asc(Mid(s, i, 1)))
"x" has no value

Random string split into characters

I have a procedure, that generates a random string without any delimiter. I store return value of this as a string, but I would like to split this text into characters and after that examine each characters. I try to use above code to split string into characters, but it gives Type mismatch error.
Sub gen()
Dim s As String
s = textgen(4000, 5)
Dim buff() As String
ReDim buff(Len(s) - 1)
For i = 1 To Len(s)
buff(i - 1) = Mid$(s, i, 1)
Next
MsgBox (buff) ' type mismatch
End Sub
The type of buff is string() - an array of strings (VBA doesn't have a Char type).
MsgBox wants a String message, not an array of these; you'll have to Join the elements:
MsgBox Join(buff) 'no error

Create a function to enter a to z in a dynamic array using redim preserve

I am learning VB scripting and i am very much new to this concept...
Please tell how to writ the below program
Create a function to enter a to z in a dynamic array using redim preserve.
Thanks
Kumar
I happened to already have a snippet to do what you were looking for, so here it is:
Const StartChar = "a"c
Const EndChar = "z"c
Dim converterOp As New Func(Of Char, Integer)(Function(input As Char) Char.ConvertToUtf32(input, 0))
Dim result() As Char = {}
For i = converterOp(StartChar) To converterOp(EndChar)
ReDim Preserve result(UBound(result) + 1)
result(UBound(result)) = CChar(Char.ConvertFromUtf32(i))
Next
Given a starting character and an ending character in a range of characters (in this case "a"c to "z"c), it converts both characters to their Unicode code point values in order to loop numerically from start to end in a For loop. Within each loop iteration, it adds a new array element to the end of the array, converts the current loop index -- which is, again, a Unicode code point somewhere between the start character and the end character -- back to a character, and fills the new array element with that character.
I know you specifically wanted to do it using a ReDim Preserve, but this is a more-efficient way to do it since it's not incurring the overhead of re-dimensioning the array in every loop iteration:
Const StartChar = "a"c
Const EndChar = "z"c
Dim converterOp As New Func(Of Char, Integer)(Function(input As Char) Char.ConvertToUtf32(input, 0))
Dim intStartCodePoint = converterOp(StartChar)
Dim intEndCodePoint = converterOp(EndChar)
Dim result(intEndCodePoint - intStartCodePoint) As Char
Dim arrIndex = 0
For i = intStartCodePoint To intEndCodePoint
result(arrIndex) = CChar(Char.ConvertFromUtf32(i))
arrIndex += 1
Next
The difference here is that it dimensions the array before entering the loop, using the start and end code points to calculate the size of the array.

Force an integer to be two digits - VB

I have a variable that holds a string "02100030000000000000000000D5010008D501000804" and I'm byte separating the string using
For i As Integer = 1 To (stringReader.Length - 1) Step 2
'Get the successive 2-character substrings, parse as bytes and add to total
Dim b As Byte = Byte.Parse(stringReader.Substring(i, 2), NumberStyles.AllowHexSpecifier)
sum = sum + CInt(b)
Next
I'm converting the strings to direct integers.e.g:(string"10" to Integer10 and ). That works fine. But Whenever I convert the string"02" to Integer, I get only Integer(2) and I need Integer(02). How can I proceed with?
My code is:
stringReader = fileReader.ReadLine()
byt = stringReader(1) + stringReader(2)
stringreader contains something like "100030000000000000000000D5010008D501000804"
Byte separation
For i As Integer = 1 To (stringReader.Length - 1) Step 2
'Get the successive 2-character substrings, parse as bytes and add to total
Dim b As Byte = Byte.Parse(stringReader.Substring(i, 2), NumberStyles.AllowHexSpecifier)
sum = sum + CInt(b)
Next
You can use
number.ToString("D2")
where number is an integral type like System.Int32(Integer).
Further reading: Standard Numeric Format Strings: The Decimal ("D") Format Specifier
If you have a String instead you could also use String.PadLeft:
"2".PadLeft(2, "0"c) ' -> "02"

Extract characters from a long string and reformat the output to CSV by using keywords with VB.net

I am new to VB.Net 2008. I have a task to resolve, it is regading extracting characters from a long string to the console, the extracted text shall be reformatted and saved into a CSV file. The string comes out of a database.
It looks something like: UNH+RAM6957+ORDERS:D:96A:UN:EGC103'BGM+38G::ZEW+REQEST6957+9'DTM+Z05:0:805'DTM+137:20100930154
The values are seperated by '.
I can query the database and display the string on the console, but now I need to extract the
Keyword 'ORDERS' for example, and lets say it's following 5 Characters. So the output should look like: ORDERS:D:96A then I need to extract the keyword 'BGM' and its following five characters so the output should look like: BGM+38G:
After extracting all the keywords, the result should be comma seperated and look like:
ORDERS:D:96A,BGM+38G: it should be saved into a CSV file automatically.
I tried already:
'Lookup for containing KeyWords
Dim FoundPosition1 = p_EDI.Contains("ORDERS")
Console.WriteLine(FoundPosition1)
Which gives the starting position of the Keyword.
I tried to trim the whole thing around the keyword "DTM". The EDI variable holds the entire string from the Database:
Dim FoundPosition2 = EDI
FoundPosition2 = Trim(Mid(EDI, InStr(EDI, "DTM")))
Console.WriteLine(FoundPosition2)
Can someone help please?
Thank you in advance!
To illustrate the steps involved:
' Find the position where ORDERS is in the string.'
Dim foundPosition = EDI.IndexOf("ORDERS")
' Start at that position and extract ORDERS + 5 characters = 11 characters in total.'
Dim ordersData = EDI.SubString(foundPosition, 11)
' Find the position where BGM is in the string.'
Dim foundPosition2 = EDI.IndexOf("BGM")
' Start at that position and extract BGM + 5 characters = 8 characters in total.'
Dim bgmData = EDI.SubString(foundPosition2, 8)
' Construct the CVS data.'
Dim cvsData = ordersData & "," & bgmData
I don't have my IDE here, but something like this will work:
dim EDI as string = "UNH+RAM6957+ORDERS:D:96A:UN:EGC103'BGM+38G::ZEW+REQEST6957+9'DTM+Z05:0:805'DTM+137:20100930154"
dim result as string = KeywordPlus(EDI, "ORDER", 5) + "," _
+ KeywordPlus(EDI, "BGM", 5)
function KeywordPlus(s as string, keyword as string, length as integer) as string
dim index as integer = s.IndexOf(keyword)
if index = -1 then return ""
return s.substring(index, keyword.length + length)
end function
for the interrested people among us, I have put the code together, and created
a CSV file out of it. Maybe it can be helpful to others...
If EDI.Contains("LOC") Then
Dim foundPosition1 = EDI.IndexOf("LOC")
' Start at that position and extract ORDERS + 5 characters = 11 characters in total.'
Dim locData = EDI.Substring(foundPosition1, 11)
'Console.WriteLine(locData)
Dim FoundPosition2 = EDI.IndexOf("QTY")
Dim qtyData = EDI.Substring(FoundPosition2, 11)
'Console.WriteLine(qtyData)
' Construct the CSV data.
Dim csvData = locData & "," & qtyData
'Console.WriteLine(csvData)
' Creating the CSV File.
Dim csvFile As String = My.Application.Info.DirectoryPath & "\Test.csv"
Dim outFile As IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(csvFile, True)
outFile.WriteLine(csvData)
outFile.Close()
Console.WriteLine(My.Computer.FileSystem.ReadAllText(csvFile))
End IF
Have fun!