Hide/Show rows/columns of the SW Vba part family

Hi all

In order to display a table as I want on a drawing, I need to format the excel file where it is located. This goes through hiding and in some cases showing columns/rows and I would like to do it automatically so by macro but I can't do it.

I recorded a macro in excel to see the look it can have:
Sub Cacher_ligne_colonne()
'
'Macro to hide the rows and columns I want
    Columns("B:E"). Select
    Selection.EntireColumn.Hidden = True
    Columns("I:K"). Select
    Selection.EntireColumn.Hidden = True
    Rows("35:36"). Select
    Range("A36"). Activate
    Selection.EntireRow.Hidden = True
    Rows("2:8"). Select
    Range("A8"). Activate
    Selection.EntireRow.Hidden = True
End Sub

But I guess under SW it's not the same, maybe you just need to add xlSW somewhere? 
And by saving the manipulation under SW it records only the movement of the window not the "masking" action:

Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = _
Application.SldWorks

Set Part = swApp.ActiveDoc
boolstatus = Part.Extension.SelectByID2("Parts Family", "DESIGNTABLE", 0, 0, 0, False, 0, Nothing, 0)
Part.InsertFamilyTableEdit
Dim myModelView As Object
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
Part.ClearSelection2 True
Part.CloseFamilyTable
End Sub

Thank you for your help!


 

I've been looking but still no solution, anyone please?

Hello

I think you should already open your table with this method: http://help.solidworks.com/2016/english/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IModelDoc2~InsertFamilyTableEdit.html

Then you create an Excel macro and you call it from SW.

1 Like

Thank you for your answer, indeed I am already opening my room family. I already have a well-developed code to choose configurations through a SW form so I already use this kind of syntax.

Being quite new to Vba programming, I didn't know that it was possible to call Excel macro* from SW. The idea would be to make a macro written in Excel but call from SW or it would run all the time? 
Do you have an example of code calling an Excel function from SW stp?

You gave yourself the answer all by yourself.

To access the methods and properties of a cell or row or column in excel, you need to use a WorkSheet object. 

In the macro you made, retrieve the sheet of the part family and then do:

mysheet.column("the column index"). Hidden= True

1 Like

Hi all

I'm coming back to this topic because I've done some research but I'm stuck again. I wrote a code that works in Excel in order to display all the rows and columns in a table:

Dim ShowAllLines As Integer
Dim ShowAllColumns As Integer
                 
ShowAllLines = Columns(1).Find("", after:=[A1]).Row
Range("A1:A" & ShowAllLines).Select
Selection.EntireRow.Hidden = False

ShowAllColumns = Rows(1).Find("", after:=[A1]).Column
Range("A1:" & Chr(64 + ShowAllColumns) & "1").Select
Selection.EntireColumn.Hidden = False

This works very well from the Vba module of Excel but as soon as I switch to SW I have an error from the first line: 

Dim ShowAllLines As Integer
Dim ShowAllColumns As Integer
                 
ShowAllLines = xlWS.Columns(1).Find("", after:=[A1]).Row
xlWS.Range("A1:A" & ShowAllLines).Select
Selection.EntireRow.Hidden = False
                 
ShowAllColumns = xlWS.Rows(1).Find("", after:=[A1]).Column
xlWS.Range("A1:" & Chr(64 + ShowAllColumns) & "1").Select
Selection.EntireColumn.Hidden = False

I guess it's in the syntax of the "Find" function that it gets stuck but I haven't found any help that already exists, that's why I'm taking the liberty of posting here.

Thank you for your help.

 

Hello

Did you add an Excel reference to your project?

Otherwise: https://excel.developpez.com/faq/index.php?page=Automation#ActiverReference

 

1 Like

By the way, use the new code tag with this button:

This gives a lot of readability to the code in the messages.

Thank you

1 Like

Thanks for the trick code remrem tag.
And yes I added the Excel reference:

Then give us your full code. Because we can't look for the error without seeing how you instantiate your variables...

I had to shorten my code because the rest was under construction so most likely incomprehensible to you and I changed the no of 2-3 variables.

Thank you 


code_lynkoa.txt

If you do a step-by-step debugging.

Are the vaiables xlWS and  xlWB different from Nothing?

 

No, they are equal to Nothing.

That's why it's dysfunctional!

This is the challenge of recovering the workbook and the excel sheet in progress.

Edit:

I have tried several avenues but without convincing results for the moment....

I don't really understand what you mean because for me I already get the active sheet and workbook since I write in it. In fact, thanks to my macro, I open the 3D file, I open the associated family, I write the user's choice in a cell, I close the family, I update the family, I rebuild and there my configuration is activated. 

And in this I would like to hide/show columns according to the user's choice.

I just saw that xlWB  is useless.

If your xlWS variable is equal to Nothing after this line:

Set xlWS = swDesignTable.EditTable2(False)

Then you don't get the sheet properly.

Yes, it's useless at the moment because as I said my code is still under construction so I created this variable for a future purpose :).

Indeed I saw xlWS was equal to Nothing but suddenly I don't see how to do it..

Hello

After a bit of research, you need to use the Worksheet property.

As stated in this example.

So you have to do:

Set xlWS = swDesignTable.Worksheet

And then check which is different from Nothing:

If Not xlWS Is Nothing Then

End if