ds-blue-logo
Preview  |  SOLIDWORKS USER FORUM
Use your SOLIDWORKS ID or 3DEXPERIENCE ID to log in.
LMLucca Mario15/08/2021

Hi guys, how are you?
I need help with my macro!
I want to read a file text with a of list files, and open then.
I'm not an expert in API, maybe someone can help me.
Thanks for helping

This is my code.


'-----------------------------------------------------------------------------------------------------------------------------------
It's txt file content:
-> user - 24/11/2019 - 14:39:48 - Rev: 00 ; C:\Desenhos\EXERCICIOS\TESTE_2.SLDDRW
-> user - 24/11/2019 - 14:39:48 - Rev: 00 ; C:\Desenhos\EXERCICIOS\TESTE_4.SLDDRW
'-----------------------------------------------------------------------------------------------------------------------------------
it's vba code:

Public myArray() As String
Public NomeArray As String
 
Sub main()

ReadTextCf "C:\Users\UserLocal\Desktop\Lista.txt"

End Sub

Sub OpenFile(File As String)

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swDocSpecification As SldWorks.DocumentSpecification
Dim longstatus As Long, longwarnings As Long
Dim sName As String

myArray = Split(File, ";")
Debug.Print "--***" & File
Debug.Print myArray(1)

For i = 0 To UBound(myArray)
Debug.Print "[" & i & "] " & myArray(i)

Set swApp = Application.SldWorks
Set swDocSpecification = swApp.GetOpenDocSpec(Chr(34) & myArray(i) & Chr(34))
sName = swDocSpecification.FileName
swDocSpecification.DocumentType = swDocDRAWING
swDocSpecification.ReadOnly = True
swDocSpecification.Silent = False
Set swModel = swApp.OpenDoc7(swDocSpecification)
longstatus = swDocSpecification.Error
longwarnings = swDocSpecification.Warning
Next i

End Sub

Function ReadTextCf(filePath As String) As String
Dim str As String
Dim fileNo As Integer
fileNo = FreeFile
Dim content As String
Dim isFirstLine As Integer
isFirstLine = True
 
Open filePath For Input As #fileNo
Do While Not EOF(fileNo)
Dim line As String
Line Input #fileNo, line
content = content & IIf(Not isFirstLine, vbLf, "") & line
isFirstLine = False
'line = UCase(line)

If InStr(line, "c:\") Or InStr(line, "C:\") Then
myArray = Split(line, ";")
str = myArray(1)
NomeDraw = Mid(str, InStrRev(str, "\") + 1) 'get file name
Path = (Left(str, InStrRev(str, "\") - 1)) 'get CurDir name
 
If Not str = Empty Then
If NomeArry = Empty Then
NomeArry = str
Else
If Not (InStr(NomeArry, str)) > 0 Then
NomeArry = NomeArry & ";" & str
End If
End If

Debug.Print " *Path " & Path
 
End If
End If
Loop
 
Close #fileNo
ReadText = content
 
Debug.Print "drawToOpen " & " - " & NomeArry
OpenFile (NomeArry)
 
End Function