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-------------
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
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 ...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