How Can I Simplify this Code and Retain it's Functionality?

How Can I Simplify this Code and Retain it's Functionality?

Anonymous
Not applicable
376 Views
3 Replies
Message 1 of 4

How Can I Simplify this Code and Retain it's Functionality?

Anonymous
Not applicable

Hello,

 

I've created a basic If...Then...End If command to change the sheet size across all sheets in the idw file and zoom-extents on all sheets.  BUT, I've come to find out that if I delete one of the sheets, it kinda-sorta messes with the flow (the message "ThisDrawing.Sheet: No sheet named XXXX was found") and the zoom-extents rule does not run.

 

So, I was wondering if there's any way to possibly simplify the code such that it won't require the precise names of each individual sheet for it to work.  This way, if the user deletes one sheet, the rule can still maintain the same functionality.

 

Format:HTML Format Version:1.0 StartHTML: 165 EndHTML: 20052 StartFragment: 314 EndFragment: 20020 StartSelection: 314 EndSelection: 314SyntaxEditor Code Snippet

If Sheet_Size = "Letter (8.5 x 11)" Then

ActiveSheet = ThisDrawing.Sheet("Primary Plant:1")
ActiveSheet.ChangeSize("A", MoveBorderItems := True)
ActiveSheet = ThisDrawing.Sheet("Primary Plant:2")
ActiveSheet.ChangeSize("A", MoveBorderItems := True)
ActiveSheet = ThisDrawing.Sheet("Alternate Plant 1:3")
ActiveSheet.ChangeSize("A", MoveBorderItems := True)
ActiveSheet = ThisDrawing.Sheet("Alternate Plant 1:4")
ActiveSheet.ChangeSize("A", MoveBorderItems := True)
ActiveSheet = ThisDrawing.Sheet("Alternate Plant 2:5")
ActiveSheet.ChangeSize("A", MoveBorderItems := True)
ActiveSheet = ThisDrawing.Sheet("Alternate Plant 2:6")
ActiveSheet.ChangeSize("A", MoveBorderItems := True)
ActiveSheet = ThisDrawing.Sheet("Primary Plant:1")
iLogicVb.RunRule("Auto-Zoom to Extents")


ElseIf Sheet_Size = "Tabloid (11 x 17)" Then

ActiveSheet = ThisDrawing.Sheet("Primary Plant:1")
ActiveSheet.ChangeSize("B", MoveBorderItems := True)
ActiveSheet = ThisDrawing.Sheet("Primary Plant:2")
ActiveSheet.ChangeSize("B", MoveBorderItems := True)
ActiveSheet = ThisDrawing.Sheet("Alternate Plant 1:3")
ActiveSheet.ChangeSize("B", MoveBorderItems := True)
ActiveSheet = ThisDrawing.Sheet("Alternate Plant 1:4")
ActiveSheet.ChangeSize("B", MoveBorderItems := True)
ActiveSheet = ThisDrawing.Sheet("Alternate Plant 2:5")
ActiveSheet.ChangeSize("B", MoveBorderItems := True)
ActiveSheet = ThisDrawing.Sheet("Alternate Plant 2:6")
ActiveSheet.ChangeSize("B", MoveBorderItems := True)
ActiveSheet = ThisDrawing.Sheet("Primary Plant:1")
iLogicVb.RunRule("Auto-Zoom to Extents")

End If

 

0 Likes
Accepted solutions (1)
377 Views
3 Replies
Replies (3)
Message 2 of 4

Owner2229
Advisor
Advisor

Hey try one of these:

 

A) For specific sheets only:

 

 

Sub Main()
    If Sheet_Size = "Letter (8.5 x 11)" Then
        ChangeSheet("A")
        iLogicVb.RunRule("Auto-Zoom to Extents")
    ElseIf Sheet_Size = "Tabloid (11 x 17)" Then
        ChangeSheet("B")
    End If
End Sub

Sub ChangeSheet(NewSize As String)
    ThisDrawing.Sheet("Primary Plant:1").ChangeSize(NewSize, MoveBorderItems := True)
    ThisDrawing.Sheet("Primary Plant:2").ChangeSize(NewSize, MoveBorderItems := True)
    ThisDrawing.Sheet("Alternate Plant 1:3").ChangeSize(NewSize, MoveBorderItems := True)
    ThisDrawing.Sheet("Alternate Plant 1:4").ChangeSize(NewSize, MoveBorderItems := True)
    ThisDrawing.Sheet("Alternate Plant 2:5").ChangeSize(NewSize, MoveBorderItems := True)
    ThisDrawing.Sheet("Alternate Plant 2:6").ChangeSize(NewSize, MoveBorderItems := True)
    iLogicVb.RunRule("Auto-Zoom to Extents")
End Sub

 

B) For all sheets:

 

Sub Main()
    If Sheet_Size = "Letter (8.5 x 11)" Then
        ChangeSheet("A")
        iLogicVb.RunRule("Auto-Zoom to Extents")
    ElseIf Sheet_Size = "Tabloid (11 x 17)" Then
        ChangeSheet("B")
    End If
End Sub

Sub ChangeSheet(NewSize As String)
    For Each oSheet As Sheet In ThisDoc.Document.Sheets
oSheet.ChangeSize(NewSize, MoveBorderItems := True) Next iLogicVb.RunRule("Auto-Zoom to Extents") End Sub

 

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 3 of 4

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi t.desrosiers,

 

It looks like Owner2229 posted while I was looking at this, but here is another option.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Dim oSize As Boolean
oSize = InputRadioBox("Select size:", "Letter (8.5 x 11)", "Tabloid (11 x 17)", True, "iLogic")

If oSize = True Then
	Sheet_Size = "Letter (8.5 x 11)"
Else
	Sheet_Size = "Tabloid (11 x 17)"
End If

'^^^ the code above is just for testing

Dim oCurrentNumber  As Sheet
oCurrentNumber = ThisDoc.Document.ActiveSheet

For Each oSheet In ThisDoc.Document.Sheets	
	oSheet.Activate
	If Sheet_Size = "Letter (8.5 x 11)" Then
		ActiveSheet.ChangeSize("A", MoveBorderItems := True)	
	ElseIf Sheet_Size = "Tabloid (11 x 17)" Then
		ActiveSheet.ChangeSize("B", MoveBorderItems := True)
	End If
	
	iLogicVb.RunRule("Auto-Zoom to Extents")
	oSheet.Update
Next

oCurrentNumber.Activate

EESignature

0 Likes
Message 4 of 4

Anonymous
Not applicable

Curtis, your solution worked like a charm.  Thanks!

0 Likes