Macro save as PDF only the first page of my drawing

Hi all

I created a macro that allows me to create a PDF of my drawings with a prefix and a second PDF with a prefix followed by "ADV",

My problem is the following on the second PDF I would like it to be only the first page of my layout, because when saving as you can tell the desired pages or not but in VBA I don't know how to do it.

Currently I use Acrobat that I open from my macro to delete the last two

Below is my macro:

*****************************************************************************

Sub main()

Dim SwApp As Object
    Dim Part As Object
    Dim SelMgr As Object
    Dim selObj As Object
    Dim AcroApp As Acrobat.AcroApp
    Set SwApp = CreateObject("SldWorks.Application")

Set Part = SwApp.ActiveDoc
Dim Clue As String
index = InputBox("index?")
Dim myModelView As Object
Set myModelView = Part.ActiveView
Dim target As Scripting.filesystemobject
Dim value As Scripting.file
Target Set = CreateObject("scripting.filesystemobject")
Set value = target.getfile(Part.GetPathName)
longstatus = Part.SaveAs3("U:\PDF to serve\" & target. GetBaseName(value) & "-" & index & ".pdf", 0, 0)
If MsgBox("ADV", vbYesNo, "Do I need a PDF for ADV?") = vbYes Then
longstatus = Part.SaveAs3("U:\PDF to serve\" & target. GetBaseName(value) & "-" & index & "-ADV" & ".pdf", 0, 0)
Shell ("C:\Program Files (x86)\Adobe\Acrobat 7.0\Acrobat\Acrobat.exe U:\PDF to be distributed\" & target. GetBaseName(value) & "-" & index & "-ADV" & ".pdf"), vbMaximizedFocus

End If

  MsgBox ("Finish")

End Sub

*****************************************************************************

Thank you in advance for your answers

Julian.

 

 

Hello

 

Personally I can't help you, but people like .PL or Joss.G and there must be others, will certainly be able to help you.

 

Mathieu

1 Like

Hello

Can you make a macro in learning the recording function with only the 1st page to see how they translate it into VBA?

 

S.B

I've already tried to use the Macro recorder to get it to give me the line of code but it doesn't work

Julian.

 

Hello

In this case, I don't think you can use the Shell, you have to use the SolidWorks function to create the PDFs, see  :

http://help.solidworks.com/2014/English/api/sldworksapi/Save_File_as_PDF_Example_VB.htm

 

Example:

Set swExportPDFData = swApp.GetExportFileData(swExportDataFileType_e.swExportPDFData)

Choose your sheet here

varSheetName = strSheetName

boolstatus = swExportPDFData.SetSheets(swExportData_ExportSpecifiedSheets, varSheetName)

   

boolstatus = swModelDocExt.SaveAs(filename, 0, 0, swExportPDFData, lErrors, lWarnings)

 

 

Source: https://forum.solidworks.com/thread/66559

1 Like

It's nice to quote Mathieu ANGER but I don't know macros well, I admit that I spent a little time on them from time to time but from there to help write lines of code... ^^

I've found ready-made macros for those who are looking for them but I'm not as good as .PL. (I think Jmsavoyat knows quite a bit about the subject too).

Cdt

Joss

1 Like

Thank you for your solution .PL ^^ it works great;) I had to adapt it a little but I'm happy with the result,

Thank you again and have a good day,

 

Julian.

1 Like

You're welcome^^

If you can post the code so that we have the exact syntax (especially to choose the first sheet only), it can be useful to others!

Have a good day to you.

Here is the complete macro that allows you to have first a PDF with all the pages and a "subscript" prefix in its name and secondly to have only the first page with "indice" prefix and other predefined "ADV" 

it's up to you to modify it as you see fit 

Have a good day everyone 

Julian.

 

************************************************************************************************************************

Sub main()

Dim SwApp As Object
    Dim Part As Object
    Dim SelMgr As Object
    Dim selObj As Object
    Dim swExportPDFData As SldWorks.ExportPdfData
    Dim boolstatus As Boolean
    Dim lErrors As Long
    Dim lWarnings As Long
    Dim strSheetName(3) As String
    Dim varSheetName As Variant
    Dim filename As String
    Dim swModelDocExt As SldWorks.ModelDocExtension

           
    Set SwApp = CreateObject("SldWorks.Application")

Set Part = SwApp.ActiveDoc
If Part Is Nothing Then
        MsgBox "No current document", vbCritical
        End
End If
Dim Clue As String
index = InputBox("index?")
Dim myModelView As Object
Set myModelView = Part.ActiveView
Dim target As Scripting.filesystemobject
Dim value As Scripting.file
Target Set = CreateObject("scripting.filesystemobject")
Set value = target.getfile(Part.GetPathName)
longstatus = Part.SaveAs3("C:\path to PDF destination" & target. GetBaseName(value) & "-" & index & ".pdf", 0, 0)
If MsgBox("ADV", vbYesNo, "Do I need a PDF for ADV?") = vbYes Then

filename = "C:\path to PDF destination" & target. GetBaseName(value) & "-" & index & "-ADV" & ".pdf"

Set swModelDocExt = Part.Extension
Set swExportPDFData = SwApp.GetExportFileData(1)

strSheetName(0) = "BOM"

varSheetName = strSheetName


boolstatus = swExportPDFData.SetSheets(swExportData_ExportSpecifiedSheets, varSheetName)
    
boolstatus = swModelDocExt.SaveAs(filename, 0, 0, swExportPDFData, lErrors, lWarnings)

   
End If
   MsgBox ("Finish")
End Sub

*************************************************************************************************************************