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
SolidworksApi macros
Replace this line
with these lines
Hi Deepak,
Thanks for your prompt response. I made a slight change to your lines. I want Revision number at the end of the file name so I tweaked it.
PDFFileName = Mid(FileName, InStrRev(FileName, "\") + 1)
PDFFileName = Left(PDFFileName, InStrRev(PDFFileName, ".") - 1) & "-R" & Revision
As you can see I have PDFs folder sitting under 01 Released drawings folder but MkDir has a limitation that it won't create a PDFs folder until 01 Released Drawings folder is present. I used System.IO.Directory on Autodesk Inventor ilogic, I don't know how to perform this operation in VBA. Secondly I had a vbYesNo question if file already exists in the location and will ask a question if I would like to overwrite the file. How do I achieve this?
Regards,
Salman
Try these codes
<p>Option Explicit</p><p>Dim swApp As SldWorks.SldWorks</p><p>Dim swModel As SldWorks.ModelDoc2</p><p>Dim PDFFileName As String</p><p>Dim PDFPath As String</p><p>Dim Revision As String</p><p></p><p>Sub Main()</p><p>Set swApp = Application.SldWorks</p><p>Set swModel = swApp.ActiveDoc</p><p></p><p>'Get Revision</p><p>Revision = swModel.CustomInfo("Revision")</p><p></p><p>'Create Folders</p><p>PDFPath = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, "\"))</p><p>PDFPath = PDFPath & "01 Released Drawings"</p><p>If Dir(PDFPath, vbDirectory) = "" Then MkDir PDFPath</p><p>PDFPath = PDFPath & "\PDFs"</p><p>If Dir(PDFPath, vbDirectory) = "" Then MkDir PDFPath</p><p></p><p>'Set File Name</p><p>PDFFileName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)</p><p>PDFFileName = Left(PDFFileName, InStrRev(PDFFileName, ".") - 1) & "-R" & Revision & ".PDF"</p><p></p><p>'Save File</p><p>swModel.SaveAs (PDFPath & "\" & PDFFileName)</p><p></p><p>End Sub</p>Thanks Deepak.
I tweaked it a little bit to save it in a correct location.
'Create Folders
Filepath = swDraw.GetPathName
PDFPath = Left(Filepath, Len(Filepath) - 31)
PDFPath = PDFPath & "01 Released Drawings"
If Dir(PDFPath, vbDirectory) = "" Then MkDir PDFPath
PDFPath = PDFPath & "\PDFs"
If Dir(PDFPath, vbDirectory) = "" Then MkDir PDFPath
Regards,
Salman
I had no clue on 31 so just used the folder path itself. Glad to know that you got it working as desired.
Actually the drawing sits under "02 CAD Files" and 31 was to get out of this folder.
Well in that case, check the macro by in this post: https://forum.solidworks.com/message/497523#comment-497523
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
When you paste this in the macro. Do you directly paste or do you delete everything first? Either way its not working for me.
https://forum.solidworks.com/message/807602#comment-807602
You need to delete everything from the macro and paste these codes. Also add the "Microsoft Scripting Runtime" library else you would get error on this line Dim fso As New Scripting.FileSystemObject
Sorry missed this, in my inbox, though it was spam, a name like solid works doesn't help the matter !
Can you please tell me how to access that menu on 2017? According to search its supposed to be under Tools References but I don't see a References under tools
see attached
Robert, nothing happens when I run the macro. Thank you
save you drawing in to a folder label : CAD FILES
this is the line you want to look at or replace to match your filing systems
Filepath = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "CAD FILES") - 1)
Then run the Marco, - then look in the parent directory of the folder you just created , the will now be a DXF and PDF folders
p.s you might need to change some of the document properties to match the macro or edit the macro to match your title block, highlighted in orange
Drawingno = SWmoddoc.CustomInfo("Drawing No.")
Rev = SWmoddoc.CustomInfo("version")
Title = SWmoddoc.CustomInfo("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
MsgBox ("File Already Exists.")
End If
End
End If
swDraw.SaveAs (Compileddir + ".PDF")
swDraw.SaveAs (Compileddir2 + ".DWG")
End Sub
All of our files are on the server. Is it possible for the macro to just ask every time where we want the PDF to be saved to?
Look at this thread, which you can select a folder to save your pdf file
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
Since this is a windows API, so you need to add a sub, say "WinAPI" and add these lines
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
I'm new to macros. Can you please tell me where to add WinAPI?
yes thats call commonly know as the save as function
Save it in the modules where your main module is located
I still can't get it to work. Can you please take a look at the macros?
Please see the att. macro
You need to add a userform so the user can interact with the program by selecting a folder
Is there no way to have the macro just access the standard Save As dialog box? I don't know how to do userforms.
I already added a userform to the macro.
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.
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)
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
Put your code in the Browser command where you see the msgbox and change this line
<p>PDFPath = GetFolder("Select a destination folder or create a new one:")</p>I just copied the coded from above and pasted in the macro I sent you
<p>Dim swApp As SldWorks.SldWorks</p><p>Dim swModel As SldWorks.ModelDoc2</p><p>Dim swDraw As SldWorks.ModelDoc</p><p>Dim Filepath As String</p><p>Dim FileName As String</p><p>Dim PDFFileName As String</p><p>Dim InterPath As String</p><p>Dim PDFPath As String</p><p>Dim oFolder As String</p><p>Dim Revision As String</p><p>Dim boolstatus As Boolean</p><p>Dim PDFPathandName As String</p><p>Private Sub CommandButton1_Click()</p><p></p><p></p><p>Set swApp = Application.SldWorks</p><p>'Set swModel = swApp.ActiveDoc</p><p>Set swDraw = swModel</p><p>Set swDraw = swApp.ActiveDoc</p><p></p><p>'Check to see if a drawing is loaded.</p><p>If (swDraw Is Nothing) Or (swDraw.GetType <> swDocDRAWING) Then</p><p>swApp.SendMsgToUser ("To be used for drawings only, Open a drawing first and then TRY!")</p><p>'If no model currently loaded, then exit</p><p>Exit Sub</p><p>End If</p><p></p><p>Filepath = swDraw.GetPathName</p><p>InterPath = Left(Filepath, Len(Filepath) - 31)</p><p>MsgBox (InterPath)</p><p></p><p>PDFPath = InterPath & "01 Released Drawings" & "\PDFs"</p><p>MsgBox (PDFPath)</p><p></p><p>Revision = swDraw.CustomInfo("Revision")</p><p>FileName = swDraw.GetPathName</p><p>PDFFileName = Mid(FileName, InStrRev(FileName, "\") + 1)</p><p>PDFFileName = Left(PDFFileName, InStrRev(PDFFileName, ".") - 1)</p><p>MsgBox (PDFFileName)</p><p></p><p>oFolder = PDFPath</p><p>If Dir(oFolder, vbDirectory) = vbNullString Then</p><p>MkDir (oFolder)</p><p>MsgBox "Folder Created"</p><p>End If</p><p></p><p></p><p>PDFPath = GetFolder("Select a destination folder or create a new one:") '*** I ONLY CHANGED THIS LINE ***</p><p></p><p>PDFPathandName = PDFPath & "/" & PDFFileName</p><p></p><p>swDraw.SaveAs (PDFPathandName & ".PDF")</p><p></p><p></p><p></p><p></p><p>End Sub</p>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.
i wouldn't bother , i have offered to help to write one for him, which he hasn't responded to ! , i believe he is just wasting everyone time
, 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.
i have emailed him my self yesterday offering to write something (its been a slow week !) - it your time , maybe your right , seam like hes wasting everyone time, rather that making a case for what he wants to do
I think he's never done any coding and kind of confusing of how to adapt these lines of coding here
Thank you Deepak but I don't have admin abilities to install Team Viewer or anything
Robert, thank you but but I don't see any PMs in my forum account
- PM message bellow
MONDAY, 11 DECEMBER 2017 - 20:16 - UK GMT
Robert H.
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
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
Christian,
I copy pasted the above into a new macro and run it.
The Browse dialog box came up. I clicked Browse and got
Compile error:
Sub of Function not defined
Thank you
Are you sure to paste the code to the macro1 I sent you yesterday
Here is the macro - see att. file
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 .
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.
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
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
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
Thank you
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,
Can you please make the macro?