Renaming Sub

H

Helmut Weber

Hi,
you need a reference to
Microsoft Visual Basic for Applications Extensibility.
Here a very basic example,
which is a search for a word rather than
a search for a name of a sub. Anyway, renaming
subs programmatically, I think, would not be
an everyday task. Except when playing with some
malicious code.
Sub RenameSub()
Dim aMdl As VBComponent
Dim aPrj As VBProject
Dim iLin As Integer
Dim NameOld$, NameNew$
NameOld$ = "Test444"
NameNew$ = "Test444new"
Set aPrj = Application.VBE.VBProjects("Normal")
Set aMdl = aPrj.VBComponents("NewMacros")
With aMdl.CodeModule
On Error GoTo notfound
iLin = .ProcBodyLine(NameOld$, ProcKind:=vbext_pk_Proc)
'Line containing NameOld$
.ReplaceLine Line:=iLin, String:="Sub " & NameNew$ & "()"
End With
Exit Sub
notfound:
MsgBox "not found"
End Sub
If NameOld$ contains a space, the string is not
found. If it isn't found, you would get an error
"Sub or function not defined". Very strange!
-
Thanks to Christian Freßdorf and Uwe Marx.
 
H

Helmut Weber

....
which is a search for a word rather than
a search for a name of a sub.
No, I was wrong here.
See help on ProcBodyLine and specially
on "vbext_pk_Proc" and "vbext_pk_Let" etc.
 
D

DA

Hallo Helmut
Sorry for late reply but was on holidays last week, so
not sure if you get to see this.

Thanks for the tip. That's exactly what I wanted.
Nothing malicious with my code I'm afraid. Just another
learning experience. Was trying to come up with a method
of switching a module on or off by renaming the sub.

I was experimenting with substitution module (ie.
EditPaste) Once you have the module you're replacing
Word's default action, but I wanted a method of just
disabling the substitution rather than having to deal
with the case in code elsewhere.

Dennis.
 
H

Helmut Weber

Hi DA,
I saw it. Thaks for the feedback.
Be sure, I see everything up to my newest posting.
---
Cheers and greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top