I'm drying too! Can you edit the macro, add the Execution and Local Variables windows (see image), then click just after Sub main() and press F8 just so that it bugs?
Basically, lacerate the macro step by step. And check in the local variable window the value of sModelName when it crashes:
I just redid tests by copying the base code and it doesn't work every time... I did 5 tests and I had 2 times where it works well, others where I don't have the PDF/DWG, or else it just doesn't launch!
I don't understand why it doesn't work every time!
Otherwise in theory I don't need to change the file path anymore
Hello; @Antoine_Ruelle , Taking a closer look at your MEP, it looks like you're not using the " Revision " property . Which will explain the error below:
Good evening Yes, it goes to the function to be able to retrieve the filename information by removing the extension. As it stands, the code offered by @sbadenis works on SW2022.
Hello @tous Antoine is looking at this macro in the meantime, Deletes lines 4 and 5 after " sub main ". And Assigns the exact name of the revision property instead of " hint "
Indeed, @Maclane for the revision part, I thought about it as well, hence the modification I had proposed with the removal of this property and the addition of folders. For properties it is indeed weird to have included " : " in the name of the property and this could indeed be a problem in some cases (macro or other). Example property without:
For @Antoine_Ruelle I confirm that the macro works well on SW2023 (so no reason on 2022) as @Cyril.f also says. You can also try @Lynkoa15's solution hoping that the problem doesn't appear this time.
Hello, thank you for this feedback @Maclane . Indeed I don't use the revision property I realized it after the fact, because for the saving in PDF/DWG I have the same file name as the SW plan. So I removed this part. And I still have the same problem, I don't have PDF/DWG
For the clue, he entered by hand in text in a property of the room which is shown on the plan. And no, we don't use PDM
sbadenis - For who is ": " I don't know if that's really the problem because it worked well 2 times in a row. This morning I did a test without the ":" and I got the message again that on line 118 I have a block that is not defined. Can you explain this line to me please? " Set swModel = swApp.ActivateDoc3(sModelName, True, swOpenDocOptions_Silent, lErrors) "
I just did a test again before answering you and it worked
@Lynkoa15 If I understood correctly for the folder path I have to determine them as variables Const dxfSubFolder As String = "\dwg" Const pdfSubFolder As String = "\pdf" Const stepSubFolder As String = "\step" To call them in the recording paths?
In any case thank you for your help, I'm starting to understand the VBA a little better
This line allows you to activate one of the open documents in silent mode (transparent to the user).
Yes, it simplifies the code and calls variables rather than entering the same text n times. And if you ever decide to change the path, it's easier to manage only one variable than to check all the code.
Okay so why in some cases the macro gets stuck on it? Is it possible that the file is already open? or that he does not find it?
And so at what level of the code should I specify the path variable to tell it to take the path of the file in question and add "/pdf" to have the right record folder? Or what part of the code from the other macro do I need to retrieve?
I just did a test again and it doesn't work, I don't understand anything anymore haha!! Here's the code
Option Explicit
Public Enum swDocumentTypes_e
swDocNONE = 0 ' Used to be TYPE_NONE
swDocPART = 1 ' Used to be TYPE_PART
swDocASSEMBLY = 2 ' Used to be TYPE_ASSEMBLY
swDocDRAWING = 3 ' Used to be TYPE_DRAWING
End Enum
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim swConfig As SldWorks.Configuration
Dim vSheetNameArr As Variant
Dim vSheetName As Variant
Dim I As Long
Dim nDocType As Long
Dim op As Long
Dim suppr As Long
Dim lErrors As Long
Dim lWarnings As Long
Dim boolstatus As Boolean
Dim bRet As Boolean
Dim FileConnu As Boolean
Dim nbConnu As Integer
Dim sModelName As String
Dim sPathName As String
Dim TabConnu(10000) As String
Dim sConfigName As String
Const dxfSubFolder As String = "\dwg"
Const pdfSubFolder As String = "\pdf"
Const stepSubFolder As String = "\step"
Sub Main()
Set swApp = Application.SldWorks
boolstatus = swApp.SetUserPreferenceIntegerValue(swStepAP, 214) 'Force la version AP214
boolstatus = swApp.SetUserPreferenceIntegerValue(swStepExportPreference, swAcisOutputGeometryPreference_e.swAcisOutputAsSolidAndSurface) 'Force l'export en format Solid/Surface Geometry
Set swModel = swApp.ActiveDoc
swModel.Extension.SaveAs GetFilename(swModel.GetPathName) & ".pdf", swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, lErrors, lWarnings 'Enregistrement en PDF
swModel.Extension.SaveAs GetFilename(swModel.GetPathName) & ".dwg", swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, lErrors, lWarnings 'Enregistrement en DWG
Call ExportStep ' Appelle le programe d'export STEP
End Sub
Function GetFilename(strPath As String) As String
Dim strTemp As String
strTemp = Mid$(strPath, InStrRev(strPath, "\") + 1)
GetFilename = Left$(strTemp, InStrRev(strTemp, ".") - 1)
End Function
Sub ExportStep() ' Vérification si c'est une piece ou un assemblage
Set swDraw = swModel
vSheetName = swDraw.GetSheetNames
vSheetNameArr = swDraw.GetSheetNames
For Each vSheetName In vSheetNameArr
bRet = swDraw.ActivateSheet(vSheetName): Debug.Assert bRet
Set swView = swDraw.GetFirstView 'Sélectionne le fond de plan
Set swView = swView.GetNextView 'Passe à la vue suivante pour exclure le fond de plan
While Not swView Is Nothing
' Determine si la vue est une pieces ou un assemblage
sModelName = swView.GetReferencedModelName
sModelName = LCase(sModelName)
sConfigName = swView.ReferencedConfiguration
FileConnu = False
If InStr(sModelName, "sldprt") > 0 Then
nDocType = swDocPART
ElseIf InStr(sModelName, "slasm") > 0 Then
nDocType = swDocASSEMBLY
Else
nDocType = swDocNONE
Exit Sub
End If
If nDocType = 1 Then
For I = 1 To nbConnu
If UCase(sModelName) & " - " & UCase(sConfigName) = TabConnu(I) Then
FileConnu = True
End If
Next
If Not FileConnu Then
nbConnu = nbConnu + 1
TabConnu(nbConnu) = UCase(sModelName) & " - " & UCase(sConfigName)
Call Export
End If
End If
Set swView = swView.GetNextView
Wend
Next vSheetName
End Sub
Sub Export()
Set swModel = swApp.ActivateDoc3(sModelName, True, swOpenDocOptions_Silent, lErrors)
Set swModel = swApp.ActiveDoc
boolstatus = swModel.ShowConfiguration2(sConfigName)
Set swConfig = swModel.GetActiveConfiguration
sPathName = swModel.GetPathName & ".step"
If Dir(sPathName, vbHidden) <> "" Then 'Test l'existence du fichier
suppr = MsgBox("Le fichier " & sPathName & " existe déjà, voulez vous le supprimer?", vbYesNo) 'Message utilisateur confirmation de suppression oui/non
If suppr = vbYes Then 'Réponse Oui
Kill (sPathName) 'Suppression du fichier existant
swModel.SaveAs2 sPathName, 0, True, False 'Enregistrement du fichier
op = MsgBox("Le fichier a été enregistré sous " & sPathName & vbNewLine)
Else 'Réponse NON
MsgBox ("Fichier conservé") 'Message utilisateur
End If
Else
swModel.SaveAs2 sPathName, 0, True, False 'Enregistrement du fichier
op = MsgBox("Le fichier a été enregistré sous " & sPathName) 'Message utilisateur
End If
swApp.CloseDoc (sModelName)
Set swModel = swApp.ActiveDoc
End Sub
Try to identify the files on which the macro does not work (especially if they are always the same):
Several avenues: The 3D and the layout are not in the same directory. 3D and blueprint don't have exactly the same name. One of the properties that the macro is looking for does not exist. The destination files (pdf/dwg and/or STEP) are already open or read-only... File and/or directory names have special characters (accents, periods,...)
Be careful also macros are sensitive to BREAKS... i.e. (to summarize) they differentiate between lowercase and uppercase) this is valid for filenames but also for extensions. …
the ideal is to browse your macro in step-by-step modes (F8), with the local variables window visible, and possibly hover over the variables in the lines of code with the mouse to check if they retrieve the data requested of them. (especially in "debug" node on breakpoints...
Subsidiary question, how do you start the macro execution? a button in solidworks or by the play key directly in the VBA editor? Do you always start macro with the "Main" routine?
Well, I looked but missing a lot of things to create the subfolders among others. @Antoine_Ruelle , you had tested the code proposed by @sbadenis (4th answer) which in my opinion was perfectly functional?
Yes and I had skipped a message but when I ask you if it works you answer me with the previous macro with the revisions. Take the latest version as indicated by @Cyril.f and @Maclane and it should work. On the other hand, not sure that the code is optimized, it is done in a hurry from that of @Cyril.f .
I have always tested on the same file. the drawing of a cap. The drawing is in a different folder than the parts, but the files have the same name.
I've already looked at the code by doing it step by step, and since I don't know too much about it I don't know how to correctly identify the problems.
To launch the macro I made a button and I also tried with the play key.
Yes for the subfolders for the moment I haven't done anything but pasted the 3 variables to be defined. Because I try to make the code work every time. I took the basic code without making any changes except to remove the Revision in the file name.
It worked once, I deleted the generated files and started again and there I had the error below
And no I haven't yet tested the code of the 4th answer @Cyril.f
Good news. All that remains is to validate the " Best answer " to close this discussion.
And see you soon for new macros... In the meantime, I advise you to consult different sites (not always in French -Visual Basic- obliges) but often didactic to get started in programming: