ds-blue-logo
Preview  |  SOLIDWORKS USER FORUM
Use your SOLIDWORKS ID or 3DEXPERIENCE ID to log in.
ASAndrew Stuart29/10/2010

Solidworks crashes after using the following function.

This occurs when a drawing is already open.  I am using this function to browse to ".txt" documents for defaults used in my macro.  A single use of this dialog might be OK but when used twice or more it definately causes a crash after the macro has finished running.  It is not always immediate but it crashes within 30 seconds or when I close the current Drawing Document.  Any ideas?

Private Function funOpenFile(ByRef PathFile As String, ByVal sFileExtension As String) As Boolean

    Dim PathOnly As String = PathFile

    ' Test supplied initial path

    If Trim(PathOnly).Length <> 0 Then

        ' Test if path or file

        Dim fa As System.IO.FileAttributes = System.IO.File.GetAttributes(PathOnly)

        If (fa And FileAttributes.Directory) <> FileAttributes.Directory Then

          PathOnly = System.IO.Path.GetDirectoryName(PathOnly)

        End If

        fa = Nothing

   

        ' Test if the path exists

        If Not Directory.Exists(System.IO.Path.GetDirectoryName(PathOnly)) Then

           PathOnly = System.Environment.GetFolderPath( _

                      System.Environment.SpecialFolder.Desktop)

        End If

    Else

        PathOnly = System.Environment.GetFolderPath( _

                   System.Environment.SpecialFolder.Desktop)

    End If

    ' Show OpenFileDialog

    Dim OpenFileDialog1 As New OpenFileDialog

    OpenFileDialog1.RestoreDirectory = True

    With OpenFileDialog1

        .InitialDirectory = PathOnly

        .Multiselect = False

       .Title = "Select Preferance File"

        .Filter = "Preferance file (*" & sFileExtension & ")|*" & sFileExtension

        .FilterIndex = 1

        .ReadOnlyChecked = True

    End With

    ' Check to make sure the user selected a file and didn't click cancel.

    If OpenFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.OK Then

        If String.Compare(System.IO.Path.GetExtension( _

                OpenFileDialog1.FileName), sFileExtension, ignoreCase:=True) = 0 Then

            PathFile = OpenFileDialog1.FileName

            Return True

        Else

            Return False

        End If

   Else

        Return False

    End If



End Function