Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro - Help 3

Status
Not open for further replies.

Scorch

Mechanical
Feb 13, 2002
80
I currently have a macro that will create a PDF and save it to the active docs current directory. I am having trouble finding any reference to do a save as and change the directory that the file is saved in. The final result that I am looking for is to run the macro and have a PDF saved in a network directory so it can be accessed by manufacturing. I'm not sure SaveAs4 is what I am looking for. Any help is appreciated.


Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.
--Howard Aiken, IBM engineer

 
Replies continue below

Recommended for you

Are you looking to copy to the new location or save to a pre-determined location?





Remember...
"If you don't use your head,
your going to have to use your feet."
 
I want to save to a pre-determined location.

Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.
--Howard Aiken, IBM engineer

 
Yes, SaveAs4 is what you are looking for. There is an example in the help showing how this function is used.

If you are saving to a predetermined directory, use a vba macro and edit the path to point to the required location.

If you want to be able to browse to the save location use VB so that you have easy access to the common dialog control, which will include a save window from which you can set the file name and browse to the appropriate folder.
 
Here is what I did with the SaveAs4. I keep getting errors when the path is pasted in the code. I know they have an invalid character that I will have to work around, but can you let me know how to code the path to a network location. Everything I have tried has not worked.

BoolStatus = Doc.SaveAs4(\\Kmpdc\GROUP\~ENGINEERING\_Wip\DocName, 0, 0, e, w)

This is currently what I have in there and I get Syntax Error. I am at a loss.

Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.
--Howard Aiken, IBM engineer

 
Try this... More explicit path plus the added parens plus the added .file extension

Doc.SaveAs4("C:\Kmpdc\GROUP\~ENGINEERING\_Wip\DocName.pdf", 0, 0, e, w)




Remember...
"If you don't use your head,
your going to have to use your feet."
 
Not quite right. Either lose the parentheses and call SaveAs4 like a sub, or use a return boolean like boolstatus. Also, be sure to explicitly define the return variables e and w as long.
Code:
Dim e as Long
Dim w as Long
Doc.SaveAs4 "C:\Kmpdc\GROUP\~ENGINEERING\_Wip\DocName.pdf", 0, 0, e, w




[bat]I could be the world's greatest underachiever, if I could just learn to apply myself.[bat]
-SolidWorks API VB programming help
 
Below is the full code. BoolStatus is False and I am unable to save the PDF. I am not sure if I am missing something obvious or not. After working with code for a while you tend to want to smack your head into a brick wall. Thanks for your help.



Option Explicit

Dim swApp, Doc As Object

Const swDocDRAWING = 3
Const swMbWarning = 1
Const swMbInformation = 2
Const swMbOk = 2

Dim BoolStatus As Boolean
Dim LongStatus As Long
Dim e As Long
Dim w As Long

Dim Description As String
Dim Msg As String
Dim DocName As String

Sub main()

Set swApp = CreateObject("SldWorks.Application")
Set Doc = swApp.ActiveDoc

If ((Doc Is Nothing) Or (Not (Doc.GetType Eqv swDocDRAWING))) Then
Msg = "A drawing document must be active to use this command!"
LongStatus = swApp.SendMsgToUser2(Msg, swMbWarning, swMbOk)
End

Else
Description = Doc.CustomInfo2("", "Description")
DocName = Doc.GetPathName

If (Not (DocName = "")) Then
DocName = Left(DocName, Len(DocName) - 7)
Else
Msg = "Please save drawing before creating pdf!"
LongStatus = swApp.SendMsgToUser2(Msg, swMbWarning, swMbOk)
End
End If

If (Not (Description = "")) Then
DocName = DocName & "_" & Description & ".pdf"
Else
DocName = DocName & ".pdf"
End If

BoolStatus = Doc.SaveAs4("g:\Kmpdc\GROUP\~ENGINEERING\_Wip\DocName", 0, 0, e, w)

If BoolStatus = False Then
Msg = "Failed to save PDF document!"
LongStatus = swApp.SendMsgToUser2(Msg, swMbWarning, swMbOk)
Else
Msg = "Saved drawing as " & DocName
LongStatus = swApp.SendMsgToUser2(Msg, swMbWarning, swMbOk)
End If

End If

Set Doc = Nothing
Set swApp = Nothing

End Sub

Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.
--Howard Aiken, IBM engineer

 
I think that the problem is that the way I get the filename is also grabbing the path of the original doc and when it gets to the Doc.SaveAs4 to where I would like the file it fails at that point. Do you have any suggestions on how to aquire the filename without the file path.

Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.
--Howard Aiken, IBM engineer

 
change this...
Code:
BoolStatus = Doc.SaveAs4("g:\Kmpdc\GROUP\~ENGINEERING\_Wip\DocName", 0, 0, e, w)
to this...
Code:
BoolStatus = Doc.SaveAs4("g:\Kmpdc\GROUP\~ENGINEERING\_Wip\" & DocName, 0, 0, e, w)

What's wrong is the way you are concatenating your string. You have a name for your document in DocName, but when you include that name inside the quotes defining your save path, it isn't recognized as a variable because it is just part of a string.

[bat]I could be the world's greatest underachiever, if I could just learn to apply myself.[bat]
-SolidWorks API VB programming help
 
I changed the boostatus line to what you suggested and the value still is false and fails to make the PDF. I am going to go back thru the code again and see if I can find anything wrong. Thanks.

Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.
--Howard Aiken, IBM engineer

 
Tick,

With my limited knowledge of writing macros, it looks as if when DocName is created it has the full pathname from the original location. It then goes thru and gets the original extension (.slddrw) stripped off and a .pdf added, But the original path is still the same. So when it gets to the boolstatus line I think it is trying to save it to the specified directory with the DocName path still in the DocName. I think it looks something like: ("G:\~ENGINEERING\~Engineering Drawings\Vertical Beaters\Test Use\g:\Kmpdc\GROUP\~ENGINEERING\_Wip\Test Use Only.pdf"). So it gets confused if my thinking is correct and can't save the file. Now my question is how do I strip off the original path and keep only the file name. Would I use GetTitle?

Thanks,

Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.
--Howard Aiken, IBM engineer

 
A couple things:

1) Add this line above:

Code:
  debug.print(docname)
[\code]

This will help you figure out if the path is still in the docname.  If it is, use InStrRev for the last occurrence of "\", then Substring to remove the path, then Substring again to remove the extension.

2)  Make sure the target path exists and is accessible for the currently logged in user.  SaveAs4 will NOT create new folders

3)  If you couldhave a multi-sheet drawing with differently sized sheeets, be sure to add this line:

[code]
swModel.Extension.UsePageSetup = SwConst.swPageSetupInUse_e.swPageSetupInUse_DrawingSheet
[\code]

That will force each page in the PDF to match the orientation of the drawing sheet.

Evan T. Basalik, MCSD
--------------------------------
It's all about prioritization...
 
Evan,

The help is much appreciated. Like I said earlier I am fairly new to using VB so your post confused me. I'm sorry but could you explain it so someone like me could understand.

Thanks,

Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.
--Howard Aiken, IBM engineer

 
I added comments where I added/modified code:

Code:
Option Explicit

Dim swApp As SldWorks.SldWorks
Dim Doc As SldWorks.ModelDoc2

Const swDocDRAWING = 3
Const swMbWarning = 1
Const swMbInformation = 2
Const swMbOk = 2

Dim BoolStatus As Boolean
Dim LongStatus As Long
Dim e As Long
Dim w As Long

Dim Description As String
Dim Msg As String
Dim DocName As String

Sub main()

Set swApp = CreateObject("SldWorks.Application")
Set Doc = swApp.ActiveDoc

If ((Doc Is Nothing) Or (Not (Doc.GetType Eqv swDocDRAWING))) Then
Msg = "A drawing document must be active to use this command!"
LongStatus = swApp.SendMsgToUser2(Msg, swMbWarning, swMbOk)
End

Else
Description = Doc.CustomInfo2("", "Description")
DocName = Doc.GetPathName

If (Not (DocName = "")) Then
DocName = Left(DocName, Len(DocName) - 7)
Else
Msg = "Please save drawing before creating pdf!"
LongStatus = swApp.SendMsgToUser2(Msg, swMbWarning, swMbOk)
End
End If

If (Not (Description = "")) Then
DocName = DocName & "_" & Description & ".pdf"
Else
DocName = DocName & ".pdf"
'at this point, DocName still has the full path
DocName = Mid(DocName, InStrRev(DocName, "\") + 1)
'now, DocName is just Filename.pdf
End If

Dim TargetLocation As String
'don't forget the trailing "\"
TargetLocation = "c:\temp\"

'also, set the pagesetup to adjust on a sheet by sheet basis
Doc.Extension.UsePageSetup = SwConst.swPageSetupInUse_e.swPageSetupInUse_DrawingSheet

'concatenate the TargetLocation and Filename
'if the TargetLocation does not exist, the call will fail
BoolStatus = Doc.SaveAs4(TargetLocation & DocName, 0, 0, e, w)

If BoolStatus = False Then
Msg = "Failed to save PDF document!"
LongStatus = swApp.SendMsgToUser2(Msg, swMbWarning, swMbOk)
Else
Msg = "Saved drawing as " & DocName
LongStatus = swApp.SendMsgToUser2(Msg, swMbWarning, swMbOk)
End If

End If

Set Doc = Nothing
Set swApp = Nothing

End Sub
[\code]


Evan T. Basalik, MCSD
--------------------------------
It's all about prioritization...
 
Evan,

That works great. Thanks for all your help. This will save us alot of time folder and Drive jumping.

Thanks Again

Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.
--Howard Aiken, IBM engineer

 
I use macros to DXF And PDF in a few different ways. I have specific locations I need to DXF all drawings too so they can be released by our config manager. I use a macro to save the dxf to this server location with the same file name as the drawing, I strip off the orig file location from the file name before doing it.

PDF's I do a whole different way. My macro calls the print dialouge to use CUTE PDF (freeware) and then saves the file (retaining the name, in a predetermined location) and I use a third macro that prompts me for location and file name for those odd instances where I need to PDF to someplace other than my ready for release directory.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor