Iterate through open documents with vba?

Discussion in 'SolidWorks' started by What-a-Tool, Oct 5, 2004.

  1. What-a-Tool

    What-a-Tool Guest

    Is there a way to iterate through all open documents (Parts, Assemblies,
    Drawings) with a "for loop" or array?

    Can't find anything in the API, accept iterating through all sheets in a
    drawing with "GetSheetNames"

    Thanks

    --

    / Sean the Mc /


    "I have not failed. I've just found 10,000 ways that won't work."
    - Thomas Alva Edison (1847-1931)
     
    What-a-Tool, Oct 5, 2004
    #1
  2. Go to SW help search and look for "Get Names of Open Documents Example
    (VB)". This example goes through all the open docs.

    WT
     
    Wayne Tiffany, Oct 5, 2004
    #2
  3. What-a-Tool

    What-a-Tool Guest

    Looked in help and API help - can't find it. (using 04 rite now.) Searched
    online api help and can't find. Please give more detail?

    --

    / Sean the Mc /


    "I have not failed. I've just found 10,000 ways that won't work."
    - Thomas Alva Edison (1847-1931)
     
    What-a-Tool, Oct 5, 2004
    #3
  4. What-a-Tool

    That70sTick Guest

    You want to start with the EnumDocuments2 object, which is SW's
    collection of loaded documents. It's a bit klunky, but it works. See
    example cited in orhter message.
     
    That70sTick, Oct 5, 2004
    #4
  5. Sorry, I was in 2005. But,,,, I can't, for the life of me, figure out why I
    can't find the same example in 2004. I looked at the methods and they were
    available in 2001+ or so. Anyway, I have listed the example here for you.
    I don't know just ow it will parse out the lines, so pay attention. Or, let
    me know and I will email it directly to you.

    WT


    Get Names of Open Documents Example (VB)
    This example shows how to get the name of the documents open in a SolidWorks
    session.



    '-------------------------------------

    '

    ' Preconditions: SolidWorks application is running.

    '

    ' Postconditions: None

    '

    '-------------------------------------

    Option Explicit

    Public Enum swDocumentTypes_e

    swDocNONE = 0 ' Used to be TYPE_NONE

    swDocPART = 1 ' Used to be TYPE_PART

    swDocASSEMBLY = 2 ' Used to be TYPE_ASSEMBLY

    swDocDRAWING = 3 ' Used to be TYPE_DRAWING



    End Enum

    Sub main()

    Dim swApp As SldWorks.SldWorks

    Dim swModel As SldWorks.ModelDoc2

    Dim swModelTitle As SldWorks.ModelDoc2

    Dim swModelPath As SldWorks.ModelDoc2

    Dim cTitle As New Collection

    Dim vTitle As Variant

    Dim cPath As New Collection

    Dim vPath As Variant



    Set swApp = Application.SldWorks

    Set swModel = swApp.GetFirstDocument



    While Not swModel Is Nothing

    ' Assembly components are opened, but are not visible

    ' until opened by the user

    Debug.Print swModel.GetPathName & " [" & swModel.Visible & "]"

    Debug.Print " " & swModel.GetTitle & " [" & swModel.GetType & "]"



    ' The document name contains a filename extension

    ' if the document has been already saved

    ' and is subject to Microsoft Explorer setting;

    ' the document name does not contain a

    ' filename extension if the document has not been saved;

    ' ModelDoc2::GetPathName is blank until the file is saved

    cTitle.Add swModel.GetTitle



    cPath.Add swModel.GetPathName



    Set swModel = swModel.GetNext

    Wend

    Debug.Print "--------------------------"



    For Each vTitle In cTitle

    Set swModelTitle = swApp.GetOpenDocument(vTitle): Debug.Assert Not
    swModelTitle Is Nothing



    Debug.Print swModelTitle.GetPathName & " [" & swModelTitle.Visible
    & "]"

    Debug.Print " " & swModelTitle.GetTitle & " [" &
    swModelTitle.GetType & "]"

    Next vTitle

    Debug.Print "--------------------------"



    For Each vPath In cPath

    Set swModelPath = swApp.GetOpenDocument(vPath)

    If Not swModelPath Is Nothing Then

    Debug.Print swModelPath.GetPathName & " [" &
    swModelPath.Visible & "]"

    Debug.Print " " & swModelPath.GetTitle & " [" &
    swModelPath.GetType & "]"

    End If

    Next vPath

    Debug.Print "--------------------------"

    End Sub

    '-------------------------------------
     
    Wayne Tiffany, Oct 5, 2004
    #5
  6. What-a-Tool

    What-a-Tool Guest

    Thank You

    --

    / Sean the Mc /


    "I have not failed. I've just found 10,000 ways that won't work."
    - Thomas Alva Edison (1847-1931)

     
    What-a-Tool, Oct 6, 2004
    #6
  7. I posted something not too long ago. Do a search on this group under my
    name and "Close All Open Documents" and you should find the code.

    The SW sample was kinda sketchy, so I ended up rewriting their code.

    Evan


     
    Evan T. Basalik, Oct 8, 2004
    #7
  8. What-a-Tool

    What-a-Tool Guest

    <It's a bit klunky, but it works.>

    Similar in some ways to working with a database recordset

    --

    / Sean the Mc /


    "I have not failed. I've just found 10,000 ways that won't work."
    - Thomas Alva Edison (1847-1931)
     
    What-a-Tool, Oct 11, 2004
    #8
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.