ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Code to edit code (https://www.excelbanter.com/excel-programming/286527-code-edit-code.html)

Sam

Code to edit code
 
Is it possible to write code to open an excel workbook,
access the code (that is password protected), perform a
Find/Replace All, and close the workbook, saving changes?

Tim Barlow

Code to edit code
 
It's possible to open an Excel workbook and edit the code - but, AFAIK, only
if the code is not protected.

I don't think there is anyway programatically to unprotect the code, even if
you know the password.

Tim

"Sam" wrote in message
...
Is it possible to write code to open an excel workbook,
access the code (that is password protected), perform a
Find/Replace All, and close the workbook, saving changes?




Rob van Gelder[_4_]

Code to edit code
 
Sam,

Assuming your code is unprotected, you could use this code:

I've added a second condition so that it doesn't modify it's own procedure
(Not .ProcOfLine(i, vbext_pk_Proc) = "testit" )

Sub testit()
Dim strFind As String, strReplace As String, vbc As VBComponent, i As
Long, j As Long

strFind = "drink me"
strReplace = "eat me"

For Each vbc In ThisWorkbook.VBProject.VBComponents
With vbc.CodeModule
For i = 1 To .CountOfLines
j = InStr(1, .Lines(i, 1), strFind, vbTextCompare)
If j 0 And Not .ProcOfLine(i, vbext_pk_Proc) = "testit"
Then _
.ReplaceLine i, Replace(.Lines(i, 1), strFind,
strReplace, , , vbTextCompare)
Next
End With
Next
End Sub

Cheers


"Sam" wrote in message
...
Is it possible to write code to open an excel workbook,
access the code (that is password protected), perform a
Find/Replace All, and close the workbook, saving changes?




Tim Barlow

Code to edit code
 

PS - for info on how to programatically edit modules etc see Chip Pearson's
website at:
http://www.cpearson.com/excel/vbe.htm

Tim

"Tim Barlow" wrote in message
...
It's possible to open an Excel workbook and edit the code - but, AFAIK,

only
if the code is not protected.

I don't think there is anyway programatically to unprotect the code, even

if
you know the password.

Tim

"Sam" wrote in message
...
Is it possible to write code to open an excel workbook,
access the code (that is password protected), perform a
Find/Replace All, and close the workbook, saving changes?







All times are GMT +1. The time now is 12:32 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com