×
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

SolidWorks macro with Excel

SolidWorks macro with Excel

SolidWorks macro with Excel

(OP)
Hi,
I am creating macro where I first open excel file, then I am supposed by VBA from SW macro access already opened excel file. Just to mention, below code works, however vba opens about 10 copies of same excel file while it is working and I think the reason for it is For loop in the beginning. Is there any other way for me to directly access already opened excel file. Each excel file would have different name, therefore vba should first find a name of the excel file directly if possible (without for loop), then call the excel file, pull data from it,then close excel file and all its copies if made.
Here is the code:

Sub main()

Set objExcel = GetObject(, "Excel.Application")
For Each wbExcel In objExcel.Workbooks
excelFullName = wbExcel.FullName
Next

Set wbExcel = Workbooks.Open(excelFullName)
Set wsExcel = wbExcel.Sheets("Sheet1")

strP7 = wsExcel.Cells(7, 15) ' takes value from cell
strP8 = wsExcel.Cells(8, 15) ' takes value from cell

dblP7 = CDbl(strP7)
dblP8 = CDbl(strP8)

wbExcel.Close SaveChanges:=False


Set xlApp = Nothing
Set objExcel = Nothing
Set wbExcel = Nothing
Set wsExcel = Nothing
----------macro continues with SW data and double values taken from excel-------------

RE: SolidWorks macro with Excel

I do not think the for loop at the beginning is opening the files. If you only have a single Excel file open in a single instance of Excel, it should only loop once. Below are two modifications that might work.

Eric

CODE

Sub main()

Set objExcel = GetObject(, "Excel.Application")
Set wbExcel = objExcel.Workbooks(1)
Set wsExcel = wbExcel.Sheets("Sheet1")

strP7 = wsExcel.Cells(7, 15) ' takes value from cell
... 
Or

CODE

Sub main()

Set objExcel = GetObject(, "Excel.Application")
For Each wbExcel In objExcel.Workbooks
  Set wsExcel = wbExcel.Sheets("Sheet1")
Next

strP7 = wsExcel.Cells(7, 15) ' takes value from cell
... 

RE: SolidWorks macro with Excel

(OP)
Sorry, I did not reply earlier as I completely forgot I did post question on a forum. In short, I did resolve the issue same day using "Kill Taskbar" command to close any excel activity after passing all data from excel to variables inside the macro before reshuffling everything and assigning values to SolidWorks variables. Thanks for help.

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