47 Replies Latest reply on Dec 14, 2017 9:11 AM by Solid Works

    Save As PDF in Different Folder Using Macro

    Salman Faiz

      Hi,

      I have a following code which is not allowing me to save file to PDF. I was wondering if someone can help me with it.

      This code, checks if a folder exists and if not it creates the folder, add revision number to the file name. I will appreciate if someone can help me in getting the save as pdf part correct.

       

      Option Explicit

      Dim swApp           As SldWorks.SldWorks

      Dim swModel         As SldWorks.ModelDoc2

      Dim swDraw          As SldWorks.ModelDoc

      Dim Filepath        As String

      Dim FileName        As String

      Dim PDFFileName     As String

      Dim InterPath       As String

      Dim PDFPath         As String

      Dim oFolder         As String

      Dim Revision        As String

      Dim boolstatus      As Boolean

      Dim PDFPathandName  As String

       

      Sub main()

      Set swApp = Application.SldWorks

      'Set swModel = swApp.ActiveDoc

      Set swDraw = swModel

      Set swDraw = swApp.ActiveDoc

       

      'Check to see if a drawing is loaded.

      If (swDraw Is Nothing) Or (swDraw.GetType <> swDocDRAWING) Then

      swApp.SendMsgToUser ("To be used for drawings only, Open a drawing first and then TRY!")

      'If no model currently loaded, then exit

      Exit Sub

      End If

       

      Filepath = swDraw.GetPathName

      InterPath = Left(Filepath, Len(Filepath) - 31)

      MsgBox (InterPath)

       

      PDFPath = InterPath & "01 Released Drawings" & "\PDFs"

      MsgBox (PDFPath)

       

      Revision = swDraw.CustomInfo("Revision")

      FileName = swDraw.GetPathName

      PDFFileName = Left(FileName, Len(FileName) - 7) & "-R" & Revision & ".pdf"

      MsgBox (PDFFileName)

       

      oFolder = PDFPath

      If Dir(oFolder, vbDirectory) = vbNullString Then

      MkDir (oFolder)

      MsgBox "Folder Created"

      End If

       

      PDFPathandName = PDFPath & "/" & PDFFileName

       

      swDraw.SaveAs (PDFPathandName & ".PDF")

       

      End Sub

        • Re: Save As PDF in Different Folder Using Macro
          Deepak Gupta

          Replace this line

          PDFFileName = Left(FileName, Len(FileName) - 7) & "-R" & Revision & ".pdf"

          with these lines

          PDFFileName = Mid(FileName, InStrRev(FileName, "\") + 1)
          PDFFileName = Left(PDFFileName , InStrRev(PDFFileName , ".")  - 1)
          • Re: Save As PDF in Different Folder Using Macro
            Robert H.

            i know its a a little late but i think this should do everything you need

             

             

            Dim swApp           As SldWorks.SldWorks

            Dim swModel         As SldWorks.ModelDoc2

            Dim swDraw          As SldWorks.DrawingDoc

            Dim Filepath        As String

            Dim FileName        As String

            Dim Drawingno       As String

            Dim Rev             As String

            Dim Compileddir     As String

            Dim Compileddir2     As String

            Dim Title           As String

            Dim strFolder       As String

             

             

            Dim boolstatus As Boolean

            Dim longstatus As Long, longwarnings As Long

            Sub main()

             

             

            Set swApp = Application.SldWorks

            Set swModel = swApp.ActiveDoc

             

             

            ' Check to see if a drawing is loaded.

             

             

            If (swModel Is Nothing) Or (swModel.GetType <> swDocDRAWING) Then

             

             

            swApp.SendMsgToUser ("To be used for drawings only, Open a drawing first and then TRY!")

             

             

            ' If no model currently loaded, then exit

             

             

            Exit Sub

             

             

            End If

             

            Set swDraw = swModel

            Set SWmoddoc = swApp.ActiveDoc

             

             

            Filepath = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "CAD FILES") - 1)

             

             

            If Dir(Filepath & "PDF", vbDirectory) = "" Then ' Change Sub folder Name here

            MkDir Filepath & "PDF" ' Change Sub folder Name here

            End If

             

             

            If Dir(Filepath & "DWG", vbDirectory) = "" Then ' Change Sub folder Name here

            MkDir Filepath & "DWG" ' Change Sub folder Name here

            End If

             

             

             

             

            ' Set DWG/DXF save options - to MultiSheetOption

             

             

             

             

            boolstatus = swApp.SetUserPreferenceIntegerValue(swUserPreferenceIntegerValue_e.swDxfMultiSheetOption, swDxfMultisheet_e.swDxfMultiSheet)

            boolstatus = swApp.SetUserPreferenceIntegerValue(swUserPreferenceIntegerValue_e.swDxfMultiSheetOption, 2)

             

             

            ' Set file name variables

             

             

            Drawingno = SWmoddoc.CustomInfo("Drawing No.")

             

             

            Rev = SWmoddoc.CustomInfo("version")

             

             

            Title = SWmoddoc.CustomInfo("Title")

             

             

            saveconfig = (Drawingno & "_#" & Rev & "_" & Title)

             

             

            Compileddir = (Filepath & "PDF\" & Drawingno & "_#" & Rev & "_" & Title)

            Compileddir2 = (Filepath & "DWG\" & Drawingno & "_#" & Rev & "_" & Title)

             

             

            Dim fso As New Scripting.FileSystemObject

             

            If fso.FileExists(Compileddir & ".PDF") Then

            If fso.FileExists(Compileddir2 & ".DWG") Then

               

               

            Dim Answer As String

            Dim MyNote As String

             

                'Place your text here

                MyNote = "Do you wish to overwrite the drawing " & vbCrLf & Drawingno & "_#" & Rev & "_" & Title

                'Display MessageBox

                Answer = MsgBox(MyNote, vbQuestion + vbYesNo + vbDefaultButton2, "File Already Exists")

             

                If Answer = vbNo Then

                    'Code for No button Press

                Else

                    'Code for Yes button Press

                    swDraw.SaveAs (Compileddir & ".PDF")

                    swDraw.SaveAs (Compileddir2 & ".DWG")

                End If

             

             

             

               

            End If

                End

            End If

             

             

            swDraw.SaveAs (Compileddir & ".PDF")

            swDraw.SaveAs (Compileddir2 & ".DWG")

             

             

             

             

            End Sub

            • Re: Save As PDF in Different Folder Using Macro
              Christian Chu

              Look at this thread, which you can select a folder to save your pdf file

                • Re: Save As PDF in Different Folder Using Macro
                  Solid Works

                  Just tried. When I run Deepak's attached macro it just closes the open drawing, doesn't do anything else. 

                   

                  When I copy paste your post into a macro and run it I get this message

                  SWMacro file has wrong format and cannot be converted into VBA macro file

                    • Re: Save As PDF in Different Folder Using Macro
                      Christian Chu

                      Since this is a windows API, so you need to add a sub, say "WinAPI" and add these lines

                      Option Explicit
                      
                      
                      Public Const SCS_32BIT_BINARY = 0
                      Public Const SCS_64BIT_BINARY = 6
                      
                      
                      Global Const SWP_NOMOVE As LongPtr = 2
                      Global Const SWP_NOSIZE As LongPtr = 1
                      Global Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
                      Global Const HWND_TOPMOST As LongPtr = -1
                      Global Const HWND_NOTOPMOST As LongPtr = -2
                      
                      
                      Public Declare PtrSafe Function GetBinaryType Lib "kernel32" Alias "GetBinaryTypeA" (ByVal sFileName As String, ByRef BinType As LongPtr) As LongPtr
                      
                      
                      Declare PtrSafe Function SetWindowPos Lib "user32" _
                      (ByVal hwnd As LongPtr, ByVal hWndInsertAfter As LongPtr, ByVal x As LongPtr, _
                      ByVal y As LongPtr, ByVal cx As LongPtr, ByVal cy As LongPtr, ByVal wFlags As LongPtr) As LongPtr
                      
                      
                      Public Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
                      (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
                      

                      Then in user form, add a command button, say "cmdBrowser" and add this line

                      Path =  GetFolder("Select a destination folder or create a new one:")

                      where Pah is a string which you can add PDF file name at path end

                        • Re: Save As PDF in Different Folder Using Macro
                          Solid Works

                          I'm new to macros. Can you please tell me where to add WinAPI?

                            • Re: Save As PDF in Different Folder Using Macro
                              Christian Chu

                              Save it in the modules where your main module is located

                                • Re: Save As PDF in Different Folder Using Macro
                                  Solid Works

                                  I still can't get it to work.  Can you please take a look at the macros?

                                    • Re: Save As PDF in Different Folder Using Macro
                                      Christian Chu

                                      Please see the att. macro

                                      You need to add a userform so the user can interact with the program by selecting a folder

                                        • Re: Save As PDF in Different Folder Using Macro
                                          Solid Works

                                          Is there no way to have the macro just access the standard Save As dialog box?  I don't know how to do userforms.

                                            • Re: Save As PDF in Different Folder Using Macro
                                              Christian Chu

                                              I already added a userform to the macro.

                                                • Re: Save As PDF in Different Folder Using Macro
                                                  Solid Works

                                                  Just tried it again. Nothing happens after I select the folder

                                                   

                                                  I run the macro. Click Browser and select the location. Then click OK

                                                   

                                                  I get a dialog box saying You Select Folder.....

                                                   

                                                  I click OK again and then nothing happens.

                                                    • Re: Save As PDF in Different Folder Using Macro
                                                      Christian Chu

                                                      Exactly what I wanted to show you , selecting a folder

                                                      Now add your SaveAsPDF code to the macro and use the path you get from the browser in the line

                                                      swModel.SaveAs (Path & "\" & PDFFileName)

                                                        • Re: Save As PDF in Different Folder Using Macro
                                                          Solid Works

                                                          Please explain where to add the code. Tried add the code in this link to the end of your Macro1 and it didn't work

                                                            • Re: Save As PDF in Different Folder Using Macro
                                                              Christian Chu

                                                              Put your code in the Browser command where you see the msgbox and change this line

                                                              PDFPath = GetFolder("Select a destination folder or create a new one:")
                                                              

                                                               

                                                              I just copied the coded from above and pasted in the macro I sent you

                                                               

                                                              Dim swApp           As SldWorks.SldWorks
                                                              Dim swModel         As SldWorks.ModelDoc2
                                                              Dim swDraw          As SldWorks.ModelDoc
                                                              Dim Filepath        As String
                                                              Dim FileName        As String
                                                              Dim PDFFileName     As String
                                                              Dim InterPath       As String
                                                              Dim PDFPath         As String
                                                              Dim oFolder         As String
                                                              Dim Revision        As String
                                                              Dim boolstatus      As Boolean
                                                              Dim PDFPathandName  As String
                                                              Private Sub CommandButton1_Click()
                                                              
                                                              
                                                              Set swApp = Application.SldWorks
                                                              'Set swModel = swApp.ActiveDoc
                                                              Set swDraw = swModel
                                                              Set swDraw = swApp.ActiveDoc
                                                              
                                                              'Check to see if a drawing is loaded.
                                                              If (swDraw Is Nothing) Or (swDraw.GetType <> swDocDRAWING) Then
                                                              swApp.SendMsgToUser ("To be used for drawings only, Open a drawing first and then TRY!")
                                                              'If no model currently loaded, then exit
                                                              Exit Sub
                                                              End If
                                                              
                                                              Filepath = swDraw.GetPathName
                                                              InterPath = Left(Filepath, Len(Filepath) - 31)
                                                              MsgBox (InterPath)
                                                              
                                                              PDFPath = InterPath & "01 Released Drawings" & "\PDFs"
                                                              MsgBox (PDFPath)
                                                              
                                                              Revision = swDraw.CustomInfo("Revision")
                                                              FileName = swDraw.GetPathName
                                                              PDFFileName = Mid(FileName, InStrRev(FileName, "\") + 1)
                                                              PDFFileName = Left(PDFFileName, InStrRev(PDFFileName, ".") - 1)
                                                              MsgBox (PDFFileName)
                                                              
                                                              oFolder = PDFPath
                                                              If Dir(oFolder, vbDirectory) = vbNullString Then
                                                              MkDir (oFolder)
                                                              MsgBox "Folder Created"
                                                              End If
                                                              
                                                              
                                                              PDFPath = GetFolder("Select a destination folder or create a new one:")  '*** I ONLY CHANGED THIS LINE ***
                                                              
                                                              PDFPathandName = PDFPath & "/" & PDFFileName
                                                              
                                                              swDraw.SaveAs (PDFPathandName & ".PDF")
                                                              
                                                              
                                                              
                                                              
                                                              End Sub
                                                              
                                                              • Re: Save As PDF in Different Folder Using Macro
                                                                Deepak Gupta

                                                                Will you be able to do a quick screen share as I feel that something very simple is not letting you use any of these macro.

                                                                 

                                                                I can do a Team Viewer session to resolve the issue right away if you want.

                                                                  • Re: Save As PDF in Different Folder Using Macro
                                                                    Robert H.

                                                                    i wouldn't bother Deepak Gupta, i have offered to help to write one for him, which he hasn't responded to ! , i believe he is just wasting everyone time

                                                                      • Re: Save As PDF in Different Folder Using Macro
                                                                        Deepak Gupta

                                                                        Robert, could be possible that he has no knowledge on how to set up or use the macro and hence not able to follow up on all suggestions. I've seen similar cases where a quick screen sharing session resolved the issue. So based on that I offered him if he is willing to do that.

                                                                          • Re: Save As PDF in Different Folder Using Macro
                                                                            Robert H.

                                                                            i have emailed him my self yesterday offering to write something (its been a slow week !) - it your time Deepak Gupta , maybe your right ,  seam like hes wasting everyone time, rather that making a case for what he wants to do

                                                                              • Re: Save As PDF in Different Folder Using Macro
                                                                                Solid Works

                                                                                Robert,  thank you but but I don't see any PMs in my forum account

                                                                                  • Re: Save As PDF in Different Folder Using Macro
                                                                                    Robert H.

                                                                                    - PM message bellow

                                                                                     

                                                                                    MONDAY, 11 DECEMBER 2017 - 20:16 - UK  GMT

                                                                                     

                                                                                    Robert H.

                                                                                    just tell me what you want to do with this macro. and tell me how your filing system works and, i look will look into writing something tomorrow.

                                                                                    or even better send me a zip file  with an example of you filing system , and where you want files to go, then we can actually help, rather running around the api forum like a headless chicken 

                                                                                     

                                                                                      • Re: Save As PDF in Different Folder Using Macro
                                                                                        Solid Works

                                                                                        Robert,

                                                                                         

                                                                                        Most of our model/drawings files are most are stored across a few servers.

                                                                                         

                                                                                        Our PDFs are also saved across a few but different servers depending on purpose.  The PDFs are NOT saved to the same folder as the model/drawing

                                                                                         

                                                                                        What we'd like to do is

                                                                                        - Open a drawing

                                                                                        - Run a macro

                                                                                        - The macro saves the PDF to a user specified location and remembers this location until the user changes it.

                                                                                         

                                                                                        The reason we're not using Save As is Save As forgets the PDF folder location as soon as you open a new model/drawing.

                                                                                         

                                                                                        Once again apologies for not getting the PM.  Please understand its definitely not intentional.

                                                                                         

                                                                                        Thank you all again

                                                                                          • Re: Save As PDF in Different Folder Using Macro
                                                                                            Robert H.

                                                                                            see thats a lot more helpfully and now we understand what you are trying to do a bit better

                                                                                             

                                                                                            where do you save them ? mine aren't saved in the same folder as the cad/drawing files but in a parent folder the dwgs go into one folder and the pdfs into a second folder, my macro actually looks for the CAD FILES in the url and then move to the parent directory of CAD FILES and creates the folder PDF AND DWG if they dont create they and save the files into the correct folder,  along with generating the naming convention e.g how the file should be named.

                                                                                             

                                                                                            i am assuming that your filing system is the same on all the server or have similar structure, e.g.  they are not saved in some random  place every time ?,  - if so you wouldn't need to even enter the location, you could have the marco, determine the save location from the document properties and its current location e.g what server it was on with a bit of logic (but i dont think it that will be the case )

                                                                                             

                                                                                            if could you send a a few examples of you filing system

                                                                                             

                                                                                            and when you say servers to you mean network drives ?

                                                                                             

                                                                                            again without more information on what your setup is like, i can't offer any meaningful  advice .

                                                                                             

                                                                                            if you are saving your pdf for specific proposes,  you could have the marco ask if a general production pdf or outsourced part pdf , or cnc pdf or bom list or what ever .

                                                                                              • Re: Save As PDF in Different Folder Using Macro
                                                                                                Solid Works

                                                                                                Yes, sorry, by server I meant network drive. The PDF save location depends on the purpose/creator.  If sales creates the PDF then they'll typically save it to some folder (exact folder also depends on purpose) on their own network drive.

                                                                                                 

                                                                                                Here is a rough layout (all separate nework drives).  Is this what you mean by examples of the filing system?

                                                                                                   Model/Drawing network drive

                                                                                                   Sales network drive

                                                                                                   Other network drive

                                                                                                   Other network drive

                                                                                                 

                                                                                                So we'd like the macro to allow us to select a save location, through a dialog box, AND remember it until we change it

                                                                                                 

                                                                                                Ideally we'd like a button on the toolbar.  Click the button and it saves (without asking anything).  Click the down arrow to change the save location.

                                                                                                Untitled.png

                                                                                                 

                                                                                                If having a down arrow button is too difficult then we're hoping...

                                                                                                Click button and it bring up a dialog box like the Save As dialog box except...

                                                                                                   PDF is already set as the file type   OR   PDF is the ONLY file type available so don't  have to set it

                                                                                                   It remembers the previous save location unlike the standard Save As dialog box.  So we can just quickly hit enter on the keyboard

                                                                                                 

                                                                                                Sorry for the slow replies. Busy day today

                                                                                                Thank you

                                                                                                  • Re: Save As PDF in Different Folder Using Macro
                                                                                                    Robert H.

                                                                                                    i need you to give me a break down of you folder structure, i dont believe you need the drop down arrow to change the location,  plus i think it will just get you into more trouble that its worth as you could easily forget to change the location of the pdf and just end up duping files into the wrong directory  ,  if your having the marco name the pdf why not have it just work out the save location, and have the marco ask if you want to save in sales / or the other drives,  this would only require you to add some properties to you drawing document and fill them in before saving out the pdf,  - the issue would be determining the properties to allow all of this to happen with a click of a button     

                                                                                                     

                                                                                                    not to mention are all your network drives configured to the same letter ?

                                                                                                     

                                                                                                    and why are the sales creating pdfs ? ,  surely you are issuing/releasing drawing to them ?

                                                                                                     

                                                                                                     

                                                                                                    this is what i mean by filing system see below, for all your network drives ,   i am assuming that you are making custom part/drawings for clients at thus they will go into a client folder,  or Production 

                                                                                                     

                                                                                                     

                                                                                                    sorry not at my workstation so i cant give you a example of mine but you get the idea

                                                                                                     

                                                                                                     

                                                                                                     

                                                                                                    21.png

                                                                                                      • Re: Save As PDF in Different Folder Using Macro
                                                                                                        Solid Works

                                                                                                        Morning Robert,

                                                                                                         

                                                                                                        Sales creates PDFs for suppliers (some request a PDF), clients (they lose their copy or don't have it with them currently), and outside printing. Again, PDFs are saved to different locations depending on purpose and not all PDFs are kept after they're created.

                                                                                                         

                                                                                                        The drop down/memorize location is so we have a intuitive future proof interface that everyone can use.  Obviously we don't have macro experience. If the macro doesn't let us choose and remember the location and our network or something else changes in the future then someone will have to come back on the forum and ask for a fix.

                                                                                                         

                                                                                                        The network drives are separate letters/hard drives.  Screenshot below

                                                                                                        Untitled.png

                                                                                                        Thank you

                                                                                                          • Re: Save As PDF in Different Folder Using Macro
                                                                                                            Robert H.

                                                                                                            Thats painfully, look at you dont have a system at all your just saving to random computers every time, each with there own space ?

                                                                                                             

                                                                                                            you need a central file server, and a properly set up network share, (along with a proper filing system, and numbering systems  ) what you presented there is not a network drive,  (that setup is to backwards, it actually hurts to look at !)

                                                                                                             

                                                                                                            every your asking for can be done, its not that hard to do, but to be honest as it would total waste of time,  - your company workflow seams, like it designed to waste time. 

                                                                                                             

                                                                                                            you dont need a marco but a total rethink of your filling system so that it reflect the need of all the users, rather that dumping files onto random computers,

                                                                                            • Re: Save As PDF in Different Folder Using Macro
                                                                                              Christian Chu

                                                                                              I'm new to macros. Can you please tell me where to add WinAPI?

                                                                                              I think he's never done any coding and kind of confusing of how to adapt these lines of coding here

                                                                                          • Re: Save As PDF in Different Folder Using Macro
                                                                                            Solid Works

                                                                                            Thank you Deepak but I don't have admin abilities to install Team Viewer or anything