Opening a file from Excel

I'm trying to open a SW file from an excel macro.

I use the following code:

Dim swApp       As SldWorks.SldWorks
Dim swModel     As SldWorks.ModelDoc2
Dim retval      As String
Dim xlApp       As Excel.Application
Dim xlWB        As Excel.Workbook
Dim exSheet     As Excel.Worksheet
Dim i           As Integer
Dim bool        As Boolean
Dim boolstatus  As Boolean
Dim myBool As Boolean
Dim myError As Long, myWarning As Long

Sub Bouton1_Cliquer()
 
myBool = Shell("C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\SLDWORKS.exe ", vbNormalFocus)
 
Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.OpenDoc("C:\Users\nouveau\Documents\mapiece.SLDPRT", swDocPART)
Set swApp = Nothing: Set swModel = Nothing
 
End Sub

Opening SW is fine, but the file mypiece.sldprt does not open. No error message.

I'm using SW 2015.

Can you tell me what's wrong?

Hello;

Your request and the attached code leave me perplexed.
What exactly do you want to do?
Open a specific document with a macro from Solidworks(Solidworks API)?
or
Use Excel to open a document from a list (via a Macro button? ?
....
In both cases: prefer the Opendoc6 command("Location and Name of the file to open",swdocpart,swOpenDocOptions_Silent,"")
An example here: https://help.solidworks.com/2015//Open_Document_Example_VB.htm?verRedirect=1
And the order information here:https://help.solidworks.com/2015/opendoc6.html

Kind regards

1 Like

I'm trying to open a Solidworks document from a button in an Excel file

I also tried the "opendoc6" function, without success.

Set swModel = swApp.OpenDoc6("C:\Users\nouveau\Documents\mapiece.SLDPRT", 2, 1, "", myError, myWarning)

Could it come from the options that I have trouble  defining? Here, ReadOnly mode. I would ideally like to open in ReadOnly AND Silent mode.

For my part I have this code to adapt (room 5, room 6 and MEP room  are to be replaced by your need). Tested and approved

Sub EssaiSW()

 Dim swApp As SldWorks.SldWorks
    Dim swModelUn As SldWorks.ModelDoc2
    Dim swModelDeux As SldWorks.ModelDoc2

    If swApp Is Nothing Then
        Set swApp = CreateObject("SLDWORKS.application")
        swApp.Visible = True
    Else
        Set swApp = Application.SldWorks
        MsgBox "Ouverture de Solidworks par Excel=Ok"
    End If

    Set swDocSpecification = swApp.GetOpenDocSpec("C:\Users\sebastien.denis\Desktop\Pièce5.sldprt")
    Set swModelUn = swApp.OpenDoc7(swDocSpecification)
    MsgBox swModelUn.GetTitle
    
    Set swDocSpecification = swApp.GetOpenDocSpec("C:\Users\sebastien.denis\Desktop\Pièce5.slddrw")
    Set swModelUn = swApp.OpenDoc7(swDocSpecification)
    MsgBox swModelUn.GetTitle
    
    swApp.CloseDoc swModelUn.GetTitle
    
    Set swModelUn = swApp.ActiveDoc
    MsgBox swModelUn.GetTitle
    
    'Set swDocSpecification = swApp.GetOpenDocSpec("C:\Users\sebastien.denis\Desktop\Pièce6.sldprt")
    'Set swModelDeux = swApp.OpenDoc7(swDocSpecification)
    
    'swApp.CloseDoc swModelUn.GetTitle

    'MsgBox swModelDeux.GetTitle
    
    'swApp.CloseDoc swModelDeux.GetTitle
    
    
End Sub

.

1 Like

Great, it works.

I continue my code;)