hi Matthew,
Matthew said:
Thanks again for that. I have managed to implement half of it but I am
having problems creating a sub routine.
This works just fine:
---
Option Compare Database
Option Explicit
Sub AddModuleToProject()
Dim Project As VBIDE.vbProject
Dim Component As VBIDE.vbComponent
Dim CodeModule As VBIDE.CodeModule
Set Project = Application.VBE.ActiveVBProject
Set Component = Project.VBComponents.Item("Callee")
Set CodeModule = Component.CodeModule
AddProcedureToModule CodeModule
End Sub
Sub AddProcedureToModule(ACodeModule As VBIDE.CodeModule)
Const DQUOTE = """" ' one " character
Dim LineNum As Long
LineNum = ACodeModule.CountOfLines + 1
ACodeModule.InsertLines LineNum, "Public Sub SayHello()"
LineNum = LineNum + 1
ACodeModule.InsertLines LineNum, " MsgBox " & DQUOTE & "Hello
World" & DQUOTE
LineNum = LineNum + 1
ACodeModule.InsertLines LineNum, "End Sub"
End Sub
---
Did you say that code wouldn't work in Access?
The code in a Word macro is using the Word object model. Take a closer
look at it. It can't work. You may try enclosing it into Word object, e.g.:
With myWord.ActiveWorkbook
End With
But you need to add a dot before each of the Word references:
---
Sub Macro1
Selection.TypeText Text:="test"
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.Font.Bold = wdToggle
End Sub
---
should look like
---
Public Sub CodeFromTextBox(AActiveWorkbook As Object)
With AActiveWorkbook
.Selection.TypeText Text:="test"
.Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
.Selection.Font.Bold = wdToggle
End With
End Sub
---
after your code injection.
mfG
--> stefan <--