Using SaveFileDialog instead of FolderBrowserDialog

Using SaveFileDialog instead of FolderBrowserDialog

Eide.N
Advocate Advocate
790 Views
4 Replies
Message 1 of 5

Using SaveFileDialog instead of FolderBrowserDialog

Eide.N
Advocate
Advocate

Hello all, I am trying to convert a current rule to a save dialog from a folder dialog. I can't figure out how to pass the folder and filename from the dialog. Can someone help my tired brain? Here is my rule:

 

'Export DWG as PDF with options
	oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
	oDocument = ThisApplication.ActiveDocument
	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
	oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
	'File Name
	Dim FileName As String
	If iProperties.Value("Project", "Revision Number") <> "" Then
		FileName = ThisDoc.FileName(False) & " Rev" & iProperties.Value("Project", "Revision Number")
	Else
		FileName = ThisDoc.FileName(False)
	End If
	'File Path
	Dim FilePath As String = ThisDoc.Path
	
'Old: Dialog for choosing folder path
'New: Dialog for saving file
	Dim Dialog As New SaveFileDialog()  
	'Dialog.SelectedPath = FilePath
	'Dialog.Description = "Click OK for Current Location" & vbLf & " or " & vbLf & " Select New File Location and Click OK"
	Dialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*"
	Dialog.DefaultExt = "pdf"
    Dialog.FilterIndex = 1
	Dialog.InitialDirectory = FilePath
	Dialog.FileName = FileName
	 
	'If Dialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
	If Dialog.ShowDialog() = DialogResult.OK Then
		'FilePath = Dialog.SelectedPath
		FileName = Dialog.FileName

	Else 
		Exit Sub
	End If
	
'Chosen Path and Filename
	oDataMedium.FileName = FilePath & "\" & FileName & ".pdf"

'Write New File
	Try
		oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
		ThisDoc.Launch(FilePath)
	Catch
		MessageBox.Show("PDF not created. Please save DWG and/or close any open PDFs.", "NO PDF FOR YOU!")
	End Try
	Exit Sub
0 Likes
Accepted solutions (2)
791 Views
4 Replies
Replies (4)
Message 2 of 5

frederic.vandenplas
Collaborator
Collaborator
Accepted solution

Hi, i hope that this is what you are looking for (note that i don't need to add the extensions so i commented them)

At the end the resulting pdf is saved and then opened which proofs i can pass the filename.

As best tip i can give is to check your variables with a messagebox or the possibility in Inventor 2019.XXX to use the this iLogic Log which new in inventor

 

'Export DWG as PDF with options
	oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
	oDocument = ThisApplication.ActiveDocument
	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
	oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
	'File Name
	Dim FileName As String
	If iProperties.Value("Project", "Revision Number") <> "" Then
		FileName = ThisDoc.FileName(False) & " Rev" & iProperties.Value("Project", "Revision Number")
	Else
		FileName = ThisDoc.FileName(False)
	End If
	'File Path
	Dim FilePath As String = ThisDoc.Path
	
	Dim Dialog As New SaveFileDialog()  
	Dialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*"
	Dialog.FilterIndex = 1
	
	Dialog.InitialDirectory = FilePath
	Dialog.FileName = FileName
	
	If Dialog.ShowDialog() = DialogResult.OK Then
				
		FileName = Dialog.FileName

	Else 
		Exit Sub
	End If
	
'Chosen Path and Filename
	oDataMedium.FileName = FileName '& ".pdf"

'Write New File
	Try
		oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
		ThisDoc.Launch(FileName) '& ".pdf"
	Catch
		MessageBox.Show("PDF not created. Please save DWG and/or close any open PDFs.", "NO PDF FOR YOU!")
	End Try
	Exit Sub

 

If you think this answer fullfilled your needs, improved your knowledge or leads to a solution,
please feel free to "kudos"
Message 3 of 5

Eide.N
Advocate
Advocate

Frederic,

Thanks for your help! My brain was too tired to see the simple solution 🙂

Message 4 of 5

Eide.N
Advocate
Advocate
Accepted solution

I changed it a little to open the folder after creating instead of the file - I usually do something with the file other than just view/print it. Here is my final version:

 

'Export DWG as PDF with options
	oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
	oDocument = ThisApplication.ActiveDocument
	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
	oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

'Set File Name
	Dim FileName As String
	If iProperties.Value("Project", "Revision Number") <> "" Then
		FileName = ThisDoc.FileName(False) & " Rev" & iProperties.Value("Project", "Revision Number")
	Else
		FileName = ThisDoc.FileName(False)
	End If

'Get Current File Path
	Dim FilePath As String = ThisDoc.Path

'File Dialog
	Dim Dialog As New SaveFileDialog()  
	Dialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*"
	Dialog.FilterIndex = 1
	
	Dialog.InitialDirectory = FilePath
	Dialog.FileName = FileName
	
	If Dialog.ShowDialog() = DialogResult.OK Then
'Set File and Path from Dialog				
		FileName = Dialog.FileName
		FilePath = System.IO.Path.GetDirectoryName(Dialog.FileName)
	Else 
		Exit Sub
	End If
	
'Set Chosen Path and Filename
	oDataMedium.FileName = FileName

'Write New File
	Try
		oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
'Open File or Folder - Comment whichever is not wanted
		'ThisDoc.Launch(FileName) 
		ThisDoc.Launch(FilePath)
	Catch
		MessageBox.Show("PDF not created. Please save DWG and/or close any open PDFs.", "NO PDF FOR YOU!")
	End Try
	Exit Sub
0 Likes
Message 5 of 5

Eide.N
Advocate
Advocate

I changed it a little to open the folder after creating instead of the file - I usually do something with the file other than just view/print it. Here is my final version:

 

'Export DWG as PDF with options
	oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
	oDocument = ThisApplication.ActiveDocument
	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
	oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

'Set File Name
	Dim FileName As String
	If iProperties.Value("Project", "Revision Number") <> "" Then
		FileName = ThisDoc.FileName(False) & " Rev" & iProperties.Value("Project", "Revision Number")
	Else
		FileName = ThisDoc.FileName(False)
	End If

'Get Current File Path
	Dim FilePath As String = ThisDoc.Path

'File Dialog
	Dim Dialog As New SaveFileDialog()  
	Dialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*"
	Dialog.FilterIndex = 1
	
	Dialog.InitialDirectory = FilePath
	Dialog.FileName = FileName
	
	If Dialog.ShowDialog() = DialogResult.OK Then
'Set File and Path from Dialog				
		FileName = Dialog.FileName
		FilePath = System.IO.Path.GetDirectoryName(Dialog.FileName)
	Else 
		Exit Sub
	End If
	
'Set Chosen Path and Filename
	oDataMedium.FileName = FileName

'Write New File
	Try
		oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
'Open File or Folder - Comment whichever is not wanted
		'ThisDoc.Launch(FileName) 
		ThisDoc.Launch(FilePath)
	Catch
		MessageBox.Show("PDF not created. Please save DWG and/or close any open PDFs.", "NO PDF FOR YOU!")
	End Try
	Exit Sub

 

0 Likes