martedì 14 aprile 2009

VBA Office : Aggiungere blocco di codice a run time senza usare un file separato

Nella Finestra protezione macro attivare (scheda editor attendibili “considera attendibili al processo visual basic”)

You can add code to a module without using a separate text file that contains the code. The macro below shows how this can be done. The macro must be customized to contain to the code you want to add:

Sub AddProcedureCode(wb As Workbook, strModuleName As String)
' requires a reference to the Microsoft Visual Basic 5.0/6.0 Extensibility library
On Error Resume Next
With wb.VBProject.VBComponents(strModuleName).CodeModule
.InsertLines .CountOfLines + 1, "Sub NewSubName()"
.InsertLines .CountOfLines + 1, " Msgbox ""Hello World!"",vbInformation,""Message Box Title"""
.InsertLines .CountOfLines + 1, "End Sub" & Chr(13)
End With
On Error GoTo 0
End Sub

Sub TestAddProcedureCode()
AddProcedureCode ThisWorkbook, "Module1"
AddProcedureCode ThisWorkbook, Worksheets("Sheet1").CodeModule
End Sub

Nessun commento:

Posta un commento