Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Jasmine Grant   on Sep 27 In MS Office Category.

  
Question Answered By: Jaime Bowman   on Sep 27

I've bodged this together from Word VBA code  that I have so I haven't tested
it yet. It should insert and run code into a module called modRunCode.


Sub subAddCode()
' To Add procedure code to a module.
' Must have a reference to the Extenibility Library.

Dim vblCodeMod As CodeModule
Dim slLine2 As String
Dim slLine3 As String
Dim slLine4 As String
Dim slLine5 As String
Dim sl1 As String
Dim vl1 As Variant

vl1 = "vl2"

slLine2 = "sub subRunCode"
slLine3 = ""
slLine4 = "end sub"
slLine5 = ""

Set vblCodeMod = _
Application.Document("Document"). _
VBProject. _
VBComponents("modRunCode"). _
CodeModule

slLine3 = vl1 & " = " & Chr(34) & vl1 & Chr(34)

' Delete old code.
On Error Resume Next
vblCodeMod.DeleteLines 2, 3
On Error GoTo 0

' Insert new code.
vblCodeMod.InsertLines 2, slLine2
vblCodeMod.InsertLines 3, slLine3
vblCodeMod.InsertLines 4, slLine4

' Do it.
Application.Run "subRunCode"

' Clean up.
Set vblCodeMod = Nothing
DoEvents

End Sub

Share: 

 

This Question has 2 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Placing VBA Code "Live" Into Another WkBook Via VBA Or get search suggestion and latest updates.


Tagged: