Macro PDF Conversion Problem

Hello

I tried to use the .PL macro (http://www.lynkoa.com/tutos/solidworks-creer-tous-les-pdf-pour-un-dossier-0) which allows you to convert all the plans in a folder to PDF but I have a problem when I launch it.

I get the message "Compilation error: Sub or Function not defined" on the line Path = BrowseFolder("Select a Path/Folder")

I searched a little on the forum and I saw that other people had had the same problem but I couldn't find a solution.

Anyone have an idea?

Thanks in advance:)

 

 

Hello

First of all, I want to say like @max59 in the previous post

 Why not use the Task Scheduler.

http://www.lynkoa.com/forum/solidworks/macro-de-conversion-de-toutes-les-pi%C3%A8ces-dun-assemblage-au-format-step-ap214

may the force be with you.

 

2 Likes

Hello Obi Wan,

I have a standard solidworks version and in this version, the scheduler doesn't convert plans to PDFs.

 

Hello

Unless I make a mistake, the macro is missing a piece. BrowseFolder is a subfunction so in my opinion it's missing hence the error message.

That must be it indeed.

Being a bit (very) bad at macro, does anyone already have this subfunction?

I saw that it worked with some members of the forum

 

See this page

http://www.lynkoa.com/forum/import-export-formats-neutres/macro-export-pdf-dwg

@+ ;-)

1 Like

Hello

Adds "Microsoft Shell Controls And Automation" to tools/references.

Kind regards

2 Likes

Well done d.roger. I had a doubt and could not find the reference.

Thank you for your answers.

@d.roger, I still get the same error after adding "Microsoft Shell Controls And Automation". I compared and I have the same references as on your screenshot enabled.

 

Hello

I just changed the way to open the folder selection window. Not tested the overall ocmbehavior.

Option Explicit
  
Dim swApp        As SldWorks.SldWorks
Dim swModel      As SldWorks.ModelDoc
Dim sFileName    As String
Dim vFileName    As String
Dim Path         As String
Dim nPath        As String
Dim nErrors      As Long
Dim nWarnings    As Long
Dim swDraw       As SldWorks.DrawingDoc
Dim swCustProp   As CustomPropertyManager
Dim swView       As SldWorks.View
Dim ConfigName   As String
Dim i            As Long
Dim valOut1      As String
Dim valOut2      As String
Dim resolvedValOut1 As String
Dim resolvedValOut2 As String
Dim PartNo      As String
Dim nFileName      As String
Dim swDocs As Variant
Dim PDFpath As String
Dim currpath As String
Dim PartNoDes As String
Dim objShell   As Shell
Dim objFolder  As Folder

    
Sub main()
    Set swApp = Application.SldWorks
    Dim swExportPDFData     As SldWorks.ExportPdfData
    Set swExportPDFData = swApp.GetExportFileData(1)
    swExportPDFData.ViewPdfAfterSaving = False
    
    Set objShell = New Shell
        Set objFolder = objShell.BrowseForFolder(0, "Example", 0, 0)
            If (Not objFolder Is Nothing) Then
                Path = objFolder.Self.Path
                Path = Path + "\"
                PDFpath = Path & "PDF"
                
                If Dir(PDFpath, vbDirectory) = "" Then MkDir PDFpath
                    sFileName = Dir(Path & "*.slddrw")
      
                    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
                        Set swView = swDraw.GetFirstView
                        Set swView = swView.GetNextView
                        Set swModel = swView.ReferencedDocument
  
  

                            PDFpath = currpath & "PDF"
      
                            If Dir(PDFpath, vbDirectory) = "" Then MkDir PDFpath
       
            
                                If swModel.GetType = swDocPART Then
                                    PartNoDes = Mid(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\") + 1)
                                    PartNoDes = Right(PartNoDes, Len(PartNoDes) - 14)
                                    PartNoDes = Left(PartNoDes, Len(PartNoDes) - 7)
                                    PartNo = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)
                                    PartNo = Left(PartNo, Len(PartNo) - 7)
                                    Set swCustProp = swModel.Extension.CustomPropertyManager(swView.ReferencedConfiguration)
                                    ConfigName = swView.ReferencedConfiguration
                                    swCustProp.Get2 "Description", valOut1, resolvedValOut1
                                    swCustProp.Get2 "Revision", valOut2, resolvedValOut2
                                    nFileName = PDFpath & "\" & PartNo & "-" & ConfigName & "-" & resolvedValOut2 & " " & PartNoDes
                                    swDraw.SaveAs3 nFileName & ".PDF", 0, 0
                      
                                ElseIf swModel.GetType = swDocASSEMBLY Then
                                    PartNoDes = Mid(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\") + 1)
                                    PartNoDes = Right(PartNoDes, Len(PartNoDes) - 11)
                                    PartNoDes = Left(PartNoDes, Len(PartNoDes) - 7)
                                    PartNo = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)
                                    PartNo = Left(PartNo, Len(PartNo) - 7)
                                    Set swCustProp = swModel.Extension.CustomPropertyManager("")
                                    swCustProp.Get2 "Description", valOut1, resolvedValOut1
                                    swCustProp.Get2 "Revision", valOut2, resolvedValOut2
                                    nFileName = PDFpath & "\" & PartNo & "-" & resolvedValOut2 & " " & PartNoDes
                                    swDraw.SaveAs3 nFileName & ".PDF", 0, 0
                   
                                End If
      
                                swApp.QuitDoc swDraw.GetPathName
                                Set swDraw = Nothing
       
                                Set swModel = Nothing
       
                            sFileName = Dir
     
                        Loop

                End If
                    
        Set objFolder = Nothing
    Set objShell = Nothing
  
MsgBox "All Done"
  
  
End Sub

 

Thank you Cyril.f :)

Now I can select my folder but I have another problem in the rest of the macro on the line: Set swModel = swView.ReferencedDocument

Runtime Error 91: Object or Block Variable Not Defined

 

 

I don't know why but after relaunching solidworks, this line passed.

However, I have a new problem on the line: 

PartNodes = Left(PartNoDes, Len(PartNoDes) - 7)

"Incorrect argument or procedural appeal"

 

 

 

Hello

Wouldn't your file extensions be hidden in your windows explorer display options?

Kind regards

Indeed they were hidden but I still have the error

Even after a Solidworks reboot?

Yes I restarted PC + Solidworks just in case

I just tried saving the plans from another folder.

It works but:

- it converts only one plan

- It creates a PDF folder in the right location but saves the plan in another place

 

I managed to get the file to save in the right place by adding Path & to the lines defining the nFileName variable

On the other hand, the macro still doesn't convert the other planes in the folder

Here's the macro I get after making a few other edits because the name of the PDF didn't match what I wanted

 

 


pdf.zip

Re

I'll watch this if I have time in the evening otherwise it will be in January unless d.roger has time to look.

Hello

Let's see if the attached version works the way you want.

Kind regards


savealldrwaspdf.swp
1 Like