Inserting Code within ThisWorkbook via Code Q

S

Sean

I have the following Code that that works great under Office XP/2003
versions except one problem PC that uses Office XP and Windows 2000. I
have a Reference set to Microsoft VBA Extensibility v 5.3. Is there an
issue with running code via VBA to insert Code into ThisWorkbook under
Win2000?

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal ClassName As String, ByVal WindowName As String) As Long
Private Declare Function LockWindowUpdate Lib "user32" _
(ByVal hWndLock As Long) As Long

Sub Populate_TW_2()

Dim StartLine As Long
Dim msg1 As String, msg2 As String

Dim VBEHwnd As Long
On Error GoTo ErrH:
Application.VBE.MainWindow.Visible = False
VBEHwnd = FindWindow("wndclass_desked_gsk", _
Application.VBE.MainWindow.Caption)
If VBEHwnd Then
LockWindowUpdate VBEHwnd
End If

msg1 = "Worksheets(""View"").Visible = True" & vbCr & _
"Dim sh As Worksheet" & vbCr & _
"For Each ws In Worksheets " & vbCr & _
"If ws.Name = ""View"" Then"

msg2 = "ws.Visible = True" & vbCr & _
"Else" & vbCr & _
"ws.Visible = xlVeryHidden" & vbCr & _
"End If" & vbCr & _
"Next"


With ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
StartLine = .CreateEventProc("BeforeClose", "Workbook") + 1
..InsertLines StartLine, msg1 & vbCr & msg2
End With
Application.VBE.MainWindow.Visible = False
ErrH:
LockWindowUpdate 0&
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