Writing to an Excel file from the SolidWorks VBA?

Hello everyone,

When I draw a simple part (without a big assembly), I currently use a macro that saves the part in a new folder created by the macro, it also saves a drawing of that part and a PDF of that drawing. Now I have an Excel directory in which I have a track of all my current projects. I would like this macro to also fill this directory with the name, the date, a hyperlink of the path...

So my first question is: What code can I use to open excel and excel file?

And the second one: What code can I use to simply write to a cell in an ecxel file ( from the solidworks VBA)?

Then, I think I could manage for the rest.

Thanks in advance!

Have a nice day

 

Hello

Look in the FOLLOWING example, you should find what you want there.

Kind regards

Hello, I didn't see what interests me in your example and I found very few examples of SolidWorks --> Excel communication, but since this morning, I have managed to make good progress on the issue.

For those looking for a solution to a similar problem:

'To open my excel file:

Set App = CreateObject("shell. Application")

MyFile = "C:\Users\sstagemaint\Desktop\PROJECT TRACKING.xlsm"
   App.Open (MyFile)

 

'Objects for orders in excel file:

   Dim Workbook As Object
   Dim Sheet As Object

   Dim exApp As Object


   Set exApp = CreateObject("Excel.Application")                                  ' Ecxel Application Selection
   Set Workbook = exApp.Workbooks("PROJECT TRACKING.xlsm")            ' Selecting the Workbook in the Application
   Set Sheet = Workbook.sheets("Sheet1")                                              ' Selecting the sheet in the workbook

 

' Select cell C8  from the SolidWorks Vba:

Sheet.Range("C8"). Select                         ' Select Cell in Sheet    

 

 

There you go, I finally solved the problem easier than I thought.

Thanks anyway for the answer.

Kind regards

Well yet in my example there is everything you need and after cleaning what is not useful in your case it gives for example:

Sub OpenExcel()

Dim xlApp As Excel.Application
Set xlApp = New Excel.Application
Dim wbk As Excel.Workbook
Dim sht As Excel.Worksheet

With xlApp
    . Visible = True
    Set wbk = . Workbooks.Add  ==> to open a new workbook (*)
    'Set wbk = . Workbooks.Open("C:\monfichier.xls") ==> to open an existing workbook (*)
    Set sht = wbk. ActiveSheet
End With

With sht
    . Range("A1"). Value = "Cell 1"
    . Range("A2"). Value = "Cell 2"
    . Range("A3"). Value = "Cell 3"
    . Range("A4"). Value = "Cell 4"
    . Range("A5"). Value = "Cell 5"
    . Range("A6"). Value = "Cell 6"
End With

End Sub

(*) Put one of the lines depending on what you want to do

And of course, don't forget to put the reference to "Microsoft Excel xx.0 Object Library".

Kind regards