Help simplifying code.

Help simplifying code.

Anonymous
Not applicable
374 Views
4 Replies
Message 1 of 5

Help simplifying code.

Anonymous
Not applicable
Hello All,

I'm trying to automate some things at work and I have this code which works but I don't like looking at how long it is. How do I shorten this simple code so that I can have the string length vary between 5 & 12 characters. Actually it can be much more than that, I just did not want to copy and paste over and over again.

Here is my code:

{code}
Public Sub Saveas_DXF15()
Dim strDrawingFullName As String
Dim MyString As String
Dim StringLength As Integer
MyString = ThisDrawing.Name
StringLength = Len(MyString)


If StringLength = 8 Then
strDrawingFullName = Right(ThisDrawing.Path & "-", Len(ThisDrawing.Path) - 66) & (Left(ThisDrawing.Name, Len(ThisDrawing.Name) - 5))
End If

If StringLength = 9 Then
strDrawingFullName = Right(ThisDrawing.Path & "-", Len(ThisDrawing.Path) - 66) & (Left(ThisDrawing.Name, Len(ThisDrawing.Name) - 6))
End If

If StringLength = 10 Then
strDrawingFullName = Right(ThisDrawing.Path & "-", Len(ThisDrawing.Path) - 66) & (Left(ThisDrawing.Name, Len(ThisDrawing.Name) - 7))
End If

If StringLength = 11 Then
strDrawingFullName = Right(ThisDrawing.Path & "-", Len(ThisDrawing.Path) - 66) & (Left(ThisDrawing.Name, Len(ThisDrawing.Name) - 8))
End If

If StringLength = 12 Then
strDrawingFullName = Right(ThisDrawing.Path & "-", Len(ThisDrawing.Path) - 66) & (Left(ThisDrawing.Name, Len(ThisDrawing.Name) - 9))
End If

If StringLength = 13 Then
strDrawingFullName = Right(ThisDrawing.Path & "-", Len(ThisDrawing.Path) - 66) & (Left(ThisDrawing.Name, Len(ThisDrawing.Name) - 10))
End If

If StringLength = 14 Then
strDrawingFullName = Right(ThisDrawing.Path & "-", Len(ThisDrawing.Path) - 66) & (Left(ThisDrawing.Name, Len(ThisDrawing.Name) - 11))
End If

If StringLength = 15 Then
strDrawingFullName = Right(ThisDrawing.Path & "-", Len(ThisDrawing.Path) - 66) & (Left(ThisDrawing.Name, Len(ThisDrawing.Name) - 12))
End If


ThisDrawing.saveas strDrawingFullName, acR15_dxf

'SET FILEDIA TO "1"
sysVarName = "FILEDIA"
ThisDrawing.SetVariable sysVarName, 1

ThisDrawing.Close



End Sub
{code}

All help is greatly appreciated.

Thanks,

Shawn
0 Likes
375 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable
wrote in message news:6267418@discussion.autodesk.com...
Hello All,

I'm trying to automate some things at work and I have this code which works
but I don't like looking at how long it is. How do I shorten this simple
code so that I can have the string length vary between 5 & 12 characters.
Actually it can be much more than that, I just did not want to copy and
paste over and over again.


doesn't that translate to
strDrawingFullName = Right(ThisDrawing.Path & "-",
Len(ThisDrawing.Path) - 66) & (Left(ThisDrawing.Name, 3))
?
or am i missing something?
0 Likes
Message 3 of 5

Anonymous
Not applicable
yes, but hoow to i shorten the code as opposed to writing the same statement with different values?
0 Likes
Message 4 of 5

Anonymous
Not applicable
Hi XIJIANGWOO,

Why not try the suggested code?

However, the code you have written does not make sense unless the path
is at least 67 characters long.

As my path is not that long, I've made couple of changes to enable the
results to be demonstrated.

I've also shown some ways of making your original code more efficient if
you continue to insist on using it.

{code}
Public Sub Saveas_DXF15()
Dim strDrawingFullName As String
Dim MyString As String
Dim StringLength As Integer
MyString = MyString
Dim i As Integer
Dim j As Integer
Dim sPath As String
StringLength = Len(MyString)
MyString = "1234567"
For i = 1 To 8
MyString = MyString & Chr(i + 64)
j = 66 ' for you
j = 26 ' Suits my path length for testing

StringLength = Len(MyString)
sPath = Right(ThisDrawing.Path & "-", Len(ThisDrawing.Path) - j)

If StringLength = 8 Then
strDrawingFullName = sPath & (Left(MyString, StringLength - 5))

ElseIf StringLength = 9 Then
strDrawingFullName = sPath & (Left(MyString, StringLength - 6))

ElseIf StringLength = 10 Then
strDrawingFullName = sPath & (Left(MyString, StringLength - 7))

ElseIf StringLength = 11 Then
strDrawingFullName = sPath & (Left(MyString, StringLength - 8))

ElseIf StringLength = 12 Then
strDrawingFullName = sPath & (Left(MyString, StringLength - 9))

ElseIf StringLength = 13 Then
strDrawingFullName = sPath & (Left(MyString, StringLength - 10))

ElseIf StringLength = 14 Then
strDrawingFullName = sPath & (Left(MyString, StringLength - 11))

ElseIf StringLength = 15 Then
strDrawingFullName = sPath & (Left(MyString, StringLength - 12))
End If
Debug.Print " Value from original code = " & strDrawingFullName &
" for Len(MyString) = " & CStr(Len(MyString))
strDrawingFullName = sPath & (Left(MyString, 3))
' ThisDrawing.SaveAs strDrawingFullName, acR15_dxf
Debug.Print " Value from suggested code = " & strDrawingFullName

Next i
'SET FILEDIA TO "1"
'sysVarName = "FILEDIA"
ThisDrawing.SetVariable "FILEDIA", 1

' ThisDrawing.Close
End Sub

{code}

To exactly match your requirement with the code suggested by mp, you
could use:

{code}
If StringLength > 7 And StringLength < 16 Then
strDrawingFullName = sPath & (Left(MyString, 3))
Else
MsgBox "Non viable file name length found"
End If


{code}
Regards,


Laurie Comerford


XIJIANGWOO wrote:
> yes, but hoow to i shorten the code as opposed to writing the same statement with different values?
0 Likes
Message 5 of 5

Anonymous
Not applicable
Thanks Guys. I was thinking too far out of the box.
Thanks for all of the help.
0 Likes