Did not have any luck, despite playing with syntax, getting the already existing excel object (the getobject ("","excel.application") line), so..save it all, close workbook and excel, create a new excel object with an arbitrarily different variable set, I left the row integer stuff out as that's always 1, call Reorder... still type mismatch. xlws does not seem to be set to anything if I break and hover over it.
I know Set xlwb = xlapp.Workbooks.Open(bomname) works as I can set excel to visible and it pops up again. I'm really hoping someone other than poor Justin has a look at this... but that doesn't seem to be a reality!
Public Sub LMExport()
'make a bunch of variables for file naming.
Dim temp As String
Dim temper As String
Dim basefilename As String
Dim FullFileName As String
Dim prefix As String
Dim prefixer As String
Dim bomname As String
' This code runs on assembly documents only
Dim oDoc As AssemblyDocument
Set oDoc = ThisApplication.ActiveDocument
' Set a reference to the BOM
Dim oBOM As BOM
Set oBOM = oDoc.ComponentDefinition.BOM
' Set the structured view to all levels
oBOM.StructuredViewFirstLevelOnly = False
' Set structured view
oBOM.StructuredViewEnabled = True
' Set ref to Structured BOM View
Dim oStructuredBOMView As BOMView
Set oStructuredBOMView = oBOM.BOMViews.Item("Structured")
' Export the BOM view to an Excel file
prefix = oDoc.FullDocumentName
temper = Right$(prefix, Len(prefix) - InStrRev(prefix, "\"))
prefixer = Left(temper, Len(temper) - 4)
FullFileName = Left(oDoc.FullDocumentName, Len(oDoc.FullDocumentName) - 3) & "iam"
temp = Right$(FullFileName, Len(FullFileName) - InStrRev(FullFileName, "\"))
basefilename = Left$(temp, InStrRev(temp, ".") - 1)
'bomname = "C:\Users\Lean6\Google Drive\LeanMachineRev2-179269\MPS_Files_\" & basefilename & ".xls"
bomname = "C:\Temp\" & basefilename & ".xls"
oStructuredBOMView.Export bomname, kMicrosoftExcelFormat
'add first column
'On Error Resume Next
Set excel_app = CreateObject("Excel.Application")
excel_app.Visible = True
excel_app.Workbooks.Open (bomname)
Dim rows As Integer
Dim rowbottom As String
rows = excel_app.ActiveSheet.UsedRange.rows.Count
rowbottom = "A1" & ":" & "A" & rows
excel_app.Range("A1").EntireColumn.Insert
excel_app.Range(rowbottom) = "=TRIM(LEFT(SUBSTITUTE(MID(CELL(""filename"",A1),FIND(""["",CELL(""filename"",A1))+1,255),"".xl"",REPT("" "",255)),255))"
excel_app.ActiveWorkbook.Save
excel_app.ActiveWorkbook.Close
excel_app.Quit
Set excel_app = Nothing
Dim xlwb As Workbook
Dim xlws As WorkSheet
Set xlapp = CreateObject("Excel.Application")
xlapp.Visible = True
Set xlwb = xlapp.Workbooks.Open(bomname)
Set xlws = xlwb.Worksheets("Sheet1")
Call ReorderXLBOM(xlapp, xlws)
xlapp.ActiveWorkbook.Save
xlapp.ActiveWorkbook.Close
xlapp.Quit
Set xlapp = Nothing
End Sub
Private Sub ReorderXLBOM(ByVal xlapp As Application, ByVal xlws As WorkSheet)
Dim ndx As Integer
Dim Found As Range
Dim counter As Integer
counter = 1
arrColOrder = Array("Order", "Item", "Part Number", "BOM Structure", "QTY", "Assembly Qty", "A1 Router", "A1 Time", "B1 Laser", "B1 Time", "C1 Saw-AL", "C1 Time", "D1 Saw-ST", "D1 Time", "E1 C-Axis", "E1 Time", "F1 Machining", "F1 Time", "G1 Debur", "G1 Time", "H1 Brake", "H1 Time", "I1 Weld", "I1 Time", "J1 RWeld", "J1 Time", "K1 Assembly", "K1 Time", "L1 Outsource", "L1 Time", "M1 Ship", "Bulk", "Setup")
On Error Resume Next
For ndx = LBound(arrColOrder) To UBound(arrColOrder)
Set Found = xlws.rows("1:1").Find(arrColOrder(ndx), , LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
If Err.Number <> 0 Then
MsgBox ("Error With Excel FIND function: " & Err.Number & " :: " & Err.Description & vbLf & vbLf & ndx)
Err.Clear
End If
If Not Found Is Nothing Then
If Found.Column <> counter Then
Found.EntireColumn.Cut
xlws.Columns(counter).Insert (-4161)
excelapp.CutCopyMode = False
End If
counter = counter + 1
End If
Next
If Err.Number <> 0 Then
MsgBox ("Reorder Columns Rule Error: " & Err.Number & " :: " & Err.Description & vbLf & vbLf & ndx)
Err.Clear
End If
End Sub
