Macro recording as pdf active sheet

Hi all

I currently have a macro that allows me to save my .slddrw document in pdf under the same directory and the same name.

So far so good, the problem is that it saves all the sheets of my drawing and I want it to save me only the active sheet when I launch my macro.

I'm sure the solution is simple but nothing to do I can't find the right code, here is the macro I got from the forum and that I use:

'**************************************************************************************************************************
'* Example of a macro that allows you to save documents as PDFs
'* You can Change the extension to save the document in any format supported by SW
'* Based on the example made by Axemble "Saveas_pdf"
'* Edit by MCD
'**************************************************************************************************************************
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swmodel As SldWorks.ModelDoc2
    Dim stPath As String
    Dim lgFile As Long
    Dim blretval as Boolean
    Dim Errors As Long
    Dim Warnings As Long
    
    Set swApp = Application.SldWorks
    'We get the active document
    Set swmodel = swApp.ActiveDoc
    
    If Not Swmodel Is Nothing Then
       'We check that the file is registered
        If swmodel. GetPathName = "" Then
            MsgBox "Please save your document before launching the macro", vbInformation
            End
        Else
            'we get the location of the file
            stPath = swmodel. GetPathName
            'we get the number of characters up to . of the extension
            lgFile = InStrRev(stPath, ".", -1, vbTextCompare) - 1
            'we recover the path without the extension
            If lgFile > 0 Then
                  stPath = Left(stPath, lgFile)
            End If
        End If
        
        'If the document is a document
        If swmodel. GetType = swDocPART Then
           We create the developed
            'blretval = swmodel. ExportFlatPatternView(stPath & ". DXF", 1)
            The DXF was created
            'blretval = swmodel. SaveAs3(stPath & ". DXF", 0, 0)
             MsgBox "This is a Piéce file. Open the drawing to make the PDF", vbInformation
            
            'If the document is a drawing
        ElseIf swmodel. GetType = swDocDRAWING Then
            The DXF was created
            'blretval = swmodel. SaveAs3(stPath & "_drw.pdf", 0, 0)
            Create the PDF
            blretval = swmodel. SaveAs3(stPath & ".pdf", 0, 0)
            
            
        End If
    
        'we save the file
        blretval = swmodel. Save3(0, 0, 0)
   
    End If

End Sub

 

Hello

You use the SaveAs3 method, by going to the help page you can see that this method is obsolete:

http://help.solidworks.com/2012/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IModelDoc2~SaveAs3.html

And that you should use SaveAs instead:

http://help.solidworks.com/2012/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IModelDocExtension~SaveAs.html

With this last method, you can specify which sheet you want to save, see this example:

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

For details on choosing sheets:

http://help.solidworks.com/2012/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IExportPdfData.html

 

4 Likes

It's starting to be complicated for me, can you try to modify my macro  STP?

You have all the information to do it.

Otherwise, as you have myCADtools, you can use one of the tools to do it, it may be easier for you: Integration or Batch Converter will be able to do it.

3 Likes

I quite agree with .PL, all the work is pre-;). Now if VBA seems complex to you and you don't want to mess with it (which is completely understandable, there is no value judgment!), small ready-made software is very good.

 

However, I would like to tone down @PL's remarks a little. I'm sure that if you try things, ask for explanations on obscure points, etc., he or another person here will be able to guide you. But giving things already ready-made... Isn't that really the philosophy of the forum:)

1 Like

Far be it from me to be haughty, but I don't have SolidWorks at hand, so I can test a macro blindly when I have time, which is not the case today!

Good evening

Try this one:

 

Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim lErrors As Long
Dim lWarnings As Long

Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
swModel.Extension.SaveAs GetFilename(swModel.GetPathName) & " rev." & swModel.GetCustomInfoValue("", "Revision") & ".pdf", swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, lErrors, lWarnings

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

Thank you for your answers, sorry Manu67, nothing happens when launching the macro.

If it doesn't work, it's because you're on 2015... It worked for me in 2014 but not in 2015 and works again in 2016. I can't explain it. Try switching to 2016 SP2 or wait some time for SP3 if you can do so.