Tuesday, August 23, 2011

Writing to a MS Word document from Autodesk Inventor

I thought I would get into a little customization this time.  What kind of customization, you ask?  The kind that doesn't really require any experience to do.


That's right, customizing Autodesk Inventor with iLogic.  We have all seen how iLogic can be used to create some pretty incredible rules-based designs.  Well I am going to extend it and use it for something outside of Inventor.


In this example, I have to create a document that accompanies my Inventor file.  This is a simple document that contains a table with 3 pieces of information:  the File Name, the Part Number, and the Designers name.  What I want to do is create a rule that will create this document automatically from the information in the iProperties of my Inventor file.


Here's how we connect iLogic to Word: iLogic can write to Bookmarks contained within a Word document.  So my first step was to create a word file that contains bookmarks that correspond to the data I want to write.  Below is a screen shot of my doc template:
Within my Inventor file I add the following iLogic code:


'set the folder name
fol ="C:\iLogic"
'Create the Word Application Object
wordApp = Interaction.CreateObject("Word.Application")
'Add the template document to the Word Session
wordDoc = wordApp.Documents.Add (fol & "\Export_to_Bookmarks.docx", Visible:=False)
'Write to the Bookmarks in the template
wordDoc.Bookmarks("FNAME").Range.Text = ThisDoc.FileName(False) 'without extension
wordDoc.Bookmarks("PNUM").Range.Text = iProperties.Value("Project", "Part Number")
wordDoc.Bookmarks("DES").Range.Text = iProperties.Value("Summary", "Author")
'Save the Word file with a new name
wordDoc.SaveAs (fol & "\" & ThisDoc.FileName(False) & ".docx")
'Quit Word session
wordApp.Quit(0)
As you can see, we first define our folder location and then create a connection to MS Word.  We then add a document from our "template" where we put the bookmarks into it.  We then simply write out our information into the appropriate bookmarks. Lastly we save the Word document to a new name and quit MS Word.

The resulting Word document looks like this:
I realize that this is just a simple example, but you can apply this to any document you want to.  If you would like a copy of the part file and the word file I used in this example, send an email to techtalk@adraft.com and I will send them to you.

3 comments:

  1. How can I hack a SolidWorks file to change the creator/author?

    Solidworks Course

    ReplyDelete
  2. Hi,

    nice to finde my coing in the Internet :D

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete