Would someone explain what this code does?

J

John Smith

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:=2578,
recursive:=True).Execute
' WB.Save
End Sub
 

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