×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Cannot find SolidWorks object

Cannot find SolidWorks object

Cannot find SolidWorks object

(OP)
I am trying to cycle thru the drawings in a folder to extract some information in Excel. My macro starts in excel, goes to the first drawing, does what's supposed to do,comes back to excel, writes the information, but when it should go to next drawing in SW I get an error saying that it cannot find the SolidWorks object.

Anybody has an idea?

Andrew

RE: Cannot find SolidWorks object

(OP)
Actually the error is:

"Object variable or With block variable not set"

??????

A

RE: Cannot find SolidWorks object

I have seen this message, but not in awhile.  Look over your code to see that it loops correctly.  Look also at If...Then...Else blocks,  Select Case blocks, etc.  Or post your code, I sure some of the sharp people out here will see the error.

Regg

RE: Cannot find SolidWorks object

Also, make sure that you use the Set statement for the methods that return objects. i.e. Set swDwg = swApp...

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.

RE: Cannot find SolidWorks object

(OP)
Here's my code. It's a mixture of several routines and it's still in work so don't be too critical about as it look like. It is a macro in Excel. This is what it does:

1. Asks for a path and a drawing mask.
2. Creates a list if the drawings.
3. Start loop
4. Switch to SolidWorks
5. Opens up the first drawing
6. Looks for BOM
7. If BOM not found goto #9.
8. If BOM found reads BOM into a table, opens up the associated model and reads some custom properties, closes the model
9. Close the drawing
10. Switch to Excel
11. If BOM was founs transfer data in Excel
12. Select next file name
13. Loop

And the code:

Option Explicit
Option Base 1

Dim swApp As Object
Dim swApp1 As Object
Dim swPart As Object
Dim swView As Object
Dim swBOM As Object
Const swDocDRAWING = 3
Const swOpenDocOptions_Silent = &H1
  Dim sPath As String
    Dim sFileSW As String
 
 
    Dim iBom As Integer
Dim sMsg As String, sMask As String
Public Type BOM_Data
    Item As String
    Qty As String
    PartNumber As String
    Title As String
    Matl As String
    partno As String
End Type
Public LineItem() As BOM_Data
Dim nobom As Boolean, lngOha As Long


Sub ImportBOM()
'
' ImportBOM Macro
' Macro recorded 2/27/2003 by ATNAGY
'
Dim swError As Long, iPos As Long, bClose As Boolean, returnOK As Boolean
Dim nextdoc As Object, stmp As String, Model As Object
Dim sModelName As String, strConfName As String, sPartName As String
Dim strTitle As String, strPartNumber As String, strMaterial As String

    Dim ret As Variant
    Dim i As Integer, iItems As Integer
    Dim docType As Integer, docTitle As String
    
'get path
sMsg = "Enter the Path to the Drawings." & vbCrLf & _
       "Make sure NOT to end it with a \"
sPath = InputBox(sMsg, "Enter Drawing Path", "G:\Bra-AFAQ-All\ENG\2003\B30185 Micromatic\Mech\SW Drawings")
If sPath = "" Then
    Exit Sub
Else
    sPath = sPath & "\"
End If
sMsg = "Enter drawings mask (if applicable). Use '*' for all."
sMask = InputBox(sMsg, "Drawings Mask", "B30185-22*")
If sMask = "" Then sMask = "*"
    
    'Attach to SolidWorks
    On Error Resume Next
    
    'process drawing files
    sFileSW = Dir(sPath & sMask & ".slddrw")
    iBom = 0
    nobom = False
    Set swApp = CreateObject("SldWorks.Application")
    Do While sFileSW <> ""
    'switch to solid works
    Set swApp = GetObject(, "SldWorks.Application")
    swApp.Visible = True
    If Err.Number <> 0 Then
        MsgBox "Can not Find SldWorks.Application" & vbCrLf & _
               "ErrNo: " & Err.Number & "  ErrMsg: " & Err.Description _
               , vbOKOnly, "Error in ExportBOM()"
        Err.Clear
        GoTo CleanUp
    End If
    
    'open drawing
    Set swPart = swApp.OpenDoc2(sPath & sFileSW, swDocDRAWING, False, False, True, lngOha)
        
    If swPart Is Nothing Then
    Call MsgBox("Unable to open document!", vbExclamation, "Import BOM")  ' Display error message
    GoTo CleanUp           ' If no model currently loaded, then exit
    Else
     docType = swPart.GetType
     docTitle = swPart.GetTitle
End If


    'Get Drawing Template (first view)
    Set swView = swPart.GetFirstView
    
    'Get the BOM
    Set swBOM = swView.GetBomTable
    
    'Find the BOM - must find the view that contains the BOM
    Do While swBOM Is Nothing And Not swView Is Nothing
        Set swView = swView.GetNextView
        Set swBOM = swView.GetBomTable
    Loop
    If swBOM Is Nothing Then
'        Screen.MousePointer = vbDefault
'        MsgBox "Can NOT find the BOM on the current drawing!"
'        GoTo CleanUp
    nobom = True
    GoTo oha
    End If
    
    'Attach to the BOM
    ret = swBOM.Attach2
    If ret = False Then
        MsgBox "Error Attaching to BOM"
        Exit Sub
    End If
    
    'Put the BOM table in an array
    iItems = swBOM.GetRowCount - 1
    ReDim LineItem(iItems)
    For i = 1 To iItems
        LineItem(i).Item = swBOM.GetEntryText(i, 0)
        LineItem(i).Qty = swBOM.GetEntryText(i, 1)
        LineItem(i).PartNumber = swBOM.GetEntryText(i, 2)
        LineItem(i).Title = swBOM.GetEntryText(i, 3)
        LineItem(i).Matl = swBOM.GetEntryText(i, 4)
        LineItem(i).partno = swBOM.GetEntryText(i, 5)
    Next i
    'Detach from the BOM
    swBOM.Detach
    
    'MsgBox "BOM Exported Successfully!"
    'read documents data
    'If doc is drawing activate model and read properties from there
'get to the referenced configuration for that
        Set swView = swPart.GetFirstView    'dwg template
        Set swView = swView.GetNextView     'first dwg view
'get referenced model
        sModelName = swView.GetReferencedModelName()
        strConfName = swView.ReferencedConfiguration

'switch to the model if it is already open
        sPartName = sModelName
        iPos = InStr(1, sPartName, "\")
        Do While iPos > 0
            sPartName = Right(sPartName, Len(sPartName) - iPos)
            iPos = InStr(1, sPartName, "\")
        Loop
          
        On Error Resume Next
        Set nextdoc = swApp.GetFirstDocument
        stmp = nextdoc.GetTitle
        bClose = True 'assume file is not open
        Do While stmp <> ""
            If InStr(1, stmp, sPartName) > 0 Then
                If nextdoc.Visible = True Then
                    bClose = False 'document is already open
                Else
                    bClose = True 'document is not open yet
                End If
                Exit Do
            End If
            Set nextdoc = nextdoc.GetNext
            stmp = nextdoc.GetTitle
            If Err.Number <> 0 Then Exit Do
        Loop
        
        Set Model = swApp.ActivateDoc2(sModelName, True, swError)
        ' get custom properties from the model
         
        strTitle = Model.CustomInfo2(strConfName, "Title")
       
        strPartNumber = Model.CustomInfo2(strConfName, "Drawing_No")

        strMaterial = Model.CustomInfo("Material")

'reactivate drawing
'close the model
        Model.Save2 True
        If bClose = True Then 'only close if we had to open it
            swApp.CloseDoc sModelName
        End If
        
        Set swPart = swApp.ActivateDoc2(docTitle, True, swError)
'close drawing
oha:        swApp.CloseDoc docTitle
'return to Excel
Set swApp1 = GetObject(, "Excel.Application")
swApp1.Visible = True
'enter first drawing info
'if bom found
If (Not nobom) Then
'counter
iBom = iBom + 1

ActiveCell.Offset(0, 2).Range("A1").Select
Call SmallPause
ActiveCell.FormulaR1C1 = strPartNumber
ActiveCell.Offset(0, 1).Range("A1").Select
Call SmallPause
ActiveCell.FormulaR1C1 = strMaterial
ActiveCell.Offset(0, 1).Range("A1").Select
Call SmallPause
ActiveCell.FormulaR1C1 = strTitle

' ActiveCell.Offset(0, 4).Range("A1").Select
'enter BOM info
For i = 1 To iItems
    ActiveCell.Offset(1, -4).Range("A1").Select
    Call SmallPause
    ActiveCell.FormulaR1C1 = LineItem(i).Item
    ActiveCell.Offset(0, 1).Range("A1").Select
    Call SmallPause
    ActiveCell.FormulaR1C1 = LineItem(i).Qty
    ActiveCell.Offset(0, 1).Range("A1").Select
    Call SmallPause
    ActiveCell.FormulaR1C1 = LineItem(i).PartNumber
    ActiveCell.Offset(0, 1).Range("A1").Select
    Call SmallPause
    ActiveCell.FormulaR1C1 = LineItem(i).Matl
    ActiveCell.Offset(0, 1).Range("A1").Select
    Call SmallPause
    ActiveCell.FormulaR1C1 = LineItem(i).Title
'    ActiveCell.Offset(0, 1).Range("A1").Select
'    ActiveCell.FormulaR1C1 = LineItem(i).xxx
Next i

ActiveCell.Offset(3, -4).Range("A1").Select
End If

    
'    Set swApp = GetObject(, "Excel.Application")
'    swApp.Visible = True
    'Set swPart = swApp.ActiveDoc
    
'next file
sFileSW = Dir
Loop        'loop


CleanUp:
    Set swApp = Nothing
    Set swPart = Nothing
    Set swView = Nothing
    Set swBOM = Nothing
    Set Model = Nothing
    Set nextdoc = Nothing
End Sub

Private Sub SmallPause()
Dim PauseTime, StartTime

    PauseTime = 0.25    ' Set duration.
    StartTime = Timer    ' Set start time.
    Do While Timer < StartTime + PauseTime
'        DoEvents    ' Yield to other processes.
    Loop
    
End Sub

RE: Cannot find SolidWorks object

What line causes the error? Remove the OnError statment to cause the program to break.

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.

RE: Cannot find SolidWorks object

One thing I notice, you try to start and attach to SolidWorks. You should only have to do one or the other. You should first try to attach. Only if it's not opened, should you create a new instance.

Set swApp = CreateObject("SldWorks.Application")
Do While sFileSW <> ""
    'switch to solid works
    Set swApp = GetObject(, "SldWorks.Application")
    swApp.Visible = True
    If Err.Number <> 0 Then
        MsgBox "Can not Find SolidWorks" '...
    End If
Loop    

Could look like this:
    
On Error Resume Next
Set swApp = GetObject(, "SldWorks.Application")
If Err.Number <> 0 Then   'SolidWorks Not Running
    Err.Clear
    Set swApp = CreateObject("SldWorks.Application")
End If
'Do Your Loop Here

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.

RE: Cannot find SolidWorks object

(OP)
Good suggestion, dsi.

The line where the program breaks is:

  Set swBOM = swView.GetBomTable

when it opens up a drawing which doesn't have a BOM.

RE: Cannot find SolidWorks object

One small detail: I noticed you define more than one variable per Dim statement.

I had a SW macro program that would fail, and it worked fine when I changed the declarations so that only one variable was declared per Dim statement.  I don't know why this worked.

Good and evil: wrap them up and disguise it as people.

RE: Cannot find SolidWorks object

I noticed you have the poor user manually typing the path name.

See this thread:
Thread559-55331 had a form that I wrote for accessing files, because I couldn't directly access the Common Dialog object from a macro.  After I posted this, I stopped using the form.

Good and evil: wrap them up and disguise it as people.

RE: Cannot find SolidWorks object

It looks as tho it's all there, so the first place Id check is the dual-booleans logic (OR/XOR instead of AND, etc), and break it up into 2 lines:


' try something like this:
Set swView = swPart.GetFirstView
    Do
      Set swBOM = swView.GetBomTable
      if not(swBOM is nothing) then exit do  ' quit loop if bom *IS* found
      Set swView = swView.GetNextView
      if swView is nothing then exit do    ' quit loop if view *IS NOT* found
    Loop

RE: Cannot find SolidWorks object

Tick: I got bitten for quite a while on this one before I discovered this VB/VBA *feature*  <cough>

' first declaration:
Dim MyFirstInt, MySecondInt as Integer
' above dims "MyFirstInt" as a VARIANT, "MySecondInt " as Integer


' second declaration:
Dim MyFirstInt as Integer, MySecondInt as Integer
' above dims "MyFirstInt" as Integer, "MySecondInt " as Integer


after I discovered this, I went back to the old style "%", "&" , "S", for variables, haa haa.

RE: Cannot find SolidWorks object

(OP)
Hey smart guys,

what rocheey proposed eliminates the error but it does not return to excel to fill it up.

For now I moved all the detail drawings into a separate folder so I had only assy drawings to process. And it worked.

I got from time to time a message like:

Microsoft Excel is waiting for another application to complete an OLE action.

which I would like to get rid of (it syops everything so I cannot run this thing overnight).

Obviously the error is related to looking for BOM into a drawing which does not have one.


Andrew

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources