What am I doing wrong?

Discussion in 'SolidWorks' started by ronsharo, Mar 5, 2009.

  1. ronsharo

    ronsharo Guest

    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
     
    ronsharo, Mar 5, 2009
    #1
  2. ronsharo

    That70sTick Guest

    Dim nErrors as Long
    Dim nWarnings as Long
     
    That70sTick, Mar 6, 2009
    #2
  3. ronsharo

    That70sTick Guest

    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?
     
    That70sTick, Mar 6, 2009
    #3
  4. ronsharo

    That70sTick Guest

    That70sTick, Mar 6, 2009
    #4
  5. ronsharo

    ronsharo Guest

    ronsharo, Mar 6, 2009
    #5
  6. ronsharo

    Mike Guest

    In your code, variable PART becomes the modeldoc2 object, use it
    instead of swModel.
    StrFileok = 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 = Part.GetPathName line.

    Mike
     
    Mike, Mar 8, 2009
    #6
  7. ronsharo

    ronsharo Guest

    Hi Mike,

    Thanks for your help.

    I'm starting to understand more,I wish there was some info I can read
    and learn more about solidworks api I learned C at the UNI and know
    some visual basic but I must learn the bsics of SW programing cause I
    seem to miss something all the time.

    here is the code I wrote:


    ******************************************************************************
    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 = Application.SldWorks
    Set Part = swApp.ActiveDoc

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

    StrFileok = 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.


    Ron.
     
    ronsharo, Mar 9, 2009
    #7
  8. ronsharo

    Mike Guest

    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 = True
    Set swApp = Application.SldWorks
    Set Part = swApp.ActiveDoc

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

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

    Set Part = Nothing
    Set swApp = 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
    http://angelsix.com/solidworks-2008-api-programming/ for more info.

    I cannot attest to it's quality or content!

    Good luck,
    Mike
     
    Mike, Mar 10, 2009
    #8
  9. ronsharo

    Mike Guest

    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 = True
    Set swApp = Application.SldWorks
    Set Part = 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 = Part.GetPathName
    drpath = Left(drpath, Len(drpath) - 6)
    drpath = drpath + "pdf"

    StrFileok = Part.SaveAs4(drpath, swSaveAsCurrentVersion,
    swSaveAsOptions_Silent, nErrors, nWarnings)
    If StrFileok = False Then
    nretval = 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 = Nothing
    Set swApp = 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
    http://angelsix.com/solidworks-2008-api-programming/ for more info.

    I cannot attest to it's quality or content!

    Good luck,
    Mike
     
    Mike, Mar 10, 2009
    #9
  10. ronsharo

    ronsharo Guest

    Hi Mike,

    Again, Thanks allot for your help.

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

    Ron.
     
    ronsharo, Mar 10, 2009
    #10
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.