What am I doing wrong?

Hi Everyone,

Im an api newbie please add your advice if you can.

What is wrong with the following code?

I get a "missing object" error

Oh and another Q - what does the debug.print do?

Thanks Ron.

' ****************************************************************************** ' C:\DOCUME~1\ron\LOCALS~1\Temp\swx3112\Macro1.swb - macro recorded on 03/05/09 by ron ' ****************************************************************************** Dim swApp As Object Dim Part As Object Dim SelMgr As Object Dim boolstatus As Boolean Dim longstatus As Long, longwarnings As Long Dim Feature As Object Dim drpath As String Dim StrFileok As Boolean

Dim nretval As Long

Sub main() StrFileok = True Set swApp = Application.SldWorks Set Part = swApp.ActiveDoc

drpath = Part.GetPathName drpath = Left(drpath, Len(drpath) - 6) drpath = drpath + "pdf" StrFileok = swModel.SaveAs4(drpath, swSaveAsCurrentVersion, swSaveAsOptions_Silent, nErrors, nWarnings) If StrFileok = False Then nretval = swApp.SendMsgToUser2("Problems saving file.", swMbWarning, swMbOk) End If

End Sub

Reply to
ronsharo
Loading thread data ...

Dim nErrors as Long Dim nWarnings as Long

Reply to
That70sTick

StrFileok = swModel.SaveAs4(drpath, swSaveAsCurrentVersion, swSaveAsOptions_Silent, nErrors, nWarnings)

Where did "swModel" come from? It is never Dim'd as an object or set as a SW model?

Reply to
That70sTick

Read this:

formatting link
Early binding will help you avoid some of the mistakes you are making.

Reply to
That70sTick

Thanks allot.

I Will read it.

do you know where can I learn Solidworks Api on the net?

Reply to
ronsharo

In your code, variable PART becomes the modeldoc2 object, use it instead of swModel.

StrFileok =3D Part.SaveAs4(drpath, swSaveAsCurrentVersion,

You should also use explicit object types (early binding) where possible rather than using generic object types.

i.e. Dim swApp As Sldworks.Sldworks Dim Part As Sldworks.ModelDoc2 Dim SelMgr As Sldworks.SelectionMgr

One advantage of early binding is intellisense, Type a variable name followed by a dot and Intellisense displays a drop-down list of all properties and methods available for it. Makes programming a lot easier!

Debug.print sends output to the immediate window for debugging purposes. Try it, Press Ctrl+G to open the immediate window then add Debug.print depath after the drpath =3D Part.GetPathName line.

Mike

Reply to
Mike

Hi Mike,

Thanks for your help.

I'm start =20

***************************************************************************= *** Dim swApp As SldWorks.SldWorks Dim Part As SldWorks.ModelDoc2 Dim SelMgr As SldWorks.SelectionMgr Dim boolstatus As Boolean Dim longstatus As Long, longwarnings As Long Dim Feature As Object Dim drpath As String Dim StrFileok As Boolean

Sub main()

Set swApp =3D Application.SldWorks Set Part =3D swApp.ActiveDoc

drpath =3D Part.GetPathName Debug.Print depath drpath =3D Left(drpath, Len(drpath) - 6) drpath =3D drpath + "pdf"

StrFileok =3D Part.SaveAs4(drpath, swSaveAsCurrentVersion, swSaveAsOptions_Silent, nErrors, nWarnings)

The last command send an error "Byref argument type mismatch"

I couldn't find the mismatch it lights the nErrors BTW.

Thanks for you help.

R> In your code, variable PART becomes the modeldoc2 object, use it > instead of swModel.

Reply to
ronsharo

Hi Ron,

The ByRef error happens because variables nErrors and nWarning were not declared as Long values

Try this code:

Sub main() Dim swApp As SldWorks.SldWorks Dim Part As SldWorks.ModelDoc2 Dim nErrors As Long Dim nWarnings As Long Dim drpath As String Dim StrFileok As Boolean Dim nretval As Long

StrFileok =3D True Set swApp =3D Application.SldWorks Set Part =3D swApp.ActiveDoc

If Part Is Nothing Then swApp.SendMsgToUser2 "No document open.", swMbWarning, swMbOk Else drpath =3D Part.GetPathName drpath =3D Left(drpath, Len(drpath) - 6) drpath =3D drpath + "pdf"

StrFileok =3D Part.SaveAs4(drpath, swSaveAsCurrentVersion, swSaveAsOptions_Silent, nErrors, nWarnings) If StrFileok =3D False Then nretval =3D swApp.SendMsgToUser2("Problems saving file.", swMbWarning, swMbOk) End If End If

Set Part =3D Nothing Set swApp =3D Nothing End Sub

As far a learning the API, Angelsix.com has or is getting ready to publish a Solidworks-2008-api-programming guide. See

formatting link
for more info.

I cannot attest to it's quality or content!

Good luck, Mike

Reply to
Mike

Hi Ron,

The ByRef error happens because variables nErrors and nWarnings were not declared as Long values.

Try this code: I added a couple other lines to enhance the macro.

--- Start of Code --- Option Explicit

' Option Explicit information from VB help ' When Option Explicit is used, you must explicitly declare all variables using the Dim or ReDim ' statements. If you attempt to use an undeclared variable name, an error occurs at compile time. ' ' IMHO, Option Explicit should ALWAYS be used. '

Sub main() Dim swApp As SldWorks.SldWorks Dim Part As SldWorks.ModelDoc2

' missing variables Dim nErrors As Long Dim nWarnings As Long

Dim drpath As String Dim StrFileok As Boolean Dim nretval As Long

StrFileok =3D True Set swApp =3D Application.SldWorks Set Part =3D swApp.ActiveDoc

' this check ensures we have a pointer to a document If Part Is Nothing Then swApp.SendMsgToUser2 "No document open.", swMbWarning, swMbOk Else drpath =3D Part.GetPathName drpath =3D Left(drpath, Len(drpath) - 6) drpath =3D drpath + "pdf"

StrFileok =3D Part.SaveAs4(drpath, swSaveAsCurrentVersion, swSaveAsOptions_Silent, nErrors, nWarnings) If StrFileok =3D False Then nretval =3D swApp.SendMsgToUser2("Problems saving file.", swMbWarning, swMbOk) End If End If

' clean-up object variables ' this isn't really required but makes better programming practice, Sorry but it's a carry-over ' from my C days Set Part =3D Nothing Set swApp =3D Nothing End Sub

--- End of Code ---

As far a learning the API, Angelsix.com has or is getting ready to publish a Solidworks-2008-api-programming guide. See

formatting link
for more info.

I cannot attest to it's quality or content!

Good luck, Mike

Reply to
Mike

Hi Mike,

Again, Thanks allot for your help.

I now understand more clearly the api help - didnt notice those two long veraibels.

Ron.

Reply to
ronsharo

PolyTech Forum website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.