Pdf Export makro

Hi,

I have made a macro which creates PDF`s of my drawings.

Our MPS system can only handle A3 as maximum sheet size,

We are using A1 A2 formats on larger drawing`s.

Is there a way to scale down the output format some how to A3?

Regards

SolidworksApi macros
Answers 7
AB
AB 2017-09-20 Best Answer

The only way I can think to do it is to use a macro to print the drawing and then select a PDF Converter as the printer. The page size can be specified before printing to match your A3 size requirement.

Edit: Also this -

MB
MB 2017-09-21

Here is a quick macro I wrote up to allow you to print to pdf with A3 size paper. It will scale the drawing sheets to fit. You will need to modify the line where it selects the paper size. The best way to get this number is to start recording a macro, then go to File>Print, change the printer to your pdf printer, >Page Setup, change the paper size to A3 click OK on page setup and close on the print window and stop recording the macro.

Open the macro you created and change the line to match.

<p><span style="font-family: Consolas;">Sub main()<br>Dim swApp                   As SldWorks.SldWorks<br>Dim swModel                 As SldWorks.ModelDoc2<br>Dim swDraw                  As SldWorks.DrawingDoc<br>Dim swModelDocExt           As SldWorks.ModelDocExtension<br>Dim swPrintSpec             As SldWorks.PrintSpecification<br>Dim swPageSetup             As SldWorks.PageSetup</span></p><p><span style="font-family: Consolas;">Dim Printer                 As String</span></p><p><span style="font-family: Consolas;">Dim sheets(0)               As Long</span></p><p><span style="font-family: Consolas;">Set swApp = Application.SldWorks<br>Set swModel = swApp.ActiveDoc<br>Set swDraw = swModel<br>Set swModelDocExt = swModel.Extension<br>Set swPrintSpec = swModelDocExt.GetPrintSpecification<br>Set swPageSetup = swModel.PageSetup</span></p><p><span style="font-family: Consolas;">swPrintSpec.RestoreDefaults<br>swPrintSpec.ResetPrintRange<br>swPageSetup.HighQuality = False<br>swPrintSpec.ScaleMethod = 3<br>swPageSetup.DrawingColor = 1<br>swPageSetup.HighQuality = False<br>swPrintSpec.ScaleMethod = swPrintAll</span></p><p><span style="font-family: Consolas;">Printer = "Adobe PDF"       'If you use a different PDF printer set the name here</span></p><p><span style="font-family: Consolas;">sheets(0) = 0      'Print all sheets</span></p><p><span style="font-family: Consolas;">swPageSetup.ScaleToFit = True</span></p><p><span style="font-family: Consolas;">If MsgBox("Do you want to print A3?", vbYesNo) = vbYes Then<br>    swPageSetup.PrinterPaperSize = 8       'Record a macro for your papersize and change the "8" to whatever the number is you get<br>    swModelDocExt.PrintOut3 sheets, 1, True, Printer, "", False<br>End If</span></p><p><span style="font-family: Consolas;">swPrintSpec.RestoreDefaults<br>swPrintSpec.ResetPrintRange</span></p><p><span style="font-family: Consolas;">End Sub</span></p>
comment imagecomment imagecomment imagecomment imagecomment imagecomment image
JM
JM 2017-09-22

Hi, Thanks.

This works partly, At work we are using PDFCreator, an this keep asking for the Directory to save the file to each time .

The title is named

In my current macro, it pulls the   & from the drawing file and puts it out in the same folder as the drawing is located.

Is this possible to achieve even this is an external app?

MP
MP 2017-09-22

Jan,

You may want to reconsider the use of PDFCreator:

PDFCreator - Wikipedia​

Scroll down and read about the malware problems with it.  I used to use it, but I stopped when they started adding in all the malware.

MB
MB 2017-09-23

Sadly I don't think that it works properly. I have tried to get it to work with both .printout3 and .printout4 and have been 100% unsuccessful. I did contact my Var in the past about this and they said it has been an issue for some time but there has yet to be a fix for it. I will continue to test and see what I can do to make this functionality work for you.

MB
MB 2017-10-26

So quick update. I put in a ticket with my VAR and they told me that this is part of SPR 834841. So until Solidworks sorts the issue out there is currently no way that I know of to print to PDF with a file name.

MB
MB 2017-10-26

​

So I have come up with a bit of a work around. The user will still have to save the pdf to a directory, but they will save it to C:\Temp, or whatever folder you want. At this point the macro will then print the pdf to the temp folder, then take the model custom properties for Revision and PartNo and rename and move the pdf to the Drawing Doc folder and delete the temp pdf. It will also overwrite an existing pdf with the same name.

If you have any questions please let me know.

I have attached the .swp along with posting the code itself. I hope this helps!

Edit: forgot to mention, this will close any open windows of Adobe Acrobat. I did this because when I print using adobe it opens a new instance of Acrobat and it that is still open when I try to rename the file it will not succeed.

<p>Sub main()<br>Dim acrobatID<br>Dim swApp                   As SldWorks.SldWorks<br>Dim swModel                 As SldWorks.ModelDoc2<br>Dim swDraw                  As SldWorks.DrawingDoc<br>Dim swSheet                 As SldWorks.Sheet<br>Dim swView                  As SldWorks.View<br>Dim swModelDocExt           As SldWorks.ModelDocExtension<br>Dim swPrintSpec             As SldWorks.PrintSpecification<br>Dim swPageSetup             As SldWorks.PageSetup<br>Dim Printer                 As String<br>Dim fName                   As String<br>Dim pName                   As String<br>Dim nPath                   As String<br>Dim rev                     As String<br>Dim sheets(0)               As Long<br>Dim index                   As Integer<br>Dim time1, time2</p><p>Const Path                  As String = "C:\Temp\"</p><p>Set swApp = Application.SldWorks<br>Set swModel = swApp.ActiveDoc<br>Set swDraw = swModel<br>Set swSheet = swDraw.GetCurrentSheet<br>Set swView = swDraw.GetFirstView<br>Set swModelDocExt = swModel.Extension<br>Set swPrintSpec = swModelDocExt.GetPrintSpecification<br>Set swPageSetup = swModel.PageSetup</p><p>nPath = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") - 1)<br>If Right(nPath, 1) <> "\" Then nPath = nPath & "\"</p><p>If swSheet.CustomPropertyView = "Default" Or swSheet.CustomPropertyView = "" Then<br>    Set swView = swView.GetNextView<br>Else<br>    Do While swView.Name = swSheet.CustomPropertyView<br>        Set swView = swView.GetNextView<br>    Loop<br>End If</p><p>Set swModel = swView.ReferencedDocument</p><p>pName = swModel.Extension.CustomPropertyManager("").Get("PartNo")<br>rev = swModel.Extension.CustomPropertyManager("").Get("Revision")</p><p>swPrintSpec.RestoreDefaults<br>swPrintSpec.ResetPrintRange<br>swPageSetup.HighQuality = False<br>swPrintSpec.ScaleMethod = 3<br>swPageSetup.DrawingColor = 1<br>swPageSetup.HighQuality = False<br>swPrintSpec.ScaleMethod = swPrintAll</p><p>Printer = "Adobe PDF"<br>sheets(0) = 0<br>swPageSetup.ScaleToFit = True<br>If MsgBox("Do you want to print A3?", vbYesNo) = vbYes Then<br>    swPageSetup.PrinterPaperSize = 8<br>    swModelDocExt.PrintOut3 sheets, 1, True, Printer, "", False<br>End If<br>swPrintSpec.RestoreDefaults<br>swPrintSpec.ResetPrintRange</p><p>time1 = Now<br>time2 = Now + TimeValue("0:00:02")<br>Do Until time1 >= time2<br>    time1 = Now()<br>Loop</p><p>TerminateProcess</p><p>fName = NewestFile(Path, "*.pdf")</p><p>If Len(Dir$(nPath & pName & "_" & rev & ".pdf")) > 0 Then<br>     Kill nPath & pName & "_" & rev & ".pdf"<br>End If</p><p>Name Path & fName As nPath & pName & "_" & rev & ".pdf"</p><p>If Len(Dir$(Path & fName)) > 0 Then<br>     Kill Path & fName<br>End If</p><p>End Sub</p><p>Sub TerminateProcess()<br>  Dim strTerminateThis As String<br>  Dim objWMIcimv2 As Object<br>  Dim objProcess As Object<br>  Dim objList As Object<br>  Dim intError As Integer</p><p>  'Process to terminate<br>  strTerminateThis = "Acrobat.exe"</p><p>  'Connect to CIMV2 Namespace<br>  Set objWMIcimv2 = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")</p><p>  'Find the process to terminate<br>  Set objList = objWMIcimv2.ExecQuery("select * from win32_process where name='" & strTerminateThis & "'")</p><p>  'Terminates a process and all of its threads.<br>  For Each objProcess In objList<br>      intError = objProcess.Terminate<br>  Next</p><p>  Set objWMIcimv2 = Nothing<br>  Set objList = Nothing<br>  Set objProcess = Nothing<br>End Sub</p><p>Function NewestFile(Directory, FileSpec)<br>' Returns the name of the most recent file in a Directory<br>' That matches the FileSpec (e.g., "*.xls").<br>' Returns an empty string if the directory does not exist or<br>' it contains no matching files<br>    Dim FileName As String<br>    Dim MostRecentFile As String<br>    Dim MostRecentDate As Date<br>    If Right(Directory, 1) <> "\" Then Directory = Directory & "\"</p><p>    FileName = Dir(Directory & FileSpec, 0)<br>    If FileName <> "" Then<br>        MostRecentFile = FileName<br>        MostRecentDate = FileDateTime(Directory & FileName)<br>        Do While FileName <> ""<br>            If FileDateTime(Directory & FileName) > MostRecentDate Then<br>                 MostRecentFile = FileName<br>                 MostRecentDate = FileDateTime(Directory & FileName)<br>             End If<br>             FileName = Dir<br>        Loop<br>    End If<br>    NewestFile = MostRecentFile<br>End Function</p>