Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Cannot find SolidWorks object

Status
Not open for further replies.

dogarila

Mechanical
Oct 28, 2001
594
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
 
Replies continue below

Recommended for you

Actually the error is:

"Object variable or With block variable not set"

??????

A
 
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 [smile]
 
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.
 
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 <> &quot;&quot;
'switch to solid works
Set swApp = GetObject(, &quot;SldWorks.Application&quot;)
swApp.Visible = True
If Err.Number <> 0 Then
MsgBox &quot;Can not Find SldWorks.Application&quot; & vbCrLf & _
&quot;ErrNo: &quot; & Err.Number & &quot; ErrMsg: &quot; & Err.Description _
, vbOKOnly, &quot;Error in ExportBOM()&quot;
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(&quot;Unable to open document!&quot;, vbExclamation, &quot;Import BOM&quot;) ' 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 &quot;Can NOT find the BOM on the current drawing!&quot;
' GoTo CleanUp
nobom = True
GoTo oha
End If

'Attach to the BOM
ret = swBOM.Attach2
If ret = False Then
MsgBox &quot;Error Attaching to BOM&quot;
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 &quot;BOM Exported Successfully!&quot;
'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, &quot;\&quot;)
Do While iPos > 0
sPartName = Right(sPartName, Len(sPartName) - iPos)
iPos = InStr(1, sPartName, &quot;\&quot;)
Loop

On Error Resume Next
Set nextdoc = swApp.GetFirstDocument
stmp = nextdoc.GetTitle
bClose = True 'assume file is not open
Do While stmp <> &quot;&quot;
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, &quot;Title&quot;)

strPartNumber = Model.CustomInfo2(strConfName, &quot;Drawing_No&quot;)

strMaterial = Model.CustomInfo(&quot;Material&quot;)

'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(, &quot;Excel.Application&quot;)
swApp1.Visible = True
'enter first drawing info
'if bom found
If (Not nobom) Then
'counter
iBom = iBom + 1

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

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

ActiveCell.Offset(3, -4).Range(&quot;A1&quot;).Select
End If


' Set swApp = GetObject(, &quot;Excel.Application&quot;)
' 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
 
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.
 
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.
Code:
Set swApp = CreateObject(&quot;SldWorks.Application&quot;)
Do While sFileSW <> &quot;&quot;
    'switch to solid works
    Set swApp = GetObject(, &quot;SldWorks.Application&quot;)
    swApp.Visible = True
    If Err.Number <> 0 Then
        MsgBox &quot;Can not Find SolidWorks&quot; '... 
    End If
Loop
Could look like this:
Code:
On Error Resume Next
Set swApp = GetObject(, &quot;SldWorks.Application&quot;)
If Err.Number <> 0 Then   'SolidWorks Not Running
    Err.Clear
    Set swApp = CreateObject(&quot;SldWorks.Application&quot;)
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.
 
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.
 
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.

[bat]Good and evil: wrap them up and disguise it as people.[bat]
 
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.

[bat]Good and evil: wrap them up and disguise it as people.[bat]
 
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
 
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 &quot;MyFirstInt&quot; as a VARIANT, &quot;MySecondInt &quot; as Integer


' second declaration:
Dim MyFirstInt as Integer, MySecondInt as Integer
' above dims &quot;MyFirstInt&quot; as Integer, &quot;MySecondInt &quot; as Integer


after I discovered this, I went back to the old style &quot;%&quot;, &quot;&&quot; , &quot;S&quot;, for variables, haa haa.
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor