+发表新主题
cheng3222 发布于2025-12-4 07:53 62 次浏览 3 位用户参与讨论
跳转到指定楼层
SolidWorks图号分离宏命令运行时报错
Compile error:
Can't find project or library
在编辑宏命令框内运行宏程序,提示是“CustPropMgr =”这处出错,有哪位知道怎么解决吗?程序见下
'定义solidwork
Dim a                  As Integer
Dim b                  As String
Dim m                  As String
Dim e                  As String
Dim k                  As String
Dim t                  As String
Dim c                  As String
Dim j                  As Integer
Dim strmat             As String
Dim tempvalue          As String
Dim Part               As Object
Dim swApp              As SldWorks.SldWorks
Dim swModelDoc         As SldWorks.ModelDoc2
Dim swConfig           As SldWorks.Configuration
Dim CustPropMgrAsSldWorks, CustomPropertyManager
Dim swModel            As SldWorks.ModelDoc2

Dim startIndex As Integer '定义截取项目号的开始位置

Dim endIndex As Integer  '定义截取项目号的结束位置

Dim projID  As String '定义项目号变量

Dim startstrmat As Integer '定义截取材质的开始位置

Dim endstrmat As Integer  '定义截取材质的结束位置

Dim caizhiID  As String '定义材质变量

Sub main()
Set swApp = Application.SldWorks
Set swModelDoc = swApp.ActiveDoc
Set swConfig = swModelDoc.ConfigurationManager.ActiveConfiguration
Set swModel = swApp.ActiveDoc
Set CustPropMgr = swModel.Extension.CustomPropertyManager(swModel.ConfigurationManager.ActiveConfiguration.Name) '配置特定延伸

'设定变量
c = swApp.ActiveDoc.GetTitle() '零件名
strmat = Chr(34) + Trim("SW-Material" + "@") + c + ".SLDPRT" + Chr(34)
a = InStr(c, " ") - 1      '重点:分隔标识符,这里是一个空格,也可换成其他符号
If a > 0 Then
   k = Left(c, a)
   t = Left(LTrim(e), 3)
   If t = "GBT" Then
      e = "GB/T" + Mid(k, 4)
   Else
         e = k
   End If
   b = Mid(c, a + 2)
   t = Right(c, 7)
If t = ".SLDPRT" Or t = ".SLDASM" Or t = ".sldprt" Or t = ".sldasm" Then
       j = Len(b) - 7 '消除后缀(区分大小写,即含4种)
    Else
        j = Len(b)
    End If
    m = Left(b, j)
End If

startIndex = InStr(c, ".") - 1  '重点:分隔标识符,这里用"."

endIndex = InStr(c, "-") - 1  '重点:分隔标识符,这里用"-"

If startIndex > 0 Then
   
    If endIndex > 0 Then
        
        If startIndex < endIndex Then
   
            projID = Mid(c, startIndex + 2, endIndex - startIndex - 1)
   
        End If
   
   End If
   
End If

startstrmat = InStr(strmat, " ") - 1  '重点:分隔标识符,这里用"."

endstrmat = InStr(strmat, "-") - 1  '重点:分隔标识符,这里用"-"

If startstrmat > 0 Then
   
    If endstrmat > 0 Then
        
        If startstrmat < endstrmat Then
   
            caizhiID = Mid(strmat, startstrmat + 2, endstrmat - startstrmat - 1)
   
        End If
   
   End If
   
End If





'删除栏

CustPropMgr.Delete ("图样代号")
CustPropMgr.Delete ("图样名称")
CustPropMgr.Delete ("Material")
CustPropMgr.Delete ("代号")
CustPropMgr.Delete ("名称")
CustPropMgr.Delete ("项目号")
CustPropMgr.Delete ("材料")


'新增

CustPropMgr.Add2 "代号", swCustomInfoText, e
CustPropMgr.Add2 "名称", swCustomInfoText, m
CustPropMgr.Add2 "数量", swCustomInfoText, ""
CustPropMgr.Add2 "单重", swCustomInfoText, ""
CustPropMgr.Add2 "总重", swCustomInfoText, ""
CustPropMgr.Add2 "备注", swCustomInfoText, ""
CustPropMgr.Add2 "代码", swCustomInfoText, ""
CustPropMgr.Add2 "材料", swCustomInfoText, strmat
CustPropMgr.Add2 "项目号", swCustomInfoText, projID
CustPropMgr.Add2 "规格", swCustomInfoText, ""
CustPropMgr.Add2 "标准厂商", swCustomInfoText, ""
CustPropMgr.Add2 "材质", swCustomInfoText, strmat

End Sub
回复

使用道具 举报

已有3人评论

wang933 发表于 2025-12-4 08:01:42
本来这宏用着是没有问题的 这几天突然就不能用了  不知道是不是win7打升级补丁造成的
回复

使用道具 举报

照哥真棒 发表于 2025-12-6 12:03:39
我试一下可以正常运行,
用其它宏看是否正常,如不正常,就与升级补丁有关。
回复

使用道具 举报

zhz135 发表于 2025-12-7 03:11:22

其他的宏是可以的
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ| Archiver|手机版|小黑屋| 碧波制图网 Stonespider

Copyright © 2021-2023 Kangli Wu   All Rights Reserved.

Powered by Discuz! X3.5( 苏ICP备18011607号-1 )

快速
回复
返回
列表
返回
顶部