By Adam Nagy
When reading e.g. an embedded Excel file then you can simply use the Excel API to interact with the document. However, if it's e.g. a text file then the default text editor application will be brought up (usually Notepad) and you have no programmatic access to the text file content.
1) The easiest solution would probably be to use a linked text file instead of an embedded one, but it might not be an option.
2) Since Inventor documents are using Structured Storage you might be able to use that API to access the text file. I found this info which could be useful for this. There is also a cool program that enables you to see what's inside the documents called Structured Storage Viewer and this is what a document would look like in it - highlighted in red is the content of the embedded text file:
One issue I found though is that the embedded object's name that you find in the Inventor Model Browser might not be in sync with the name used inside the Structured Storage.
We also have this article on using Structured Storage to store information in Inventor documents, which could also come handy.
A colleague also pointed out this article about using this API from .NET: http://www.developerfusion.com/article/84406/com-structured-storage-from-net/ and this project: http://sourceforge.net/projects/openmcdf/
3) Another option is to hook into the edit mechanism of the embedded file. We'll register our program in the registry as the editor for txt files so that will be called with the path of the temporary txt file that Inventor creates when the document with the embedded text file is opened. Then we edit the file, and when our program exits the modified txt file content will be stored in the Inventor document. I checked and if I modified the txt file after my app closed, then those changes did not get saved into the Inventor document.
For this solution I created a sample project that can be used to modify an embedded text file programmatically: https://github.com/adamenagy/Inventor-ReadWriteEmbeddedFile
Once the program is compiled you can use it like this from VBA:
Sub ModifyEmbeddedTextFile() Dim doc As PartDocument Set doc = ThisApplication.ActiveDocument ' Before running this code the embedded text file ' needs to be selected in the Inventor Model Browser Dim obj As ReferencedOLEFileDescriptor Set obj = doc.SelectSet(1) ' Register our reader for text files Call Shell( _ """" & _ "C:\Temp\TextReaderCS.exe" & _ """ """ & _ "/r" & _ """", vbNormalFocus) ' Get the embedded object opened ' Note: this will use the temporary file with ' the content of the embedded object ' the path of which will be passed to the editor ' program Call obj.Activate(kShowOLEVerb, Nothing) ' Unregister our editor for text files Call Shell( _ """" & _ "C:\Temp\TextReaderCS.exe" & _ """ """ & _ "/u" & _ """", vbNormalFocus) End Sub
4) If you just want to read the file then maybe you could just have a File System monitor checking any new file creation in the temp folder, so when Inventor creates the temp files for the embedded files during the opening of the Inventor document then you could be notified about that and could store their paths and read them if needed: System.IO.FileSystemWatcher
If you have multiple embedded documents then it might not be easy to figure out which temp file is for which one.