I have a small problem when trying to change the path of the export file in a macro:
The lines to be amended are as follows:
Set FileList = fso.CreateTextFile("C:\Temp\Exclu.txt", 8, -2)
Shell "notepad.exe ""C:\Temp\Exclu.txt""", vbNormalFocus
I would like to retrieve the path of my assembly instead of C:\Temp
Here is my non-functional code for base:
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set Assembly = swApp.ActiveDoc
Set myAsy = Assembly
'**********Chemin d'export MEP**********
'*******Récup chemin existant***********
sOutputFolder = Left(swModel.GetPathName(), Len(swModel.GetPathName()) - 7)
sOutputFile = sOutputFolder & "\Exclu.txt"
Debug.Print sOutputFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Set FileList = fso.CreateTextFile("C:\Temp\Exclu.txt", 8, -2)
Set FileList = fso.CreateTextFile(sOutputFile, 8, -2)
myCmps = myAsy.GetComponents(False)
For i = 0 To UBound(myCmps)
Set myCmp = myCmps(i)
If myCmp.ExcludeFromBOM Then
FileList.Write myCmp.Name2 & vbCrLf
End If
Next i
FileList.Close
Shell "notepad.exe ""C:\Temp\Exclu.txt""", vbNormalFocus
End Sub
If anyone has an idea, I've been drying for an hour for something probably very stupid...
I was the one who fired the creation of the file named "exclu.txt", it seemed weird to me but it's because I had misinterpreted the question a bit.
So, yes it doesn't want to create the "exclu.txt" file where you want because the folder doesn't exist, so you have to create it before if it doesn't exist:
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set Assembly = swApp.ActiveDoc
Set myAsy = Assembly
'**********Chemin d'export MEP**********
'*******Récup chemin existant***********
soutputFolder = Left(swModel.GetPathName(), Len(swModel.GetPathName()) - 7)
soutputfile = soutputFolder & "\Exclu.txt"
Debug.Print soutputfile
If Dir(soutputFolder) = "" Then
MkDir soutputFolder
End If
Set fso = CreateObject("Scripting.FileSystemObject")
'Set FileList = fso.CreateTextFile("C:\Temp\Exclu.txt", 8, -2)
Set FileList = fso.CreateTextFile(soutputfile, 8, -2)
myCmps = myAsy.GetComponents(False)
For i = 0 To UBound(myCmps)
Set myCmp = myCmps(i)
If myCmp.ExcludeFromBOM Then
FileList.Write myCmp.Name2 & vbCrLf
End If
Next i
FileList.Close
'Shell "notepad.exe ""C:\Temp\Exclu.txt""", vbNormalFocus
Shell "notepad.exe """ & soutputfile & """", vbNormalFocus
End Sub
Thank you d.roger your solution which works perfectly, allowed me to find my mistake.
However, I just wanted to retrieve the path to the assembly folder and in this folder save my Exclu.txt file.
It was the creation of the folder that put the flea in my ear since the folder of my assembly is necessarily already existing!
Thanks anyway for the code that brings me other knowledge. And I still choose your answer as the best because you answered my request, which was just not clear at all.
Sub main()
Dim swModel As SldWorks.ModelDoc2
Dim swPart As SldWorks.PartDoc
Dim bRet As Boolean
Dim MyPath As String
Dim MyFolder As String
Set swApp = Application.SldWorks
Set Assembly = swApp.ActiveDoc
Set myAsy = Assembly
Set swModel = swApp.ActiveDoc
'**********Chemin d'export MEP**********
'*******Récup chemin existant***********
MyFolder = CurDir$
Debug.Print "Current Folder = " & MyFolder
soutputfile = MyFolder & "\Exclu.txt"
Debug.Print soutputfile
Set fso = CreateObject("Scripting.FileSystemObject")
'On créer le fichier .txt
Set FileList = fso.CreateTextFile(soutputfile, 8, -2)
myCmps = myAsy.GetComponents(False)
For i = 0 To UBound(myCmps)
Set myCmp = myCmps(i)
If myCmp.ExcludeFromBOM Then
FileList.Write myCmp.Name2 & vbCrLf
End If
Next i
FileList.Close
'On ouvre le fichier avec le bloc note
Shell "notepad.exe """ & soutputfile & """", vbNormalFocus
End Sub