Replacing VBA code on Mass

M

Mark M

Hi people,

I have a wonderful task of replacing a line of code in a
sub routine with word document. Whats bad about that I
hear you ask, well this line of code is in over 250
documents!

Is there a way to code something that can do a find and
replace within the VB code.

Any help will be much appreciated

Thanks

Mark
 
H

Helmut Weber

Hi Mark,
given, opening all the 250 docs is not a problem,
and you have set a reference to the appropriate
Microsoft Visual Basic for Applications Extensibility.
then the routine, to delete the first line that
contains "An Error" line, would look like this.
Sub DeleteLineInMacro()
Dim lLin As Long ' line
Dim i As Integer
VBE.MainWindow.Visible = True 'show editor
With VBE.VBProjects("Normal").VBComponents("ThisModule")
.CodeModule.CodePane.Show ' show code
lLin = .CodeModule.CountOfLines ' all lines
If .CodeModule.Find("An Error", 1, 1, lLin, 1) Then
.CodeModule.DeleteLines (lLin)
exit sub
End If
End If
End With
End Sub
Further is assumed: The line is in the project "normal"
and there in the component "ThisModule".
Is it possible though, to construct a loop over all
components od all modules. But beware, to place the above
routine at the end of whatever component. Otherwise,
it might delete the search string in itself.
A bit tricky, indeed!
Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
Word 97, W98
 
H

Helmut Weber

sorry about my English,
you must place the mentioned routine somewhere
after (!) the first occurance of the serach string,
otherwise...
 

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