ilogic change source of embeded excel sheet

ilogic change source of embeded excel sheet

Anonymous
Not applicable
1,978 Views
7 Replies
Message 1 of 8

ilogic change source of embeded excel sheet

Anonymous
Not applicable
I have an assembly that contain an embedded Excel sheet driving subassemblies and parts that contain the same source sheet. , My question is, is it possible to make an Ilogic that will find and change the source for all subassemblies and part that s contain in the top assembly
0 Likes
Accepted solutions (1)
1,979 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable

Hi,

 

Try to create an iLogic rule in your assembly file, and paste following code:

 

 

 

    Dim oAssyDoc As Inventor.AssemblyDocument = ThisDoc.Document
    
    Dim OldExcelFile As String = "C:\...\...\ExcelFile01.xlsx"
    Dim NewExcelFile As String = "C:\...\...\ExcelFile02.xlsx"
    
    Dim oDoc As Inventor.Document = Nothing
    For Each oDoc In oAssyDoc.AllReferencedDocuments
        If oDoc.DocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Or oDoc.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then
    
            'You should implement some checks here to see if file is readonly or not
    
            Dim oReferencedFile As Inventor.ReferencedOLEFileDescriptor = Nothing
            If oDoc.ReferencedOLEFileDescriptors.Count > 0 Then
                For Each oReferencedFile In oDoc.ReferencedOLEFileDescriptors
                    If oReferencedFile.FullFileName = OldExcelFile Then
                        oReferencedFile.FileDescriptor.ReplaceReference(NewExcelFile)
                        oDoc.Update2(True)
                    End If
                Next
            End If
        End If
    Next
    
    oAssyDoc.update2(True)
    
    MsgBox("Done", MsgBoxStyle.Information, "Done")

 

Make sure to enter the correct names for the old and new Excel files in the 2nd and 3rd line.

You should also add some checks to see if the file you are replacing the Excel file is actually read/write. Libraries & Vault restrictions may result in read only files.

 

Also make sure your Excel files are placed in your workspace !!!!

 

 

Message 3 of 8

Anonymous
Not applicable

Wow!!! It's working very good, for all subassemblies and parts, but doens't change Excel source for the main ass'y

But i can do with that

 

Many TKS.

0 Likes
Message 4 of 8

MechMachineMan
Advisor
Advisor
Accepted solution
Sub Main()

Dim oAssyDoc As Inventor.AssemblyDocument = ThisDoc.Document Dim OldExcelFile As String = "C:\...\...\ExcelFile01.xlsx" Dim NewExcelFile As String = "C:\...\...\ExcelFile02.xlsx"
'Handle Top level Doc
call ReplaceRefFile(oAssyDoc, OldExcelFile, NewExcelFile)


'Iterate through sub level docs. Dim oDoc As Inventor.Document = Nothing For Each oDoc In oAssyDoc.AllReferencedDocuments If oDoc.DocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Or oDoc.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then Call ReplaceRefFile(oDoc, OldExcelFile, NewExcelFile) End If Next oAssyDoc.update2(True) MsgBox("Done", MsgBoxStyle.Information, "Done")

End Sub
Sub ReplaceRefFile(oDoc As Document, OldExcelFile As String, NewExcelFile As String) Dim oReferencedFile As Inventor.ReferencedOLEFileDescriptor = Nothing If oDoc.ReferencedOLEFileDescriptors.Count > 0 Then
If oDoc.IsModifiable = True For Each oReferencedFile In oDoc.ReferencedOLEFileDescriptors If oReferencedFile.FullFileName = OldExcelFile Then oReferencedFile.FileDescriptor.ReplaceReference(NewExcelFile) oDoc.Update2(True) End If Next
Else
MsgBox(oDoc.FullFileName & vblf & "is not modifiable. References NOT replaced")
End if End If End Sub

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 5 of 8

Anonymous
Not applicable

Many Thank you, this rule doing exactly what i was looking for, i added inputboxes to enter file path and working perfect !!!

0 Likes
Message 6 of 8

Anonymous
Not applicable

With added input boxes

 

Sub Main()
   
    Dim oAssyDoc As Inventor.AssemblyDocument = ThisDoc.Document

    Dim OldExcelFile As String = InputBox("What was the full name of the old linked Excel file? (Includes file path)", "Old Linked Excel file", "")
    Dim NewExcelFile As String = InputBox("What is the full name of the new linked Excel file? (Includes file path)", "New Linked Excel file", OldExcelFile)
    
    'Handle Top level Doc
    Call ReplaceRefFile(oAssyDoc, OldExcelFile, NewExcelFile)


    'Iterate through sub level docs.
    Dim oDoc As Inventor.Document = Nothing
    For Each oDoc In oAssyDoc.AllReferencedDocuments
        If oDoc.DocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Or oDoc.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then
             Call ReplaceRefFile(oDoc, OldExcelFile, NewExcelFile)
        End If
    Next
    
    oAssyDoc.update2(True)
    
    MsgBox("Done", MsgBoxStyle.Information, "Done")

End Sub

Sub ReplaceRefFile(oDoc As Document, OldExcelFile As String, NewExcelFile As String)
            Dim oReferencedFile As Inventor.ReferencedOLEFileDescriptor = Nothing
            If oDoc.ReferencedOLEFileDescriptors.Count > 0 Then
                If oDoc.IsModifiable = True
                  For Each oReferencedFile In oDoc.ReferencedOLEFileDescriptors
                    If oReferencedFile.FullFileName = OldExcelFile Then
                        oReferencedFile.FileDescriptor.ReplaceReference(NewExcelFile)
                        oDoc.Update2(True)
                    End If
                  Next
                 Else
                     MsgBox(oDoc.FullFileName & vbLf & "is not modifiable. References NOT replaced")
                 End If
            End If
End Sub
0 Likes
Message 7 of 8

Darrell.johnson
Enthusiast
Enthusiast

Hi,

 

is it possible to change the source of Inventor-parts in a similar way?

0 Likes
Message 8 of 8

Anonymous
Not applicable

Hi Mr. lcampstejin,

I found Your reply to a question in regards to changing the Excel source to recreate an assembly or iParts using iLogic,

I liked that approach but that change is with a hardcoded location.

How can I do the same but dynamically? so I don't have to go to every time and change it manually.

I have my Assembly in a Template location and I copy everything to a new Project folder and the Excel file is retaining the original location and not the new one which it should be since the parameter values will be different.

Can you suggest me something in this matter?

I have the batch process written in visual studio 2017, but if you think that this can be done at iLogic level that also can be helpful as long as the Excel file will follow the new source folder location.

Thank you for any suggestion you may have, and I am looking forward to hearing from you.

Sincerely, Sam Lazcano

0 Likes