×
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

Trying to write a macro to re-order sheets of a drawing...code inside

Trying to write a macro to re-order sheets of a drawing...code inside

Trying to write a macro to re-order sheets of a drawing...code inside

(OP)
I'm new to VBA and this macro-writing business, but here is my attempt.

This macro:

-inserts a sheet, naming it with the correct number (it does not get called sheetcount + 1 because we want to disregard one of the sheets when numbering them)

-Deletes a particular template note on inserted pages (I could not get it to delete by selecting the note I.D., so I did a dirty workaround of using a selection box.)

-Retrieves an array of the current sheet names

-Sorts them (I think...Haven't seen the results of this yet.)

-Applies the sorted names back to the drawing file using the SolidWorks IReorderSheets function.(Supposedly. This is the part I can't get to work. I get a "byRef argument type mismatch" error on "value = instance.IReorderSheets(SheetCount2, vSheetNames")

Has anyone used the SolidWorks API IReorderSheets function before? It seems that I have to dim vSheetNames As Variant to get that array created, but then the IReorderSheets function doesn't like the Variant type? I tried to change vSheetNames to String throughout but that put the error on the "vSheetNames = swDoc.GetSheetNames" line.

Any help would be greatly appreciated.

Thanks,

James
 




------------------------

Dim instance As IDrawingDoc
Dim value As Boolean
Dim swApp As Object
Dim vSheetNames As Variant
Dim swCurrentSheet As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim SheetCount2 As Integer
Dim SheetCount As Integer




Sub main()

Set swApp = _
Application.SldWorks

Set swDoc = swApp.ActiveDoc

If swDoc.GetType <> swDocDRAWING Then
    MsgBox "This macro is to be used with drawings only."
    Exit Sub
End If

' The above prevents the user from running this macro in part mode.

Set Part = swApp.ActiveDoc
SheetCount = swDoc.GetSheetCount 'counts the sheets
boolstatus = Part.NewSheet3("Sheet " & SheetCount, 12, 12, 1, 5, False, "u:\james mcdonald\template project\NEW SHEET.slddrt", 0.2794, 0.2159, "Default")

' New sheet is created and numbered to follow the counted sheets

Set swCurrentSheet = swDoc.GetCurrentSheet

' Current sheet ID is assigned to "swCurrentSheet" variable.

boolstatus = Part.Extension.SelectByID2("swCurrentSheet", "SHEET", 0.09845245225603, 0.1260896778594, 0, False, 0, Nothing, 0)

' Current sheet is selected

Part.EditTemplate

' Edit template mode selected

Part.ClearSelection2 True
boolstatus = Part.Extension.SketchBoxSelect("0.256344", "-0.018144", "0.000000", "0.612411", "0.061910", "0.000000")

' Selection box created to encompass part notes

Part.EditDelete

' Part notes deleted.

boolstatus = Part.Extension.SelectByID2("Sheet Format13", "SHEET", 0#, 0.125, 0, False, 0, Nothing, 0)
Part.EditSheet

' Return to edit sheet mode.

vSheetNames = swDoc.GetSheetNames

' Retrieves array of current sheet names

Call AlphaSort(vSheetNames)

' Routine to sort sheet names

SheetCount2 = swDoc.GetSheetCount

' Sheets counted a 2nd time, since one has been added.

value = instance.IReorderSheets(SheetCount2, vSheetNames)

' Call function to apply sorted sheet names to document.

End Sub

-----------------------------

Sub AlphaSort(vSheetNames As Variant)
     
    Dim First           As Integer
    Dim Last            As Integer
    Dim i               As Integer
    Dim j               As Integer
    Dim Temp            As String
    Dim List            As String
     
    First = LBound(vSheetNames())
    Last = UBound(vSheetNames())
    For i = First To Last - 1
        For j = i + 1 To Last
            If vSheetNames(i) > vSheetNames(j) Then
                Temp = vSheetNames(j)
                vSheetNames(j) = vSheetNames(i)
                vSheetNames(i) = Temp
            End If
        Next j
    Next i
End Sub

---------------------------------

Function IReorderSheets( _
    ByVal SheetCount2 As Integer, _
    ByRef vSheetNames As Variant _
) As Boolean
End Function




  

RE: Trying to write a macro to re-order sheets of a drawing...code inside

(OP)
OK here's my 2nd try. I've truncated this to onyl encompass the issue I'm concerned with.

Please, if anyone can help me with this, I'd be grateful. This one gives me "Subscript out of range" on the "First = LBound(sheetNameArray())" line in the sub-routine.

Thanks,
James

---------------------------------------

Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim SheetCount As Integer

Sub main()

Set swApp = _
Application.SldWorks
Set swDoc = swApp.ActiveDoc
Set Part = swApp.ActiveDoc
Part.ClearSelection2 True
Dim sheetNameArray As Variant

'get sheet count
SheetCount = swDoc.GetSheetCount

'Define array with sheet count
ReDim sheetNameArray(0 To SheetCount)

'Sort array elements
Call AlphaSort(sheetNameArray)

'reorder array
boolstatus = Part.ReorderSheets((sheetNameArray))
End Sub

Sub AlphaSort(sheetNameArray As Variant)
     
    Dim First           As Integer
    Dim Last            As Integer
    Dim i               As Integer
    Dim j               As Integer
    Dim Temp            As String
    Dim List            As String
     
    First = LBound(sheetNameArray())
    Last = UBound(sheetNameArray())
    For i = First To Last - 1
        For j = i + 1 To Last
            If sheetNameArray(i) > sheetNameArray(j) Then
                Temp = sheetNameArray(j)
                sheetNameArray(j) = sheetNameArray(i)
                sheetNameArray(i) = Temp
            End If
        Next j
    Next i
End Sub

RE: Trying to write a macro to re-order sheets of a drawing...code inside

Try changing it to:

    First = LBound(sheetNameArray)
    Last = UBound(sheetNameArray)

I am not sure how VB would try to interpret sheetNameArray() in that context.

Eric

RE: Trying to write a macro to re-order sheets of a drawing...code inside

(OP)
You are correct. That change allows the macor to funciton. Unfortunately, it doesn't do anything, because as you might have noticed I never populated the array that's being sorted with the names of the sheets.  Do you mind trying to take a stab at that using the getnames function?

My problem when I try, is that the array that the getnames function works with, apparently, must be Dim'd as Object. I'm struggling trying to properly declare an Object array and also trying to pass the values from it to my Variant array.

As I mentioned, I'm new to this.

Thanks,
James   

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