Message 1 of 5
Set Save File Dialog Initial Directory

Not applicable
10-13-2015
05:31 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I need a code that Opens a selected Part From an Assembly and so a saves a copy of this new part. Code to open file works good.
i have a assembly in subfolder of Workspace. I Set the initial directory for Save Dialog to this sub folder, but it always opens the workspace Folder. But the same thing works for OpenFile Dialog. What is wrong with my code.
for eg Suppose C:\_work\Plant_14\Slide\BearingHousing.iam is the FIlepath for assembly, where "_work" is the workspace folder. FileDialog initial directory is "_work" whereas i want "C:\_work\Plant_14\Slide\"
i Work in Inventor 2010 SP4 64Bit Edition.
Thanks
Dim DocFilePath As String Sub Open_Selected() Dim Doc As Document Dim obj As Object Dim ObjFullfileName As String Dim DocType As DocumentTypeEnum Dim eLocType As LocationTypeEnum Dim DocFullfileName As String 'Get the active document. Set Doc = ThisApplication.ActiveDocument On Error Resume Next DocFullfileName = Doc.Fullfilename Dim CompFileNameOnly As String 'get the location of the last backslash Dim index As Integer index = InStrRev(DocFullfileName, "\") DocFilePath = Left(DocFullfileName, index - 1) 'Check if any file is open. if nothing is open exit macro. Set oDef = Doc.ComponentDefinition If Err Then ' Unable to get the Parameters object, so exit. MsgBox "Unable to access the parameters. An assembly must be Open." Exit Sub End If On Error GoTo 0 'Get Document Type and check wheter it is a part or assembly file, else exit Macro DocType = Doc.DocumentType If DocType = kAssemblyDocumentObject Then 'Pass Else MsgBox "File Type Not Compatible. Please Use an Assembly File" Exit Sub End If On Error Resume Next Set obj = Doc.SelectSet.Item(1) If Err Then MsgBox "Select Something in an assembly" Else If (TypeOf obj Is ComponentOccurrence) Then Dim occ As ComponentOccurrence Set occ = obj ObjFullfileName = occ.Definition.Document.Fullfilename End If Set oDoc = ThisApplication.Documents.Open(ObjFullfileName, True) oDoc.Activate End If TestFileDialog End Sub Public Sub TestFileDialog() ' Set a reference to the FileLocations object. 'Dim oFileLocations As FileLocations 'Set oFileLocations = ThisApplication.FileLocations 'Dim sPath As String 'Debug.Print "Workspace: " & oFileLocations.Workspace ' Display the workspace. 'sPath = oFileLocations.Workspace ' Create a new FileDialog object. Dim oFileDlg As FileDialog Call ThisApplication.CreateFileDialog(oFileDlg) ' Define the filter to select part and assembly files or any file. oFileDlg.Filter = "Inventor Files (*.iam;*.ipt)|*.iam;*.ipt|All Files (*.*)|*.*" ' Define the part and assembly files filter to be the default filter. oFileDlg.FilterIndex = 1 ' Set the title for the dialog. oFileDlg.DialogTitle = "Open File Test" ' Set the initial directory that will be displayed in the dialog. oFileDlg.InitialDirectory = DocFilePath 'sPath '"C:\Temp" ' Set the flag so an error will be raised if the user clicks the Cancel button. oFileDlg.CancelError = True ' Show the open dialog. The same procedure is also used for the Save dialog. ' The commented code can be used for the Save dialog. On Error Resume Next 'oFileDlg.ShowOpen oFileDlg.ShowSave ' If an error was raised, the user clicked cancel, otherwise display the filename. If Err Then MsgBox "User cancelled out of dialog" ElseIf oFileDlg.filename <> "" Then MsgBox "File " & oFileDlg.filename & " was selected." End If End Sub Public Sub GetLocation() Dim oDoc As Document Set oDoc = ThisApplication.ActiveDocument Dim oProjectFile As FileLocations Set oProjectFile = ThisApplication.FileLocations Dim strRepositoryName As String Dim eLocType As LocationTypeEnum Call oProjectFile.FindInLocations(oDoc.Fullfilename, strRepositoryName, eLocType) If eLocType = kWorkspaceLocation Then Dim strWorkspace As String strWorkspace = oProjectFile.Workspace MsgBox "Part is in Workspace:" & " Path=" & strWorkspace End If If eLocType = kLibraryLocation Then ' Get the list of library paths. Dim strLibraryNames() As String Dim strLibraryPaths() As String Dim iNumLibraries As Long Call oProjectFile.Libraries(iNumLibraries, strLibraryNames, strLibraryPaths) If iNumLibraries > 0 Then ' Iterate through the list of Libraries. The array is filled ' zero based, so the iteration begins a zero. For i = 0 To iNumLibraries - 1 If strRepositoryName = strLibraryNames(i) Then MsgBox "Part is in Library:" & " Name=" & strLibraryNames(i) & ", Path=" & strLibraryPaths(i) End If Next End If End If End Sub