Macro PDF export in bulk

Hi all

I'm looking for a macro that would allow me to bulk export multiple Drafts to PDFs.

I should like to point out right away that I only have the basic version at my desk so I don't have the possibility to schedule this type of tasks.

Thank you

You can apparently do this with the Task Sheduler:

https://www.youtube.com/watch?v=WgjajdK6CEc

 

Otherwise with specific tools like Cad+ (not tested):

https://cadplus.xarial.com/

 

For my part, I use the MyCADservices utilities and more particularly Integration or BatchConverters (very quickly amortized for recurring needs):

Integration: https://help.visiativ.com/mycadtools/2020/fr/Integration.html

BatchConverter: https://help.visiativ.com/mycadtools/2020/fr/BatchConverter.html

 

 

1 Like

Hello;

Otherwise I propose this macro.
Opens all *.slddrw files in a directory and saves them under the same name as a PDF.

Option Explicit

Private Const BIF_RETURNONLYFSDIRS As Long = &H1
Private Const BIF_DONTGOBELOWDOMAIN As Long = &H2
Private Const BIF_RETURNFSANCESTORS As Long = &H8
Private Const BIF_BROWSEFORCOMPUTER As Long = &H1000
Private Const BIF_BROWSEFORPRINTER As Long = &H2000
Private Const BIF_BROWSEINCLUDEFILES As Long = &H4000
Private Const MAX_PATH As Long = 260

Function BrowseFolder(Optional Caption As String, Optional InitialFolder As String) As String

Dim SH As Shell32.Shell
Dim F As Shell32.Folder

Set SH = New Shell32.Shell
Set F = SH.BrowseforFolder(0&, Caption, BIF_RETURNONLYFSDIRS, InitialFolder)
If Not F Is Nothing Then
    If F = "Desktop" Then
        BrowseFolder = Environ("USERPROFILE") & "\Desktop"
    Else
        BrowseFolder = F.Items.Item.Path
    End If
End If

End Function

Sub Main()
Dim swApp        As SldWorks.SldWorks
Dim swModel      As SldWorks.ModelDoc
Dim sFileName    As String
Dim Path         As String
Dim nErrors      As Long
Dim nWarnings    As Long
Dim swDraw       As SldWorks.DrawingDoc
Dim PartNoDes    As String
Dim swExportPDFData As SldWorks.ExportPdfData


    Set swApp = Application.SldWorks
    Set swExportPDFData = swApp.GetExportFileData(1)
    swExportPDFData.ViewPdfAfterSaving = False
    
    Path = BrowseFolder("Selectionner le Repertoire à Traiter.")
    If Path = "" Then
        MsgBox "Erreur, Selectionnez le repertoire à nouveau."
        End
    Else
    Path = Path + "\"
    End If
         
    sFileName = Dir(Path & "*.slddrw")
    Do Until sFileName = ""
        Set swModel = swApp.OpenDoc6(Path + sFileName, swDocDRAWING, swOpenDocOptions_Silent, "", nErrors, nWarnings)
        Set swModel = swApp.ActiveDoc
        Set swDraw = swApp.ActiveDoc
            PartNoDes = Mid(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\") + 1)
            PartNoDes = Left(PartNoDes, Len(PartNoDes) - 7)
            swDraw.SaveAs3 Path & PartNoDes & ".PDF", 0, 0
        swApp.QuitDoc swDraw.GetPathName
        Set swDraw = Nothing
        Set swModel = Nothing
        sFileName = Dir
    Loop

MsgBox "Le Traitement est terminé."
End Sub

 

Kind regards.

2 Likes

Thank you

How should this macro be executed? Do you have to open all the drafts beforehand?

Hello;

Well no, you asked for a Batch treatment....
The easiest way is to create a button/macro directly in Solidworks.
See: https://www.lynkoa.com/forum/2d/creer-un-bouton-macro


When it is launched, my macro will ask you to select the directory containing the Drawings to be processed, then open them one by one to save them in pdf.
 

Kind regards.

OK

So when I run the macro I get a message that tells me:

Compilation error:

A user-defined type not defined. 

Hello;

Could you give me even less information? (Bebug = On which line does the program stop?)

I suspect you didn't load the Necessary References in the interface:
From the Macro Editor, go to Option and click on References...
Search the list: (And check the boxes)
- Visual Basic For Application
- Solidworks 20xx Exensibility Type Library (20xx=your version of Solidworks, if several choose the most recent)
- Ole Automation
- sldWorks 20xx Type Library (20xx=your version of Solidworks, if several choose the most recent)
- SOLIDWORKS 20xx Constant type library (20xx=your version of Solidworks, if several choose the most recent)
- SOLIDWORKS 20xx Comands type library (20xx=your version of Solidworks, if several choose the most recent)
- Microsoft Exel 16.0 Object Library
- Microsoft Shell, Microsoft Shell controls and Application.

then confirm your selection. (I am deliberately making you load more Booksellers than necessary for this macro, but they will be useful to you one day or another)
Relaunch the Macro.

Note: The next time you request a Macro, please specify the version (year) of your Solidworks as well as your Macro level....

Kind regards.
 

1 Like

Sorry I'm a novice in Macro so it's not easy to clearly explain where it screams.

I'll try the trick and I'll keep you posted.

Thank you

Sorry for this late answer but here is the name of the line where the macro stop:

Function BrowseFolder(Optional Caption As String, Optional InitialFolder As String) As String

And I specify that everything is well checked.

Hello

For Shell functions, you also need the reference "Microsoft Shell controls and Application" otherwise the compilation of the code is in error.

I think the problem comes from there (checked on my post).

1 Like

Hello;

Indeed, this reference is necessary, thank you Cyril.f for this correction.
I'm correcting the list on my previous post.

Kind regards.