Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBE(Visual Basic Editor) Keep focus in VBE

Status
Not open for further replies.

Hellboy007

Technical User
Jul 8, 2005
14
CA
Hi

I have created a xla file. The main purpuse of that file, is to be able to
copy the code lines of the selected VBProject on to the clipboard. In that
file, i have created 2 userforms. The goal of the first one when called
upon, is to present in a TreeView control, all the Procedure that are
present in the selected vbaproject.

Now, the trouble that I have is that when i call the userform(show), two
things occur that I dont Want:

1- I Go back in Excel, insted of staying in VBE
2- When I unload the userform, I dont go back to the Vbaproject that a
selected before I call the Userform, i usually go back to the vbaproject
that contains my userform module.

So:
How I can I stay In vbe, while calling a userform ?
And :
How can I go back to the VbaProject that I was on before calling the
Userform when I unload it from the memorie. (this is how the sequence end
with)

Thank's

Philippe

p.s. I have receved this code that seam to work but i still go back on my userform sheet call upon !

Public Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function SetParent Lib "user32.dll" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long

Property Get Hwnd() As Long
Hwnd = FindWindow(vbNullString, Caption)
End Property

With Application.VBE.MainWindow
If .Visible And Not .WindowState = 1 Then 'minimized
.SetFocus
Call SetParent(Me.Hwnd, .Hwnd)
End If
End With
Repaint
 
1. In order to trigger a form in VBE you have to create a button on the VBE toolbars.
2. In order to trigger this button click you have to create a class.
3. In this class you have to identify the active VBComponent and focus on its Code Pane after the code execution:
Code:
On Error Resume Next
   NumePr = Application.VBE.SelectedVBComponent.Collection.Parent.Filename
   NumeMod = Application.VBE.SelectedVBComponent.Name
   If Err.Number > 0 Then Err.Clear: NumePr = Application.VBE.ActiveVBProject.Filename
   ' This side of the code make the button able to execute the code at click...
   Application.Run CommandBarControl.OnAction
On Error GoTo 0

The two variable (NumePr and NumeMod) have to be declared in a module:
Code:
Global NumePr As String, NumeMod As String

At the end of the code to be executed you have to write:
Code:
NumePr = Right(NumePr, Len(NumePr) - InStrRev(NumePr, "\", , vbTextCompare))
 If NumeMod <> "" Then
    Workbooks(NumePr).VBProject.VBComponents(NumeMod).Activate
 Else
   If Workbooks(NumePr).VBProject.Protection = vbext_pp_none Then
             Workbooks(NumePr).VBProject.VBComponents(1).CodeModule.CodePane.Show
   Else
       MsgBox "The VBA active project name is " & Chr(34) & NumePr & Chr(34) & _
        " - Password protected"
   End If
 End If

Using all these you have the name of the active VBProject in NumePr.

Fane Duru
 
Hi Fane Duru

I have used modoless form and this is the code that i used to keep focus:
Private Sub EvtHandler_Click(ByVal CommandBarControl As Object, Handled As Boolean, CancelDefault As Boolean)
Dim ipos1%, ipos2%, ipos3%

On Error Resume Next
With Application
.WindowState = xlMinimized
Call ClearClipboard

With .VBE.MainWindow
ipos1 = InStr(1, .Caption, "-") + 2
ipos2 = InStr(ipos1, .Caption, "-") - 1
ipos3 = InStr(ipos1, .Caption, "[") - 1
If ipos3 > 0 And ipos3 < ipos2 Then ipos2 = ipos3
strNomWorkbook = Mid(.Caption, ipos1, ipos2 - ipos1)
If strNomWorkbook = Empty Then
strNomWorkbook = Trim(Mid(.Caption, ipos1, ipos3 - ipos1))
.SetFocus
.CodePanes(1).SetSelection 1, 1, 1, 1
End If
.SetFocus
End With


.Run CommandBarControl.OnAction

End With
On Error GoTo 0

' Indicate to the Events object that we've successfully handled the event.
'
Handled = True
CancelDefault = True

End Sub

and I have used this for my userform:

Private Sub UserForm_Terminate()
On Error Resume Next
With Application.VBE
.MainWindow.SetFocus
.CodePanes(1).SetSelection 1, 1, 1, 1
End With
On Error GoTo 0
End Sub

Thank's for your help Fane !

Phil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top