如何删除重复的set语句?

时间:2016-08-11 11:29:08

标签: vba solidworks

最近,我研究VBA以自动化一些solidworks的工作。我写了以下代码。

Option Explicit

Dim swApp As SldWorks.SldWorks

Sub main()
    Set swApp = Application.SldWorks
    Call createNewPartDoc
    'Some code here
End Sub

Sub createNewPartDoc()
    Set swApp = Application.SldWorks
    'Some code here for create new part document
End Sub

然后,我注意到一些重复的代码。

如何在createNewPartDoc子例程或其他子例程中删除重复的代码(Set swApp = Application.SldWorks)?

1 个答案:

答案 0 :(得分:1)

只需将swApp作为参数传递:

Option Explicit

Sub main()
    Dim swApp As SldWorks.SldWorks
    Set swApp = Application.SldWorks
    Call createNewPartDoc(swApp)
    'Some code here
End Sub

Sub createNewPartDoc(App As SldWorks.SldWorks)
    'Some code here for create new part document
End Sub