Get Embedded (non-linked) Attachments

Hi all,

I'm working with parts that will have one or more attachments in the design binder. These will be embedded and not linked. They will be either Excel files or text files. They have been added with the following
myPart.Extension.InsertAttachment(filePath, false);
I'm struggling to find a way to read them with either the OLEObject or GetAttachments approach for a few reason. As they are embedded I cannot use the file path generate by GetAttachments as there is nothing there to open. Equally, for an embedded attachment swOleObj.FileName returns null which means I wouldn't know which attachment is which.

My thought was to use the filepath from GetAttachments to form a search string for SelectByID2. So if I got c:\\myfile.txt returned from an entry in the GetAttachments array I would then trim the string for use the in following

if (myPart.Extension.SelectByID2("myfile.txt", "EMBEDLINKDOC", 0, 0, 0, False, 0, Nothing, 0))
{
swOleObj = swSelMgr.GetSelectedObject6(1, 0);
}

This way I would know what OLEObject I have and will be able to use .SetActivate to begin reading it. The trouble is that SelectByID2 returns false. When I used the macro recorded to select myfile.txt in the Design Binder the SelectByID2 line was identical to the line above.  Oddly, even replaying the recorded macro wouldn't select myfile.txt.
Can anyone offer any suggestions or tips? I'm open to alternative methods, I just want to read an embedded attachment and know which attachment it is. I've searched the forum and found a similar question that wasn't answered. I've seen a few posts regarding OLEObjects as well but none seem to cover what to do when embedded. I'm working in C# but generally language agnostic.

SolidworksApi/macros
Answers 1
JC
JC 2021-04-08 Best Answer
Function ExtractFile(ByVal swoleobj As SwOLEObject, ByVal SaveToLoc As String) As Boolean<br>        Try<br>            If swoleobj.IsLinked = False Then Debug.Print("Document is not linked")<br><br>            Dim bufferdata As Object = swoleobj.Buffer<br>            Dim buffersize = swoleobj.BufferSize<br>            Dim sbyteArray(UBound(bufferdata)) As SByte<br>            Dim ext As String = ".docx"<br>            Dim documentTypeClassID As String = swoleobj.Clsid<br>            Dim buffarray(UBound(bufferdata)) As Byte<br>            Dim textstart As Integer = 2182<br>            Debug.Print("Document type class id: " & swoleobj.Clsid)<br><br>            Select Case swoleobj.Clsid.ToUpper<br>                Case "{00020906-0000-0000-C000-000000000046}"<br>                    ext = ".doc"<br>                Case "{F4754C9B-64F5-4B40-8AF4-679732AC0607}"<br>                    ext = ".docx"<br>                Case "{0003000C-0000-0000-C000-000000000046}"<br>                    ext = ".txt"<br>            End Select<br>            Try<br>                Array.Copy(bufferdata, sbyteArray, buffarray.Length)<br>            Catch ex As Exception<br><br>            End Try<br>            Dim txt As String = ""<br>            Dim fs As FileStream = New FileStream(SaveToLoc & ".zip", FileMode.OpenOrCreate)<br>            Using bw = New BinaryWriter(fs)<br>                For i = 0 To UBound(sbyteArray)<br>                    bw.Write(sbyteArray(i))<br>                Next<br>                fs.Flush()<br>                fs.Dispose()<br>                fs.Close()<br>            End Using<br>            Return True<br><br>        Catch ex As Exception<br><br>        End Try<br>    End Function‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍<span class="line-numbers-rows"><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span></span>


Here is the code to extract the binary data from the swoleObject.

the result is some sort of packaged file that i am not quite sure of.

inside the files, they mention OLE 10 Native and also olepress. these are structured storage types from info on the google.


take the output "ZIP" files and view them with 7zip.

if you discover the final code pieces to this, please post it as this will make it possible to extract embedded files in parts for all. 


Jacob Corder
Cadsharp LLC