FolderBrowserDialog() not working with vb.net

FolderBrowserDialog() not working with vb.net

mindaV3J2J
Enthusiast Enthusiast
774 Views
4 Replies
Message 1 of 5

FolderBrowserDialog() not working with vb.net

mindaV3J2J
Enthusiast
Enthusiast

I get that this function is undefined, although it should enable me to pick the folder I need. Here's the code I'm using to do so.

 

Private Function BrowseForFolder(prompt As String) As String
Using dialog As New FolderBrowserDialog()
dialog.Description = prompt
If dialog.ShowDialog() = DialogResult.OK Then
Return dialog.SelectedPath
End If
Return String.Empty
End Using
End Function

 

How can I fix this?

0 Likes
775 Views
4 Replies
Replies (4)
Message 2 of 5

WCrihfield
Mentor
Mentor

Hi @mindaV3J2J.  It may simply be due to the lack of an Imports statement in your rule's header for the System.Windows.Forms resource, because it may not recognize the object types.  This slightly edited version of your code works just fine for me in a regular iLogic rule without any Imports statements.

Sub Main
	Dim sFolder As String = BrowseForFolder("Pick A Folder")
	MsgBox("sFolder = " & sFolder,,"")
End Sub

Private Function BrowseForFolder(prompt As String) As String
	Using dialog As New System.Windows.Forms.FolderBrowserDialog()
		dialog.Description = prompt
		If dialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
			Return dialog.SelectedPath
		End If
		Return String.Empty
	End Using
End Function

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 5

mindaV3J2J
Enthusiast
Enthusiast

that's not working for me unfortunately. Am I missing a reference maybe? I added the one that allowed me to use the inventor API, but no luck with this.

0 Likes
Message 4 of 5

mindaV3J2J
Enthusiast
Enthusiast


Additionally, it cannot find the System.Windows.Forms library. How can I get around this?

0 Likes
Message 5 of 5

mindaV3J2J
Enthusiast
Enthusiast

heres my code as well.

 

Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Runtime.InteropServices.ComTypes
Imports System.Windows.Forms
Imports Inventor

Namespace d

Module Module1

Sub Main()
Dim invApp As Inventor.Application = GetInventorInstance()
If invApp Is Nothing Then
' MessageBox.Show("Autodesk Inventor is not running.")
Return
End If

Dim oDoc As Document = invApp.ActiveDocument
Dim folderPath As String = BrowseForFolder("Please select an input folder")
If String.IsNullOrEmpty(folderPath) Then
Return
End If

Dim oFSO As Object = CreateObject("Scripting.FileSystemObject")
Dim oFolder As Object = oFSO.GetFolder(folderPath)

Dim setFilePath As String = BrowseForFolder("Please select an output folder")
If String.IsNullOrEmpty(setFilePath) Then
Return
End If

For Each oFile As Object In oFolder.Files
If LCase(oFSO.GetExtensionName(oFile.Name)) = "iam" Then
Dim sFilename As String = folderPath & "\" & oFile.Name
Dim oAsmDoc As AssemblyDocument = invApp.Documents.Open(sFilename, False)

' MessageBox.Show(oAsmDoc.FullFileName)

Dim oRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments
For Each oRefDoc As Document In oRefDocs
If oRefDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
Dim oSheetMetDoc As PartDocument = invApp.Documents.Open(oRefDoc.FullFileName, True)
Dim oCompDef As SheetMetalComponentDefinition = oSheetMetDoc.ComponentDefinition
Dim oDef As FlatPattern = oCompDef.FlatPattern

If oDef Is Nothing Then GoTo NextOcc

Dim sOut As String = "FLAT PATTERN DXF?AcadVersion=2018" &
"&OuterProfileLayer=IV_INTERIOR_PROFILES" &
"&InvisibleLayers=IV_TANGENT;IV_ROLL_TANGENT"

Dim prevName As String = oSheetMetDoc.DisplayName
Dim arr() As String = prevName.Split("."c)
prevName = arr(0)

Dim sFname As String = setFilePath & "\" & prevName & ".dxf"

Dim oDataIO As DataIO = oCompDef.DataIO
oDataIO.WriteDataToFile(sOut, sFname)
oCompDef.FlatPattern.ExitEdit()
End If

NextOcc:
Next

If oDoc IsNot oAsmDoc Then
oAsmDoc.Close(True) ' True = skip save
End If
End If
Next

' MessageBox.Show("Flat patterns exported to " & setFilePath)
End Sub

Private Function GetInventorInstance() As Inventor.Application
Try
Return DirectCast(Marshal.GetActiveObject("Inventor.Application"), Inventor.Application)
Catch ex As Exception
Return Nothing
End Try
End Function

Private Function BrowseForFolder(prompt As String) As String
Using dialog As New FolderBrowserDialog()
dialog.Description = prompt
If dialog.ShowDialog() = DialogResult.OK Then
Return dialog.SelectedPath
End If
Return String.Empty
End Using
End Function
End Module
End Namespace

0 Likes