cszhouhx01
发表于 2024-9-25 21:43:59
楼主你好!我按着你给的代码宏运行了还是有问题呢 运行报错
steve_suich
发表于 2024-9-26 10:27:40
cszhouhx01 发表于 2024-9-25 21:43
楼主你好!我按着你给的代码宏运行了还是有问题呢 运行报错
提供一下你的宏,我检查一下
cszhouhx01
发表于 2024-9-27 21:36:48
Dim swApp As Object
Dim Part As Object
Sub main()( d; Q1 b; F0 ?# l- {
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc4 t2 a3 y3 ~0 J9 @
Set swSelMgr = Part.SelectionManager
Set swComp = swSelMgr.GetSelectedObject(1)! D. @) D9 Z1 O4 s% c
oldpathname = swComp.GetPathName! t: iS9 z% v% S$ R$ _# x
Path = Left(oldpathname, InStrRev(oldpathname, "\"))
ntype = Mid(oldpathname, InStrRev(oldpathname, ".")): f% I; o3 ^. B# V/ iN- Y( I5 s
oldfi = Mid(oldpathname, InStrRev(oldpathname, "\") + 1)& _! r& R0 n4 o
oldname = Left(oldfi, InStrRev(oldfi, ".") - 1)2 d! N5 r1 [/ I# BM" H6 j
mip = InputBox("changename", "name", oldname)$ e* \5 I$ z- t; B" w
If mip <> "" Then
Part.Extension.RenameDocument mip8 b: X. F' n' yw
Part.Save
tmpfi = Dir(Path & "*.SLDDRW")
Do Until tmpfi = ""- v4 j; l6 {8 v9 h# M5 [
vDepend = swApp.GetDocumentDependencies(Path & tmpfi, False, False)
If Mid(vDepend(1), InStrRev(vDepend(1), "") + 1) = oldfi Then% I+ Y3 @)
Name Path & tmpfi As Path & mip & ".SLDDRW"
bl = swApp.ReplaceReferencedDocument(Path & mip & ".SLDDRW", vDepend(1), Path & mip & ntype)9 s+ K- S$ t8 i9 a& j' E0 B4 o2 @% w
Exit Do
End If
tmpfi = Dir* @: D+ V& bH3 |; B
Loop
End If@/ K' \6 k[/ y
End Sub2
你好 我就是按你这提供的代码写的宏
前流氓用户新号
发表于 2024-9-29 08:56:31
怎么讲
steve_suich
发表于 2024-9-30 09:49:00
这个宏代码确实有点奇怪,同样的代码在不同的电脑上运行有的可以,有的不行。SW版本都是2016,不好排查原因了。:funk:
steve_suich
发表于 2024-9-30 10:14:20
发现一个问题,如果工程图里面没有参考任何模型。那么这个代码vDepend = swApp.GetDocumentDependencies(Path & tmpfi, False, False)就会报错。
steve_suich
发表于 2024-9-30 10:38:51
cszhouhx01 发表于 2024-9-27 21:36
Dim swApp As Object
Dim Part As Object
Sub main()( d; Q1 b; F0 ?# l- {
If Mid(vDepend(1), InStrRev(vDepend(1), "") + 1) = oldfi Then缺少"\"
正确的应该是If Mid(vDepend(1), InStrRev(vDepend(1), "\") + 1) = oldfi Then