Inventor VBA UserForm to open a file - then close after action

Inventor VBA UserForm to open a file - then close after action

andrew_canfield
Collaborator Collaborator
3,002 Views
11 Replies
Message 1 of 12

Inventor VBA UserForm to open a file - then close after action

andrew_canfield
Collaborator
Collaborator

Hello

The API help & iLogic examples show Inventor with files already open - how can you open files?

I'd like a user form where the file path can be entered & then inventor open the file.

This is for two purposes where I've found example code, printing to pdf & shrinkwrapping.

(the folder structure here is preventing the task scheduler from running).

when the process is finished, close the file & move to the next.

Regards

Andrew

0 Likes
3,003 Views
11 Replies
Replies (11)
Message 2 of 12

chandra.shekar.g
Autodesk Support
Autodesk Support

@andrew_canfield,

 

Hoping that answers in below forum discussion would be helpful

 

https://forums.autodesk.com/t5/inventor-customization/place-vba-program-on-toolbar-ribbon/td-p/35558...

 

You can also refer below Youtube link to create user form

 

https://www.youtube.com/watch?v=5PN7lWJSobQ

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 3 of 12

andrew_canfield
Collaborator
Collaborator

Thankyou - it's not an easy learning curve!

 

Very early stages - i understand the processing should be done in the module, not the userform.

I can't pass the value from the userform to the module:

 

The final screen shot shows the message box returning the value aa from the userform when the command button is clicked. However click exit the module message box only shows " _" ww & aa are both missing?

(aa is in the userform, ww is just a copy of aa)

 

vba2.JPGvba3.JPGvba4.JPG

 

Regards

 

Andrew

0 Likes
Message 4 of 12

andrew_canfield
Collaborator
Collaborator

After some further study i've discovered passing values between the user form & the module isn't straight forward so concentrating on the userform for now.

Another discovery is having a variable = variable & looping number is difficult too (impossible?)

The idea is for the user to populate the textboxes with drawing numbers - then iterate through them.

 

open idw1, (defer updates on), print to pdf, close

next

open idw2, (defer updates on), print to pdf, close

 

What alternatives are there?

 

 

 

input1.JPGtextboxes.JPG

 

 

Regards

 

Andrew

0 Likes
Message 5 of 12

MjDeck
Autodesk
Autodesk

Instead of creating a user form, maybe you can just use the FileDialog object. That gives you a more standard interface for selecting files. If necessary, you could use a FileDialog from a form.
I attached some sample code that could go in a VBA module. It will bring up a file dialog where you can select multiple drawings.


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 6 of 12

andrew_canfield
Collaborator
Collaborator

Thankyou - I like it, however..

There's a complication - I'm trying to recreate 'task scheduler' - to print pdf's (& later create shrinkwraps ; task scheduler will not work with the data management software here).

The drawings are large & take a while to open. (opening with deferred updates on is required too).

I was hoping to process the pdf printing overnight or an unoccupied machine.

The sequence being:

Open (deferred), print pdf, close, next. 

Regards

 

Andrew

0 Likes
Message 7 of 12

chandra.shekar.g
Autodesk Support
Autodesk Support

@andrew_canfield,

 

Try below iLogic code to open drawing and closing the same after action.

Sub Main()
	Dim path = "C:\Users\Peter\Desktop\TestFolder\"

	'Searches directory and it's subdirectories for all files, which "*" stands for
	'Say for example you only want to search for jpeg files... then change "*" to "*.jpg"  
	Dim files() As String
	files = System.IO.Directory.GetFiles(path, "*.idw", System.IO.SearchOption.AllDirectories)
	 
	For Each FileName As String In files		  

	  	Dim oDoc As DrawingDocument
		oDoc = ThisApplication.Documents.Open(FileName, True) 

		oDoc.DrawingSettings.DeferUpdates = False  
		
		Publish_PDF(oDoc)
		
		Call oDoc.Close(True)
	Next
End Sub 

Sub Publish_PDF(oDoc As DrawingDocument)
	'Code to publish pdf
End Sub

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 8 of 12

andrew_canfield
Collaborator
Collaborator

Thankyou - would this run in iLogic?

 

fileselect.JPG

0 Likes
Message 9 of 12

MjDeck
Autodesk
Autodesk

@andrew_canfield , if you're still getting a compile error:
Please copy and paste the full text of the iLogic rule and attach it here as a text file. Use Ctrl+A in the rule editor to select the entire text.


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 10 of 12

MjDeck
Autodesk
Autodesk

ProcessDrawingsTest.txt is VBA code. It's designed to be copied and pasted into the VBA editor.


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 11 of 12

andrew_canfield
Collaborator
Collaborator

The first part almost works!

I mean works as in the select file & file path is displayed, I can't pdf it though - only the idw holding the code gets printed - maybe sticking with vba is better?

 

oDataMedium = ThisApplication.TransientObjects.CreateDataMedium 

Dim filenames() As String
 
Dim oTextbox As Inventor.TextBox
 
    Dim oFileDlg As Inventor.FileDialog
    Call ThisApplication.CreateFileDialog(oFileDlg)
 
    oFileDlg.Filter = "Drawing Files (*.idw;*.dwg)|*.idw;*.dwg|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 Drawings"
 
    oFileDlg.MultiSelectEnabled = True
 
    ' 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.
    On Error Resume Next
    oFileDlg.ShowOpen
 
    GetFilenames = Split(oFileDlg.FileName, "|") ' if multiple names were selected, they will be separated by vertical bar characters
    
    For Each Fname In GetFilenames
    
    xxx = Fname
    
    MessageBox.Show(xxx, "Title")


''' hide everything below this line until the final NEXT & each selected file is displayed. oPrintMgr = ThisApplication.ActiveDocument.PrintManager

''it's not this doc to print as pdf but the xxx file
pdfname = "123" 'old line was'''ThisDoc.FileName(False) filePath = xxx 'old line was '''ThisDoc.Path


oPrintMgr.Printer = "Microsoft Print to PDF" oPrintMgr.AllColorsAsBlack = True oPrintMgr.Orientation = kLandscapeOrientation oPrintMgr.Scalemode = kCustomScale oPrintMgr.ScaleMode = PrintScaleModeEnum.kPrintBestFitScale oPrintMgr.PaperSize = kPaperSizeA4 oPrintMgr.PrintToFile(filePath+"\"+pdfname+".pdf") Next






0 Likes
Message 12 of 12

MjDeck
Autodesk
Autodesk

You don't have any code to open the document.
Try replacing this line:

oPrintMgr = ThisApplication.ActiveDocument.PrintManager

with this:

Dim oDoc As DrawingDocument
oDoc = ThisApplication.Documents.Open(FileName, True) 
oPrintMgr = oDoc.PrintManager




Mike Deck
Software Developer
Autodesk, Inc.

0 Likes