社区
Inventor 产品技术应用讨论区
欢迎访问欧特克Inventor论坛!分享知识,发帖提问,浏览Inventor热帖
取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

分享一个将Inventor BOM表分类导出Excel的iLogic代码

9 条回复9
已解决
回复
1 条消息(共 10 条)
chenj
4032 次查看, 9 条回复

分享一个将Inventor BOM表分类导出Excel的iLogic代码

网上找到的一段将Inventor BOM表分类导出Excel的iLogic代码,在运行规则前,确认BOM表启用了"仅零件"的BOM表视图。iLogic代码如下:

Option Explicit On
AddReference "microsoft.office.interop.excel.dll"
Imports XL = Microsoft.Office.Interop.Excel

Class BOMExport
'将字符串修改为对应的语言版本
Private Const sBOMStructureType As String = "BOM 表结构" 'BOM 表结构列字段名称
Private Const sNormal As String = "普通件"
Private Const sPurchased As String = "外购件"
Private Const sInseparable As String = "不可拆分件"

Private Const sPath As String = "C:\Temp\" 'BOM 导出路径
Private Const sFilename As String = "仅零件_" '文件名的静态部分 

Private Sub Main()

'宏:
'- 装配打开中
'- 仅零件视图中不排除“零件类型”列

Dim oApp As Inventor.Application = ThisApplication
Dim oAssdoc As AssemblyDocument = oApp.ActiveDocument
Dim oBOM As BOM = oAssdoc.ComponentDefinition.BOM
Dim oBOMview As BOMView
Dim oExcelApp As XL.Application = GetObject("", "Excel.Application")

If oExcelApp Is Nothing Then
MsgBox ("无法获取 Excel")
Exit Sub
End If

oBOM.PartsOnlyViewEnabled = True

For Each oBOMview In oBOM.BOMViews
If oBOMview.ViewType = kPartsOnlyBOMViewType Then
Exit For
End If
Next

If oBOMview Is Nothing Then
MsgBox ("无法获取BOM仅零件视图")
Exit Sub
End If

If Dir(sPath & sFilename & sNormal & ".xls") <> "" Then
Call MsgBox("文件 " & sPath & sFilename & sNormal & ".xls 已存在.", vbCritical, "导出BOM")
oExcelApp.Quit
Exit Sub
ElseIf Dir(sPath & sFilename & sPurchased & ".xls") <> "" Then
Call MsgBox("文件 " & sPath & sFilename & sNormal & ".xls 已存在.", vbCritical, "导出BOM")
oExcelApp.Quit
Exit Sub
ElseIf Dir(sPath & sFilename & sInseparable & ".xls") <> "" Then
Call MsgBox("文件 " & sPath & sFilename & sNormal & ".xls 已存在.", vbCritical, "导出BOM")
oExcelApp.Quit
Exit Sub
End If

Call oBOMview.Export(sPath & sFilename & sNormal & ".xls", kMicrosoftExcelFormat, sNormal)
Call oBOMview.Export(sPath & sFilename & sPurchased & ".xls", kMicrosoftExcelFormat, sPurchased)
Call oBOMview.Export(sPath & sFilename & sInseparable & ".xls", kMicrosoftExcelFormat, sInseparable)

Dim oWB As xl.Workbook = oExcelApp.Workbooks.Open(sPath & sFilename & sNormal & ".xls")
If Not oWB Is Nothing Then
oWB = Filter(oExcelApp, oWB, sNormal)
If Not oWB Is Nothing Then
oWB.Save
End If
End If

Dim oWB2 As xl.Workbook = oExcelApp.Workbooks.Open(sPath & sFilename & sPurchased & ".xls")
If Not oWB2 Is Nothing Then
oWB2 = Filter(oExcelApp, oWB2, sPurchased)
If Not oWB2 Is Nothing Then
oWB2.Save
End If
End If

Dim oWB3 As xl.Workbook = oExcelApp.Workbooks.Open(sPath & sFilename & sInseparable & ".xls")
If Not oWB3 Is Nothing Then
oWB3 = Filter(oExcelApp, oWB3, sInseparable)
If Not oWB3 Is Nothing Then
oWB3.Save
End If
End If

Dim Result As MsgBoxResult
Result = MsgBox("导出完成. 是否查看excel?", vbYesNo, "导出BOM")

If Result = vbYes Then
oExcelApp.Visible = True
Else
For Each oWB In oExcelApp.Workbooks
oWB.Close (False)
Next
oExcelApp.Quit
End If

End Sub


Private Function Filter(ByVal oExcelApp As xl.Application , ByVal oWB As xl.Workbook, ByVal sName As String) As xl.Workbook
Dim oRange As xl.Range
Dim lLastRow As Long
Dim t As Long
oRange=oExcelApp.Cells(oWB.ActiveSheet.Rows.Count, 1)
lLastRow = oRange.End(xl.XlDirection.xlUp ).Rows.Row

Dim s As Long
s = FindColumn(oExcelApp, oWB)
If s = 0 Then
Call MsgBox("未找到BOM表结构列.", vbCritical, "导出BOM")
Filter = Nothing
Exit Function
End If

For t = lLastRow To 2 Step -1
If Not oExcelApp.Cells(t, s).Value = sName Then
Call oWB.ActiveSheet.Rows(t).Delete( Shift:=xl.XlDirection.xlUp)
End If
Next t

Return oWB
End Function


Private Function FindColumn(ByVal oExcelApp As xl.Application , ByVal oWB As xl.Workbook) As Long
Dim oRange As xl.Range
Dim lLastColumn As Long
oRange=oExcelApp.Cells(1, oWB.ActiveSheet.Columns.Count)
lLastColumn = oRange.End(xl.XlDirection.xlToLeft).Columns.Column

Dim s As Long
Dim rCells As xl.Range
For s = lLastColumn To 1 Step -1
rCells = oExcelApp.Cells(1, s)
If rCells.Value = sBOMStructureType Then
Return s
Exit For
End If
Next s
End Function

End Class

标记 (1)
9 条回复9
2 条消息(共 10 条)
lynn_zhang
回复: chenj

感谢陈老师的分享!已置顶。





Lynn Zhang
Community Manager


3 条消息(共 10 条)

陈总也在 “Inventor俱乐部” QQ群?


If my post answers your question, please click the "Accept as Solution" button. This helps everyone find answers more quickly!
如果我的回帖解决了您的问题,请点击 "接受为解决方案" 按钮. 这可以帮助其他人更快的找到解决方案!


王 承之
Autodesk AGN [Inventor 俱乐部] Leader
Inventor Club | Bilibili


AGN L    EESignature

4 条消息(共 10 条)
858456055
回复: chenj

大哥其实这是我从api说明里搞来的!

设计之上!
5 条消息(共 10 条)

我的一用报59行错误

6 条消息(共 10 条)

自己思考哪里出了问题,59之上添加这句  on error resume next然后再试试

设计之上!
7 条消息(共 10 条)

陈总莫不是陈伯雄老师?另外向群主大人问好!

标记 (1)
8 条消息(共 10 条)

不是,这个陈老师也很厉害

设计之上!
9 条消息(共 10 条)
442780782
回复: chenj

代码报错01.png

运行代码报错。

10 条消息(共 10 条)
858456055
回复: chenj

转接一下试试!dim aa=kpartsonlybomviewtype,obomview.viewtype=aa

设计之上!

找不到想要的内容?向社区提问或分享您的知识。

到论坛发帖  

”