MS Word VBA to display Unicode strings - vba

We have some VBA code in MS Word 2010 which needs to display Persian (Farsi) words dynamically; in other words, depending on which buttons the user clicks on the VBA app window, we will display a different string of Persian.
The only way we discovered to do this was to use something like this and concatenate a very long string character by character: the ChrW(&633). Do you know another way we can do this? The strings will remain the same, but we need a couple of different ones. Can they be loaded from a file?
Your help is much appreciated. Thanks.

Lets have an UTF-8 text file unicode.txt, a form UserForm1 with label Label1 and Button CommandButton1:
'ensure reference is set to Microsoft ActiveX DataObjects library
'(the latest version of it) under "tools/references"
Dim adoStream As ADODB.Stream
Dim var_String As Variant
Set adoStream = New ADODB.Stream
adoStream.Charset = "UTF-8"
adoStream.Open
adoStream.LoadFromFile "unicode.txt"
Label1 = adoStream.ReadText
adoStream.Close

Related

store word content in variable

How do I copy the entire content (approx 2 pages) of a Word document in VBA and store in a variable?
I keep trying several things, none of which works:
Dim mainData As String
ThisDocument.Activate
ActiveDocument.WholeStory 'error on this line
mainData = Selection.Text
With 'record macro' I can simulate selecting a piece or the entire text, but I can't simulate storing that into a variable.
The above code throws
'This command is not available because no document is open',
but hadn't I first activated this (the current) document, and then selected it (ActiveDocument.WholeStory)?
Why doesn't this work?
Later edit: I managed to do the selection like this:
Dim sText As String
Application.Selection.ClearFormatting
Application.Selection.WholeStory
sText = Application.Selection.Text
MsgBox sText
but the problem is I can't store the entire text (2 pages) in a variable. Part of it is truncated. Would you know how to store word by word (I only need a word at a time anyway)?
Later edit. I applied strReverse on the text to find out the text is actually stored entirely in the variable, just not fully displayed in the message box.
Don't use ThisDocument in code, unless you specifically want to address the file in which the code is stored and running. ThisDocument is the "code name" of that file.
Instead, use ActiveDocument to mean the document currently active in the Word window.
An addition, if you want the Selection in the currently active document, there's no reason to activate it - it's already active.
So to get the entire document content in a string
Dim mainData As String
mainData = ActiveDocument.Content.Text
where Content returns the entire main body's text as a Range object.
Note: The MsgBox has an upper character limit. If you're working with long text strings and want to see what they hold the following has more (but not "infinite") capacity:
Debug.Print mainData
All you need is:
Dim mainData As String
mainData = ActiveDocument.Range.Text

Issue with pdf docs not showing up

We recently wrote some code for a client using the Aspose.pdf library, on my system the pdf in question opened fine and most of the merge fields were filled in (we don't have the exact list of merge fields that they do).
They're telling me that on their system, some documents take 2-4 mins to open while others don't open at all.
What could be a possible cause of the document not opening at all?
My code is below:
' Load form
Dim doc As Aspose.Pdf.Document = New Aspose.Pdf.Document(sTemplateDir & sDocName)
'Get names of form fields
Dim fields As Aspose.Pdf.InteractiveFeatures.Forms.Field() = doc.Form.Fields
Dim sField As String
Dim field As Aspose.Pdf.InteractiveFeatures.Forms.Field
If fields.Length > 0 Then
For Each field In fields
'Get name of field
sField = field.FullName
'If the merge field isn't valid then we'll just leave it and assume its a fill-in
If nMergeCol.Contains(sField) And Not IsNothing(sField) Then
field.Value = nMergeCol.Item(sField)
End If
Next
End If
This has been resolved! As we suspected, it was a problem with the client's Javascript within the pdf file. The problem was within the calculations the absolute value was being used (name.value). Once this was switched to the relative value (this.event.value) the pdf file began behaving correctly with the AsPose code.

VB.NET - Working with tab delimited text file

I need help with how to work on text file (like database).
I create excel GUI (with macro's), that search imputed string in sheets with lots of data and display entire row with matching string (for people with installed MS office)
Now I must create alternative VB.Net application working only on tab delimited text files (without ADO.Net) for people who haven't installed MS office, and I don't know how start to work with it.
import them? if yes, then how.
working directly on them? if yes, then how.
My text files is exported excels files/sheets to tab delimited .txt, with loots of columns (100+) with headers, and lots of rows 500+
need help :)
thx
If you want to get the headers from the first line of the file then do this ...
Sub Main()
Dim dt = New DataTable
Dim lines = File.ReadAllLines("TextFile1.txt")
Dim headers = lines(0).Split(vbTab)
For Each header In headers
dt.Columns.Add(header)
Next
For Each line In lines.Skip(1)
Dim parts = line.Split(vbTab)
dt.Rows.Add(parts)
Next
End Sub

Creating cross-references in VBA but losing formatting on save

I'm creating a word document from scratch as an OLE object via VBA, and have created cross-references in it. I am actually using LotusScript, but the principles of VBA should apply.
Once I have created the cross-reference, I format the entire table cell that contains it (Arial 8 Italic), but when the document is saved, the field updates its format. I know that I can set a property of the field manually by ticking the "Preserve formatting during updates " option in the Word front-end, but is there a VBA property for that?
NB, The cross-reference is to a heading, and the formatting I'm getting appears to be the same as that heading, which is not what I want.
Many thanks,
Phil
I found the solution :-)
This is LotusScript, but I'm sure VB users can work out what it means. Also, I call a function and 3 subroutines, which are all self-explanatory. rg is a Range, in which I've just created the cross-reference. Due to the nature off the application, I know that the field is the first one in the range. The final line is the important one.
Set rg = getTableCell(subTable, 2, 1).Range
Dim fld As Variant
Set fld = rg.Fields(1)
Call SetItalicsOnOff(rg, True)
Call SetFontFace(rg, "Arial")
Call SetFontSize(rg, 8)
fld.Code.Text = fld.Code.Text & " \* MERGEFORMAT"

Foreign characters show up as ????? in VBA 2003, how to set up UTF-8?

I have a Database in Access 2003 that needs to work with foreign language characters. The characters show up fine in the table. However, when VBA tries to read them it cannot do it.
As an example, the column ANSWER_TEXT from cf_Answer displays: 佛吉尼亞海灘
But a select statement in VBA:
sqlstmt = "SELECT ANSWER_TEXT AS ans_text FROM cf_Answer"
Set rst_a = dbs.OpenRecordset(sqlstmt, dbOpenSnapshot)
rst_a![ans_text] returns ??????.
I know this has something to do with UTF-8 encoding but I cannot find a way to set it. Is there a way to set it in the table? Currently, ANSWER_TEXT is of datatype memo. Or perhaps there is a way to set VBA to understand those characters?
Can anyone at least point me in the right direction?
The issue may be due to where you're displaying that unicode text.
I pasted those same characters into a text field in my table. Retrieving them with DLookup in the Immediate window causes them to be displayed as question marks because the Immediate window does not display unicode as you wish.
? DLookup("some_text", "tblFoo", "id = 1")
??????
A MsgBox also displays them as question marks.
MsgBox DLookup("some_text", "tblFoo", "id = 1")
However a form text box control does have the capability to handle unicode properly. Binding the text box to the field which contains those characters gives me this ...
A query can also reference unicode characters, and this uses one in its WHERE clause and displays them all correctly when the query is opened in Datasheet View.
SELECT f.id, f.some_text
FROM tblFoo AS f
WHERE (((f.some_text) Like '佛*'));
I suspect this all comes down to how you're trying to use those unicode characters and where you're displaying them.
In a comment, you stated writing those unicode characters to a text file would produce only question marks. However, if you write unicode to a text file (as in the procedure below) and display the file in an editor which is capable of handling unicode correctly, you will see the same characters you see in Datasheet View of the table where they are stored. This screenshot shows Wordpad opened with the file which was created from the code below.
Dim objFso As Scripting.FileSystemObject
Dim objFile As Scripting.TextStream
Set objFso = New Scripting.FileSystemObject
Set objFile = objFso.OpenTextFile(CurrentProject.Path & _
Chr(92) & "unicode.txt", ForWriting, True, TristateTrue)
objFile.Write DLookup("some_text", "tblFoo", "id = 1")
objFile.Close
Set objFile = Nothing
Set objFso = Nothing