Announcements

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

Problems with special characters in vba variable

Anonymous

Problems with special characters in vba variable

Anonymous
Not applicable

Dear ACAD and VBA Pros,

 

This is my first entry in this forum, looking forward for your help. 

 I am using AUTOCAD 2020.

 My problem is the following:

I would like to integrate a barcode in my drawing (.dwg). I choose to build a block with an attribute and give that one my barcode font. Works out so far.

The next step is to integrate the value of this attribute from a specific .TXT file (Barcode.txt), located in my project folder. Within this .txt file I got one line which should be my attribute value. I managed that.

 

The problem is, that this .txt file contains some special charcters like “ËÌ!!#”, as it is a calculated value to shorten the length of the barcode. As soon as I load those characters into my VBA variable, they change to some completely other characters. So the content I set my attribute is wrong, and therefore the barcode is too.

Exampel: .txt content"Ë#!!4(*Ì" changes to "Ë#!!4(*ÃŒ" in vba and autocad.

It works if I manually copy-paste my .txt file content into my attribute.

Anyone have some experience with this?

 

I have never used LISP, maybe someone has a solution for my problem using LISP. 

 

Here is what I got so far. 

 

Sub Change_it()

''GET BARCODE CONTENT
Dim dokpath, barpath, filecont As String

Dim pathpos As Long
Dim iFile As Integer: iFile = FreeFile
Dim oData As New DataObject

dokpath = Application.ActiveDocument.Path

pathpos = InStr(1, dokpath, "MyDrawingfolderWithinTheProjectfolder")

barpath = Left(dokpath, pathpos - 1) & "Barcode.txt" ''file in my Projectfolder
    
Open barpath For Input As #1
    
Do Until EOF(1)
    
    Line Input #1, filecont

Loop

Close #1



''FILL ATRIBUTE WITH CONTENT

Dim obj As AcadBlockReference
   Dim inspt As Variant

   ThisDrawing.Utility.GetEntity obj, inspt, "Select object:"

   ' Checks if you selected a block.
   If obj.ObjectName = "AcDbBlockReference" Then

       ' Check for attributes.
       If obj.HasAttributes Then

           Dim AttList As Variant

           ' Build a list of attributes for the current block.
           AttList = obj.GetAttributes

           ' Cycle throught the list of attributes.
           For i = LBound(AttList) To UBound(AttList)

               ' Check for the correct attribute tag.
               If AttList(i).TagString = "BARCODE" Then
                    AttList(i).TextString = filecont

               End If

           Next

       End If



   Else
       MsgBox "You did not select a block."
   End If
End Sub

 

0 Likes
Reply
Accepted solutions (1)
1,779 Views
3 Replies
Replies (3)

ed57gmc
Mentor
Mentor
Accepted solution

The problem is with the Line Input statement. It's as old as DOS and can't handle unicode. I found this thread with some sample code in it. You could also try storing your text in a spreadsheet and using COM to get the value. Search this forum for samples on working with Excel.

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

Anonymous
Not applicable

Thanks for your answer. I used an .xlsx and the excel-reference. Worked out!

0 Likes

ed57gmc
Mentor
Mentor

Glad you got it worked out.

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