thebatfink
Well-known member
- Joined
- Mar 29, 2012
- Messages
- 47
- Programming Experience
- Beginner
Hi,
I have been doing this for a while within Excel using VBA. I'm now trying to perform the same function but using VB.NET.. So the code below (although modified to suit .NET) *used* to do what I wanted it to. Basically I am deleting a procedure from the ThisWorkbook codemodule and inserting a new procedure.
The problem appears to be VBComp = VBProj.VBComponents("ThisWorkbook") is expecting an integer for the component, not the components name. With Excel this would be "ThisWorkbook" or "Module2" etc etc. An integer is fine I guess, but how do I find out what the integer value is for the component I want?
Help would be much appreciated and may save what little hair I have left from being pulled out!!
Thanks
I have been doing this for a while within Excel using VBA. I'm now trying to perform the same function but using VB.NET.. So the code below (although modified to suit .NET) *used* to do what I wanted it to. Basically I am deleting a procedure from the ThisWorkbook codemodule and inserting a new procedure.
VB.NET:
Dim ExcelApp As Excel.Application = New Excel.Application
Dim PlanWorkBook As Excel.Workbook
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
PlanWorkBook = ExcelApp.Workbooks.Open(PlanFileName)
VBProj = PlanWorkBook.VBProject
VBComp = VBProj.VBComponents("ThisWorkbook")
CodeMod = VBComp.CodeModule
Dim ProcName As String = "Workbook_Open"
With CodeMod
Dim StartLine As Integer = .ProcStartLine(ProcName, VBIDE.vbext_ProcKind.vbext_pk_Proc)
Dim NumLines As Integer = .ProcCountLines(ProcName, VBIDE.vbext_ProcKind.vbext_pk_Proc)
.DeleteLines(StartLine:=StartLine, Count:=NumLines)
End With
With CodeMod
Dim LineNum As Integer = .CreateEventProc("Open", "Workbook")
LineNum = LineNum + 1
InsertLines(LineNum, "msgbox ""hi""")
End With
The problem appears to be VBComp = VBProj.VBComponents("ThisWorkbook") is expecting an integer for the component, not the components name. With Excel this would be "ThisWorkbook" or "Module2" etc etc. An integer is fine I guess, but how do I find out what the integer value is for the component I want?
Help would be much appreciated and may save what little hair I have left from being pulled out!!
Thanks