0

Im trying to generate an Excel BOM data base without using CATIA. For this porpouse I did an Excel subroutine that reads a CATPart file as a txt file. This way I can manage to get the parameters inside the CATPart file using a simple InStr function.

The problem starts when I try to get the Part Number string (CATIA native, not a created parameter). When I open a CATPart as a txt file I can see that the Part Number is allways on the side of a string "ASMPRODUCT".

It's a quite simple code. But when I try to get the string "ASMPRODUCT" using the InStr function, it does NOT work. AND THE WORD IS THERE WHEN YOU OPEN THE TXT FILE!! I've checked using InStrRev also but I don't have a result either.

Did someone already battled with this problem? Is there a solution?

Dim fsDataFile As Object
Dim sText As Object
Dim sPath As String
Dim line As String

Set fsDataFile = CreateObject("Scripting.FileSystemObject")

line_n = 0
'The fully qualified path to your lookup file
sPath = "some CATPart file"

'Create a text stream from your FileSystemObject
Set sText = fsDataFile.OpenTextFile(sPath, 1, False)

'Loop through each line in the text stream
Done = False
Do While sText.AtEndOfStream <> True
    line_n = line_n + 1
    line = sText.ReadLine
    'Some times the file contains the word more than once, the 
    'boolean variable Done avoid this problem.
    If InStr(1, line, "ASMPRODUCT", 1) <> 0 And Done = False Then
        Debug.Print line_n & " | PN: " & Mid(line, InStr(1, line,"ASMPRODUCT") + 11, 16)
        Done = True
    End If
Loop

sText.Close
Seba Sain
  • 1
  • 3
  • please show us your code so we can help – Aldert Aug 05 '22 at 18:57
  • instead of trying to read your word, read each line and create an output. you will know what comes of it... I dont undestand why are you talking about instr and instrrev. Output your lignes, check if your Line.contains("ASMPRODUCT") = true – Disvoys Aug 07 '22 at 15:12
  • The code is really simple. I just use CreateObject("Scripting.FileSystemObject") and then I open the ".CATPart" file as a ".txt" file using another object fsDataFile.OpenTextFile(sPath, 1). After that I just iterate reading all lines and searching a word that I´m interested. (Eg. In the CATPart I created previously a "Technology" parameter with a string value "SheetMetal"). The code can extract this parameters without opening CATIA. – Seba Sain Aug 08 '22 at 16:11
  • So.. I don't have problems reading my parameters. The problems starts when I try to read the Part Number parameter already created by CATIA. To be completely honest I already solved the problem reading the CATPart file with excel instead of the notepad. It's a very time consuming proccess but it works. – Seba Sain Aug 08 '22 at 16:17
  • I uploaded the code. But I reapeat, the code works in every case. The only word it can not find is "ASMPRODUCT" and the word is there when you open the CATPart file using the notepad. – Seba Sain Aug 08 '22 at 16:37
  • Your script does not read a line. Try changing `line = line + 1` into `line = sText.ReadLine` – Shrotter Aug 08 '22 at 17:47
  • I'm sorry, it was a mistake translating and transcripting the code to this webpage. I've already edited the post. – Seba Sain Aug 08 '22 at 17:58
  • Well i confirm i got the same issue. I got the good Line with "if Line like "*ASMPRODUCT*" then msgbox(Line). But returns a special caracter.. – Disvoys Aug 09 '22 at 09:58
  • Ok, getting the line is already a result. I will continue from there to see if I can move forward with this method and finally get the string I want. Thanks a lot. – Seba Sain Aug 09 '22 at 17:46
  • Perhaps those characters are in a wide character set. Or perhaps there are things between the letters un-shown by notepad. Do an experiment: Edit the file, delete everything before and after the word, save the result as a new file, and read it character by character and see what it is there. – C R Johnson Aug 09 '22 at 19:35
  • I suggest to use a text editor which shows also control characters (e.g. Notepad++) to analyze the file. ASMPRODUCT is followed by Chr(6) and the the partnumber is followed by Chr(11) – Shrotter Aug 10 '22 at 06:15
  • @CRJohnson For my experience, you cannot edit the file by text or you will not be able to read the CATPart file anymore. – Disvoys Aug 10 '22 at 07:09
  • @C R Johnson , If you mean manually, of course it will work. Programatically I'm not quite sure how to do the "delete part" you mention. I mean, I think if I would be able to delete it, I would not have a problem to read it (so the problem would not exist). – Seba Sain Aug 10 '22 at 07:57
  • @Shrotter This text editor you name.. do you have to install it previously? Because the main problem is exactly that. I cant ask the people using my macro to install any program at all. They are people from logistics, schedule programing, etc.. they are smart enough to open an Excel file, and that's all. Just kidding, but I can't anicipate who is going to use the macro, and what kind of knowledge (or permits) he/she has. – Seba Sain Aug 10 '22 at 08:04
  • @SebaSain This text editor is only necessary for you during writing the macro (helps you to analyze the file and develop your macro). – Shrotter Aug 10 '22 at 08:30

0 Answers0