Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

JamesMcD

Automotive
Dec 10, 2001
25
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




 
Replies continue below

Recommended for you

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
 
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
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor