动态添加函数后如何“同步”VB项目?

时间:2017-07-09 06:30:42

标签: excel vba excel-vba excel-2010

我正在编写一个Excel VBA项目,它在代码执行期间创建新函数。

新添加的功能需要能够立即执行。

虽然我确实看到添加了新功能,但在第一次执行时收到“1004”运行时错误。

以下代码将新函数testMethod添加到现有DynamicFunctions模块中(如果该函数尚未存在)并执行它。

Dynamic Code Generate & Execute@VBAProgramming The VBA Editor

获取的代码段

错误讯息: “运行时错误'1004': 无法运行宏'testMethod'。宏可能在此工作簿中不可用,或者可能禁用所有宏“

(关于第二次执行的注意事项,该函数已经存在于模块中,因此代码只执行它并弹出消息框)

Dim code As String
code = "Public Function testMethod()" & vbNewLine & _
            vbTab & "MsgBox """ & Time & """" & vbNewLine & _
            "End Function"

Dim methodExist As Boolean
methodExist = checkProcName("testMethod")

If (methodExist = False) Then
    Dim VBComp As VBIDE.VBComponent
    Set VBComp = ThisWorkbook.VBProject.VBComponents("DynamicFunctions")
    Call VBComp.CodeModule.AddFromString(code)
End If

Application.Run "testMethod"



Function checkProcName(sProcName As String) As Boolean
' ===========================================================================
' Found on http://www.cpearson.com at http://www.cpearson.com/excel/vbe.aspx
' then modified
'
' USAGE:
' to check if a procedure exists, call 'checkProcName' passing
' in the target workbook (which should be open), the Module,
' and the procedure name
'
' ===========================================================================
Dim oVBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule

Dim ProcName As String
Dim ProcKind As VBIDE.vbext_ProcKind

checkProcName = False

Set VBProj = ThisWorkbook.VBProject
Set VBComp = VBProj.VBComponents("DynamicFunctions")
Set CodeMod = VBComp.CodeModule

With CodeMod
    LineNum = .CountOfDeclarationLines + 1
    Do Until LineNum >= .CountOfLines
        ProcName = .ProcOfLine(LineNum, ProcKind)
        If ProcName = sProcName Then
            checkProcName = True
            Exit Do
        End If
        Debug.Print ProcName
        LineNum = .ProcStartLine(ProcName, ProcKind) + .ProcCountLines(ProcName, ProcKind) + 1
    Loop
End With


End Function

1 个答案:

答案 0 :(得分:0)

除非有特定原因需要在项目中使用 DynamicFunctions 模块,否则您可以随时创建一个,运行代码并在完成后删除。

dependencies": {
    "apisauce": "^0.14.0",
    "babel-polyfill": "^6.23.0",
    "bluebird": "^3.5.0",
    "body-parser": "^1.17.2",
    "classnames": "^2.2.5",
    "cookie-parser": "^1.4.3",
    "core-js": "^2.4.1",
    "express": "^4.15.3",
    "express-graphql": "^0.6.6",
    "express-jwt": "^5.3.0",
    "graphql": "^0.10.3",
    "history": "^4.6.3",
    "isomorphic-style-loader": "^2.0.0",
    "jsonwebtoken": "^7.4.1",
    "material-ui": "^0.18.6",
    "node-fetch": "^1.7.1",
    "normalize.css": "^7.0.0",
    "passport": "^0.3.2",
    "passport-facebook": "^2.1.1",
    "pretty-error": "^2.1.1",
    "prop-types": "^15.5.10",
    "query-string": "^4.3.4",
    "react": "^15.5.4",
    "react-dom": "^15.5.4",
    "react-tap-event-plugin": "^2.0.1",
    "sequelize": "^4.2.1",
    "serialize-javascript": "^1.3.0",
    "source-map-support": "^0.4.15",
    "sqlite3": "^3.1.8",
    "universal-router": "^3.2.0",
    "whatwg-fetch": "^2.0.3"
  },

修改

创建程序(如果不存在)并运行。

Public Sub CreateModuleRunMethodAndDelete()
    Dim code As String
        code = "Public Function testMethod()" & vbNewLine & _
                vbTab & "MsgBox """ & Time & """" & vbNewLine & _
                "End Function"

    'Create and append code
    With ThisWorkbook.VBProject
        With .VBComponents.Add(vbext_ct_StdModule)
            .Name = "Temp"
            .CodeModule.AddFromString code
        End With
    End With

    'Run
    Application.Run "testMethod"

    'Delete module
    With ThisWorkbook.VBProject
        .VBComponents.Remove .VBComponents("Temp")
     End With
End Sub