×
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

API Question for opening Excel

API Question for opening Excel

API Question for opening Excel

(OP)
In my feable attempts to open Excel from SW, I have tried to mirror some previous code and it works to an extent.

This code was written from Excel to open SolidWorks.  It opens SolidWorks and then a given document.  Assume variables have been named and defined.


‘Opens SolidWorks
Set swApp = CreateObject("SldWorks.Application")
swApp.Visible = True
Set Part = swApp.ActiveDoc

‘ Load model and expand window
Set Part = swApp.OpenDoc("folder location\file.SLDPRT", 1)
Set Part = swApp.ActivateDoc("Pulley.SLDPRT")
swApp.ActiveDoc.ActiveView.FrameLeft = 0
swApp.ActiveDoc.ActiveView.FrameTop = 0
swApp.ActiveDoc.ActiveView.FrameState = 1
swApp.ActiveDoc.ActiveView.FrameState = 1

========

What I want to achieve is the same things, but from the reverse direction.  I want to open Excel from SolidWorks.

Set ExSheet = CreateObject("Excel.Sheet")
ExSheet.Application.Visible = True
Set exDoc = ExSheet.ActiveDoc

' Load Excel and expand window
Set exDoc = ExSheet.OpenDoc("folder location\file.xls", 1)
Set exDoc = ExSheet.ActivateDoc("Pulley.SLDPRT")
ExSheet.ActiveDoc.ActiveView.FrameLeft = 0
ExSheet.ActiveDoc.ActiveView.FrameTop = 0
ExSheet.ActiveDoc.ActiveView.FrameState = 1
ExSheet.ActiveDoc.ActiveView.FrameState = 1

I’m getting lost at the point where Excel opens.  It opens Excel and a sheet, but stops at the bold text line.  The debugger message box opens and displays “Object doesn’t support this method or property”.  I believe that I’m using the wrong syntax to keep going in Excel so I can’t even tell if the rest would be correct.

Any suggestions?  My API experience is very basic.  I’m a cutter and paster from examples and try to get them to work. So anything would help.

Christopher Zona - Senior CAD Designer
Concord, Ontario

RE: API Question for opening Excel

I think I'm missing something. Why use API to open Excel from SW?

Chris
Systems Analyst, I.S.
SolidWorks/PDMWorks 05
AutoCAD 06
ctopher's home site (updated 06-21-05)
FAQ559-1100
FAQ559-716

RE: API Question for opening Excel

(OP)
It's part of a larger project to define a model, but there's more going on than just a design table.

Christopher Zona - Senior CAD Designer
Concord, Ontario

RE: API Question for opening Excel

Christopher,

One cannot assume that API calls used in SolidWorks are the same as those for Excel.

I do not know exactly what object you are trying to retrieve.  For the active workbook use xlApp.ActiveWorkbook, and for the active sheet use xlApp.ActiveSheet where xlApp is the excel application object.  Your code looks like it returns the active sheet.  Without looking into it more I do not know how to get the application object from your code.

SA

RE: API Question for opening Excel

Here's a snippet from a macro that exports a SW BOM table to Excel.  

tblAnnot is a pointer to a BOM table (code to find the BOM on the sheet is not shown here).  

The code reads the entire BOM into a 2D array then deposits the contents into the Excel sheet.

Hope this helps.

CODE

ReDim sTempArray(tblAnnot.RowCount, tblAnnot.ColumnCount)
For i = 0 To tblAnnot.RowCount - 1
    For j = 0 To tblAnnot.ColumnCount - 1
        sTempArray(i, j) = tblAnnot.Text(i, j)
    Next j
Next i

Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.Workbooks.Open ExcelTempPath
Set xlBook = xlApp.Workbooks(xlApp.Workbooks.Count)
Set xlsheet = xlBook.Worksheets(1)

For i = 1 To UBound(sTempArray, 1) 'skip the first line (headers)
    For j = 0 To UBound(sTempArray, 2)
        xlsheet.Cells(i + 1, j + 1) = sTempArray(i, j)
    Next j
Next i

RE: API Question for opening Excel

(OP)
SolidAir & Handleman,

Thanks for the response.  A combination of both samples worked for what I am trying to set up.

Christopher Zona - Senior CAD Designer
Concord, Ontario

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