ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Would someone explain what this code does? (https://www.excelbanter.com/excel-programming/378816-would-someone-explain-what-code-does.html)

John Smith

Would someone explain what this code does?
 

Would someone explain what this line of code does?

If InStr(oWin.Caption, "(") 0 Then oWin.Close

Why does it looke for "("?

Here is the whole story. I would like to programmatically protect
the VBProject of a worksheet which is programmatically added. I
have a .xla file that adds a workbook (book1) with one sheet and
add codes to the sheet's worksheet_selectionchange event. I found
the following code on the Internet which is supposed to protect a
worksheet's VBProject. But no matter what I do, it is the xla's
VBProject that is protected, not the sheet1's VBProject. I guess
the code windows are not properly closed. But I cannot read the
code. Hence, I have no idea how to modify it. Thanks.



Public myBook as workbook

Application.SheetsInNewWorkbook = 1
Set myBook = Application.Workbooks.Add
....................................
....................................

call PVBP(myBook, "test")

Sub PVBP(WB As Workbook, ByVal Password As String)

Dim VBP As VBProject, oWin As VBIDE.Window
Dim wbActive As Workbook
Dim i As Integer

Set VBP = WB.VBProject
Set wbActive = ActiveWorkbook

' Close any code windows To ensure we hit the right project
For Each oWin In VBP.VBE.Windows
If InStr(oWin.Caption, "(") 0 Then oWin.Close
Next oWin

WB.Activate
' now use lovely SendKeys To unprotect
Application.OnKey "%{F11}"
SendKeys "+{TAB}{RIGHT}%V{+}{TAB}" & Password & "{TAB}" &
Password & "~"
Application.VBE.CommandBars(1).FindControl(ID:=257 8,
recursive:=True).Execute
' WB.Save
End Sub



All times are GMT +1. The time now is 05:38 PM.

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