Loading in non-English text to AutoCAD via VBA?

Loading in non-English text to AutoCAD via VBA?

aaron.wrightCPM6Y
Contributor Contributor
2,454 Views
13 Replies
Message 1 of 14

Loading in non-English text to AutoCAD via VBA?

aaron.wrightCPM6Y
Contributor
Contributor

I'm scanning a CSV document which contains both english and russian text. If I copy the russian text directory into autoCAD, it shows up fine.

 

However, when reading from the CSV in VBA, the Russian text is inaccurate. If I try displaying the Russian via a Message Box for example, or add it in the drawing, it just shows up as random symbols. I imagine because of something to do with the type of encoding when reading the file in. My code currently looks like the below.

 

    Open FilePath For Input As #1
        Do Until EOF(1)
            Line Input #1, LineFromFile
            LineItems = Split(LineFromFile, ",")
            mykey = LCase(LineItems(0))
            myvalue = LineItems(1)
            translator_dict.add key:=mykey, Item:=myvalue
        Loop
    Close #1

 

I am just wondering if there's a way for me to load in the Russian correctly.

0 Likes
Accepted solutions (1)
2,455 Views
13 Replies
Replies (13)
Message 2 of 14

Ed__Jobe
Mentor
Mentor

That INPUT function is as old as DOS. It's not going to do what you want. I would read that file using Excel too.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 3 of 14

aaron.wrightCPM6Y
Contributor
Contributor

Thanks. I'll give this a try, although I did notice when opening the CSV in Excel, Excel was also showing the incorrect text.

0 Likes
Message 4 of 14

Ed__Jobe
Mentor
Mentor

Post a sample of the csv.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 5 of 14

aaron.wrightCPM6Y
Contributor
Contributor

Sure. I've just attached a small example.

0 Likes
Message 6 of 14

aaron.wrightCPM6Y
Contributor
Contributor

I managed to get it to open correctly in Excel just using the import functionality rather than opening the file directly in Excel.

 

However, still struggling to refactor my code so that it will display the Russian correctly in VBA. I feel like I need to open the CSV with UTF-8 encoding but can't find a solution for this.

0 Likes
Message 7 of 14

Ed__Jobe
Mentor
Mentor
Accepted solution

Try this for an idea.

Public Function GetXL() As Application
    On Error Resume Next
    Dim xlApp As Application
    Set xlApp = GetObject(, "Excel.Application")
    If xlApp Is Nothing Then
        Set xlApp = CreateObject("Excel.Application")
    End If
    Set GetXL = xlApp
End Function

Sub RussianTest()
    Dim xl As Application
    Set xl = GetXL()
    Dim wb As Workbook
    Dim strText As String
    Dim iPt(0 To 2) As Double
    iPt(0) = 1
    iPt(1) = 1
    iPt(2) = 0
    
    Set wb = xl.Workbooks.Open("C:\path\test-russian.xlsx")
    strText = wb.Sheets("Sheet1").Cells(1, 2).Value()
    ThisDrawing.PaperSpace.AddText strText, iPt, 1#
    
    Set wb = Nothing
    Set xl = Nothing
    
End Sub

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 8 of 14

aaron.wrightCPM6Y
Contributor
Contributor

Perfect thank you! I had to refactor a bit, but this looks to be doing what I want it to do.

0 Likes
Message 9 of 14

aaron.wrightCPM6Y
Contributor
Contributor

EDIT: Tried XL.Quit but it closes all instances of Excel

 

One last thing, I've tried closing the Excel file once the script is finished. However, after running the script I get an error that the file is being used by another program. To fix it, I need to close Excel via Task Manager. Is there anything obviously wrong with my function below? I can submit a new question for this.

 

I would assume that the Set XL = Nothing at the bottom would have sorted it.

 

 

 

 

Public Function AddToDictionary(FilePath As String, SheetName As String)

    On Error Resume Next
    
    ' Create Excel Object
    Dim XL As Excel.Application
    Set XL = GetObject(, "Excel.Application")
    If XL Is Nothing Then
        Set XL = CreateObject("Excel.Application")
    End If
    
    Dim wb As Workbook
    Dim i As Long
    Set wb = XL.Workbooks.Open(FilePath)
    Set mysheet = wb.Sheets(SheetName)
    
    ' Create Dictionary
    Set translator_dict = CreateObject("Scripting.Dictionary")
    
    ' Number of rows in first column of sheet
    NumRows = mysheet.Range("A2", mysheet.Range("A2").End(xlDown)).Rows.Count

    ' Append sheet data to dictionary
    For i = 1 To NumRows + 1
        If Not IsEmpty(mysheet.Cells(i, 1).Value) Then
            translator_dict.add key:=LCase(mysheet.Cells(i, 1).Value()), Item:=mysheet.Cells(i, 2).Value()
        End If
    Next i
    
    Set mysheet = Nothing
    Set wb = Nothing
    Set XL = Nothing
    
    Set AddToDictionary = translator_dict
    
End Function

 

 

 

 

0 Likes
Message 10 of 14

Ed__Jobe
Mentor
Mentor


 

Public Function AddToDictionary(FilePath As String, SheetName As String)

    On Error Resume Next
    
    ' Create Excel Object
    Dim XL As Excel.Application
    Set XL = GetObject(, "Excel.Application")
    If XL Is Nothing Then
        Set XL = CreateObject("Excel.Application")
    End If
    
    Dim wb As Workbook
    Dim i As Long
    Set wb = XL.Workbooks.Open(FilePath)
    Set mysheet = wb.Sheets(SheetName)
    
    ' Create Dictionary
    Set translator_dict = CreateObject("Scripting.Dictionary")
    
    ' Number of rows in first column of sheet
    NumRows = mysheet.Range("A2", mysheet.Range("A2").End(xlDown)).Rows.Count

    ' Append sheet data to dictionary
    For i = 1 To NumRows + 1
        If Not IsEmpty(mysheet.Cells(i, 1).Value) Then
            translator_dict.add key:=LCase(mysheet.Cells(i, 1).Value()), Item:=mysheet.Cells(i, 2).Value()
        End If
    Next i
    
    Set mysheet = Nothing
    wb.Close          'Added
    Set wb = Nothing
    xl.Quit           'Added
    Set XL = Nothing
    
    Set AddToDictionary = translator_dict
    
End Function

 

 

 

 


See my changes above. If you add the line to quit xl, you can add logic to check the Workbooks collection first. If there are still workbooks open, the Count will be greater than 0. So if the Count is 0, you can Quit xl.

 

Also, I highly recommend that you leave the xl object creation in its own sub. A lot of people assume that On Error Resume Next only applies to the next line. But it stays in effect during the scope of the sub. IOW, it will suppress all errors in your sub unless you reset it. Search VBA help for "On Error Statement"  topic on how to do that. The preferred method is to containerize your code. Keep tasks separate. Besides, there may come a time when you want to use that function for another sub.

 

Also, you didn't Dim the NumRows variable. VBA will handle it for you at runtime, but it will always be cast as a Variant. If you include Option Explicit statement at the top of your module, the IDE will remind you that you didn't dim a var. If you go Tools>Options>Editor tab and check "Require Variable Declaration", it will add the Option Explicit statement for you when you create a new module.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 11 of 14

aaron.wrightCPM6Y
Contributor
Contributor

Perfect thank you. I shall take a look.

 

I'm not really a VBA programmer, so I've never quite understood why 'Dim' and 'Set' are used to declare variables or objects. If something is of type variant, is does it just take up more memory?

 

And thanks for mentioning the Option Explicit statement idea. I will add that in - as well split XL creation into a different sub.

0 Likes
Message 12 of 14

Ed__Jobe
Mentor
Mentor

Dim dimensions a variable, i.e. allocates memory...and yes, a Variant takes up the most memory. Set does not declare a variable, it assigns to the memory location created by Dim.

 

A variable is a memory location. Think of it as a bucket for storing things. There are different types of memory locations. Some intrinsic data types like single, double and string don't need special handling. But object types of variables do and so require the Set statement.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 13 of 14

aaron.wrightCPM6Y
Contributor
Contributor

Thank you. That makes more sense now.

 

I'll also try split my code up so the XL creation is separate. However most of what I try results in numerous errors which I'm finding difficult to resolve. For what I've done below, does it make sense to try split this function into two separate functions?

 

Public Function AddToDictionary(FilePath As String, SheetName As String)

    On Error Resume Next
    
    ' Create Excel Object
    Dim XL As Excel.Application
    Set XL = GetObject(, "Excel.Application")
    If XL Is Nothing Then
        Set XL = CreateObject("Excel.Application")
    End If
    
    Dim wb As Workbook
    Dim i As Long
    Set wb = XL.Workbooks.Open(FilePath)
    Set mysheet = wb.Sheets(SheetName)
    
    ' Create Dictionary
    Set translator_dict = CreateObject("Scripting.Dictionary")
    
    ' Number of rows in first column of sheet
    NumRows = mysheet.Range("A2", mysheet.Range("A2").End(xlDown)).Rows.Count

    ' Append sheet data to dictionary
    For i = 1 To NumRows + 1
        If Not IsEmpty(mysheet.Cells(i, 1).Value) Then
            translator_dict.add key:=LCase(mysheet.Cells(i, 1).Value()), Item:=mysheet.Cells(i, 2).Value()
        End If
    Next i
    
    wb.Close
    Set mysheet = Nothing
    Set wb = Nothing
    Set XL = Nothing
    
    Set AddToDictionary = translator_dict
    
End Function

 

0 Likes
Message 14 of 14

aaron.wrightCPM6Y
Contributor
Contributor
Forget this. I found my mistake, and have now divided it up into separate functions.
0 Likes